Results 1 to 7 of 7
  1. #1
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    175

    Don't understand why can't use select query


    I fell like I am missing something. The most common type of query (that I need) is the select query. When I try a select query (in code), I get the error "A RunSQL action requires an argument consisting of an SQL statement". So I end up building a 'make table' query. I just want to make sure I am not missing anything. Any help is appreciated.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I get the error "A RunSQL action requires an argument consisting of an SQL statement"
    "RunSQL" can only be used for action queries. Append, Update and Delete are action queries. A SELECT query is NOT an action query.

    In code, for select queries, you use something like:

    Code:
    Option Compare Database
    Option Explicit
    
    Sub SomeProcedure()
    
    Dim d As DAO.Database
    Dim r As DAO.Recordset
    Dim strSQL As String
    
    strSQL = "SELECT Field1, Field2 FROM MyTable WHERE Field3 = " & Me.cboControl1 & " ORDER BY Field1;"
    
    Set r = CurrentDb.OpenRecordset (strSQL)
    ' do stuff
    
    r.Close
    Set r = Nothing
    
    End Sub
    or

    Code:
    Option Compare Database
    Option Explicit
    
    Sub SomeProcedure()
    
    Dim d As DAO.Database
    Dim r As DAO.Recordset
    Dim strSQL As String
    
    Set d = CurrentDb
    
    strSQL = "SELECT Field1, Field2 FROM MyTable WHERE Field3 = " & Me.cboControl1 & " ORDER BY Field1;"
    
    Set r = d.OpenRecordset(strSQL)
    
    ' do stuff
    
    r.Close
    Set r = Nothing
    Set d = Nothing
    
    End Sub

  4. #4
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    175
    Ok ssanfu, that worked. However, how do I reference the data in the query?

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Depends on what you are trying to do.
    A recordset opened in code cannot be bound to a form; the recordsetcreated in code can only be accessed in code.

    For instance, I have a dB where I enter work hours. Rather than have a combo box to select each worker (approx 3000) and enter the hours, I first create dummy records with the hours = -1111. Then I open the hours entry form, selecting only those hours where the hours - 1111.

    When I click the "Enter Hours" button, the code uses a recordset to select all of the employees. It then loops through the recordset adding a record for each active employee. Then the form opens with only the employees with hours = -1111 (the form is filtered). I enter the actual hours (maybe some of the employees didn't work this month). When I close the form, all records where hours = -1111 are deleted.

    Two recordsets are opened in code, one for the active workers (call it recordset "W") and one to enter the hour records (call it "E").
    If the worker in the current record of recordset "W" is determined to be an active employee, then a record is inserted into recordset "E".

    That is an overview of one way to use a recordset in code.

    If you can explain what you trying to do, we should be able to help you with the code.

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    Further to ssanfu's comments, and using his sample code. I'm not sure exactly what you mean by

    how do I reference the data in the query
    If by query you mean
    Code:
     "SELECT Field1, Field2 FROM MyTable WHERE Field3 = " & Me.cboControl1 & " ORDER BY Field1;"
    You should not look at that as a query in Access terms. That is the sql of the recordset r.

    If you want to refer to fields in that recordset you can use:

    r!Field1
    r!Field2

    or
    r.fields("Field1")
    r.fields("Field2")
    Last edited by orange; 01-28-2014 at 06:09 PM.

  7. #7
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    175
    Thanks Orange, that is exactly what I was looking for and I thank both of you.

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

Similar Threads

  1. Help me understand queries in my code
    By Datech in forum Queries
    Replies: 0
    Last Post: 06-15-2012, 08:32 PM
  2. Someone to help me understand a guide
    By carrybag93 in forum Access
    Replies: 4
    Last Post: 06-04-2012, 12:20 AM
  3. Need to understand code
    By accessnewb in forum Programming
    Replies: 2
    Last Post: 08-03-2011, 12:03 PM
  4. Need To Understand MS Access Across A Network
    By johnmagu in forum Access
    Replies: 8
    Last Post: 05-25-2010, 03:14 PM
  5. Help me to understand forms
    By RTaylor in forum Forms
    Replies: 0
    Last Post: 04-10-2010, 01:36 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