Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    kawi6rr is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    39

    VBA Run a simple select query?

    I’m not quite sure where I’m going wrong here but this code should just show the results in a query window but when I click the button to run it nothing happens.



    Code:
    Private Sub btn_Classes_by_Date_Click()
    Dim strSQL As String
    
    
    Me.beginDate.Value = beginDate
    Me.endDate.Value = endDate
    
    
    strSQL = "SELECT MRN,Patient_Name,Surgery_Date,Scheduled_For,Enrolled_Date " & _
            "FROM Total_Joint_Readiness " & _
            "WHERE Scheduled_For >= '" & beginDate & "' AND Scheduled_For <= '" & endDate & "' "
    
    
    DoCmd.RunSQL strSQL
    
    
    End Sub
    any help is appreciated.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    No need to write in code.
    build a query then run it with...
    docmd.openQuery "qsMyQuery"

    make a form and read the dates off the form.

  3. #3
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Dates need to be surrounded by # not single quote.

  4. #4
    kawi6rr is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    39
    I'm referencing two from fields where I set value to beginDate and endDate. Thanks for the # I forgot about those and date fields but I'm getting this error when trying to run the code below.

    Run-time error ''12342
    A RunSQL action requires and argument consisting of an sql statement.

    Code:
    Me.beginDate.Value = beginDate
    Me.endDate.Value = endDate
    
    
    strSQL = "SELECT MRN,Patient_Name,Surgery_Date,Scheduled_For,Enrolled_Date" & _
             "FROM Total_Joint_Readiness" & _
             "WHERE Scheduled_For >= #" & beginDate & "# AND Scheduled_For <= #" & endDate & "# "
    
    
    DoCmd.RunSQL strSQL

  5. #5
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Try putting spaces at the end of each line (it reads Enrolled_DateFROM).

    Before the DoCmd add the statement: Debug.Print strSQL - then copy that to a new query window and you can test it there.

  6. #6
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Using "Between date 1 and date2" is easier to read, instead of less than equal to.

  7. #7
    kawi6rr is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    39
    Something else must be going on with this because I made all the changes so the code look like this now.

    debug.print strSQL showed an error which I fixed.

    Code:
    strSQL = "SELECT MRN,Patient_Name,Surgery_Date,Scheduled_For,Enrolled_Date " & _
                  "FROM Total_Joint_Readiness " & _
                  "WHERE Scheduled_For >= #' & beginDate & '# AND Scheduled_For <= #' & endDate & '# "
    I'm still getting the same error.
    Error = A RunSQL action requires and argument consisting of an sql statement.

  8. #8
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    The single quotes should be double quotes - debug.print should be showing you this!

  9. #9
    kawi6rr is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    39
    I had it both ways as single quote and double quote and I get the same error. degug.print did help me fix one error but not the current error. The debug.print code runs fine it's the vb code seems not to be recognizing the sql statement.

  10. #10
    kawi6rr is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    39
    This should show the sql statement in the query window when it's run correct?

  11. #11
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    The debug,print will print the SQL statement as it will run. You then copy the SQL and paste it into a query, then try and run it. Fix any errors you find,

  12. #12
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You are using
    Code:
    DoCmd.RunSQL strSQL
    From HELP about RunSQL:
    A string expression that's a valid SQL statement for an action query or a data-definition query. It (an "action query") uses an INSERT INTO, DELETE, SELECT...INTO, UPDATE, CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX, or DROP INDEX statement.

    You have a SELECT query and, in code, you are trying to open a record set.
    So:
    Code:
        Dim sSQL As String
        Dim rs As DAO.Recordset
        sSQL = "Select * from tbltest;"
        Set rs = CurrentDb.OpenRecordset(sSQL)
        '
        'more statements
        '
        '
        rs.Close
    Note: opening a query in code will not show the results in a query window (AFAIK).

  13. #13
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    I don't see where you posted that you're trying to open a recordset based on your sql statement. What were you intending to do with it? If you want to display the results, it makes little sense to construct it on the fly when a saved query can reference fields on your form. If you want to validate a returned value or check that there are records that meet the criteria, opening a recordset that you can check is one way. Supplying the sql to a newly opened form or report is another valid reason for doing this. Presenting a datasheet would not be. That being said, you would have to create a temporary (or saved) query def to provide a datasheet, which may not make sense either.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    kawi6rr is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    39
    The individual who requested this wants 2 fields where they can input 2 dates that filter the query for them, datepicker, so they only have to click. I originally had it open a saved parameter query but that forced her to type in the dates manually... I won't go into anymore details on that it's a hot spot So I figured I would setup 2 input fields and feed those dates into a query.

  15. #15
    kawi6rr is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    39
    Solved: Thanks for everyone who helped me I eventually figured out how to get what I wanted to work but it was quite a bit different approach to what I originally started with.

    Code:
    Private Sub btn_Classes_by_Date_Click()
    Dim conn As ADODB.Connection
    Dim strSQL As String
    
    
    DoCmd.SetWarnings False
    
    
    strSQL = "delete * from Scheduled_For"
    DoCmd.RunSQL strSQL
           
    strSQL = "INSERT INTO Scheduled_For(MRN,Patient_Name,Surgery_Date,Scheduled_For,Enrolled_Date) " & _
             "SELECT MRN,Patient_Name,Surgery_Date,Scheduled_For,Enrolled_Date " & _
             "FROM Total_Joint_Readiness " & _
             "WHERE Scheduled_For >= #" & Me!beginDate & "# AND Scheduled_For <= #" & Me!endDate & "# ;"
                         
    DoCmd.RunSQL strSQL
    
    
    DoCmd.OpenQuery "Classes_By_Date", , acReadOnly
    
    End Sub

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. "Too complex query" for a Simple Select Query
    By dragon232 in forum Queries
    Replies: 3
    Last Post: 11-29-2016, 02:45 PM
  2. Novice Needs Help With Simple Select Queries
    By GAtkins in forum Queries
    Replies: 5
    Last Post: 11-26-2013, 04:36 PM
  3. Simple Select Query
    By Plee in forum Queries
    Replies: 2
    Last Post: 04-18-2011, 02:34 PM
  4. Simple Select query
    By sandlucky in forum Queries
    Replies: 3
    Last Post: 03-30-2011, 04:26 AM
  5. Replies: 2
    Last Post: 03-31-2009, 11:15 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