Results 1 to 2 of 2
  1. #1
    Esmatullaharifi is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2014
    Posts
    40

    Question vba code with query

    I have a form with three text boxes and one button. I want to use these three text boxes as parameters for specific query, thus i want a code that has the ability to pass these three value to query parameters or another code to solve this problem.


    I need your help dear guise.
    Thanks,

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    If you open any query and switch to SQL View, you will see the SQL code behind the query.
    What I often do in these situations, is use the Text Box selections and VBA to build the SQL code I need for the query. Then assign the built SQL code to an existing query, and open it.

    What I do to keep the SQL code in VBA small is I first create a query that has all my records and calculations (without the criteria). I then save it (let's call it Query1). I then create a second query which selects all the records from Query1 (using the asterisk) and adds in my Criteria. Let's call that Query2.

    So, then my SQL code will build the SQL code for Query2 based on my Criteria selections.

    Here is some sample code that builds that code, assuming the first text box is a numeric entry and the second is a string entry. I then open a Report at the end which uses Query2 as its Control Source.
    Code:
    Private Sub cmdRunReport_Click()
    
        Dim mySelect As String
        Dim myCriteria As String
        Dim mySQL As String
        
    '   Get basis of SQL code
        mySelect = "SELECT Query1.* FROM Query1 WHERE" 
        
    '   Add first criteria (numeric example)
        myCriteria = " [Field1]=" & Me.TextBox1
    
    '   Add second criteria (string example)
        myCriteria = myCriteria & " AND [Field2]=" & Chr(34) & Me.TextBox2 & Chr(34)
    
    '   Build complete SQL string
        mySQL = mySelect & myCriteria";"
        
    '   Assign SQL to query
        CurrentDb.QueryDefs("Query2").SQL = mySQL
        
    '   Open report
        DoCmd.OpenReport "Report1", acViewPreview, "", ""
    
    End Sub
    A few things to note:
    - Chr(34) returns double-quotes. All string values must be enclosed in quotes in SQL.
    - To see exactly how your SQL query should look, build an "example" in Query2, switch to SQL View, and note the code (print or copy). This is what you are trying to build with VBA. To help debug, you can add a line of code after you build the SQL code to return its contents and compare to what you are trying to build, i.e.
    Code:
    ...
    '   Build complete SQL string
        mySQL = mySelect & myCriteria";"
        MsgBox mySQL
    ...

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 10-15-2014, 02:23 AM
  2. Run Query in VB code
    By T_Tronix in forum Access
    Replies: 3
    Last Post: 02-10-2014, 02:14 PM
  3. Replies: 1
    Last Post: 05-04-2013, 12:19 PM
  4. Query using VBA code
    By pepok4 in forum Programming
    Replies: 13
    Last Post: 02-05-2013, 02:17 PM
  5. query using code
    By rohini in forum Queries
    Replies: 1
    Last Post: 05-17-2012, 03:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums