how to give a value of textbox in query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hardik Patel
    New Member
    • May 2011
    • 21

    how to give a value of textbox in query

    hi guys,
    I have a combobox and i want to use that value in query
    suppose I have form in that i have textbox and combo box
    and button. And I have code like this in button onclick event.

    Code:

    Code:
    If Combo2.Value = "Department" Then
    strSQL = "select distinct * from Laptops where Department like '*" & txtKeyword.Value & "*'  "
    Me.SubLaptops.Controls.Item("txtQuantityOrdered") = DSum("[quantity ordered]", "Laptops", "[Department] like '*" & txtKeyword.Value & "*'")

    but i want make query of this code how can i do?
    Code:
    DSum("[quantity ordered]", "Laptops", "[Department] like '*" & txtKeyword.Value & "*'")
    i want to pass the value of textbox in query.
    i tried like this

    Code:
    select sum[quantity order] from laptop
    where [comboox value] = "whatever User Select the value from combobox " & [textbox value] =" whatever user type"

    Thanks a lot.
    Last edited by Niheel; Jun 5 '11, 04:59 PM. Reason: Code MUST be posted in CODE tags
  • Mihail
    Contributor
    • Apr 2011
    • 759

    #2
    Create a PUBLIC function:
    Code:
    Public Function WhereClause() as String
        WhereClause = YourForm.YourComboBox.Text & YourForm.YoutTextBox.Text
    End Function
    In the "Criteria" row of your query write: WhereClause()

    Of course the name of the function can be anything else.

    I am sure that exist an easy way but I am not skilled enough in Acces VBA.

    Good luck !

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      That is one good answer. Another is to refer to the item specifically within your SQL (See Referring to Items on a Sub-Form for more on this) :

      Code:
      strSQL = "SELECT DISTINCT * " & _
               "FROM Laptops " & _
               "WHERE "Department Like '*" & Forms("Formname").txtKeyword.Value & "*')"

      Comment

      Working...