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

    Query will not run

    I have created many queries without any problems. But now all I have is problems. When I run the following query, I get a message saying "A RunSQL action requires an argument consisting of an SQL statement". The query is:

    Dim strQue As String
    strQue = "SELECT tblProposalWorksheet.fLngProposalNo " _
    & "FROM tblProposalWorksheet " _
    & "WHERE (((tblProposalWorksheet.fLngProposalNo)=1993074)); "

    DoCmd.SetWarnings False
    DoCmd.RunSQL strQue
    DoCmd.SetWarnings True

    Provided I have a table named 'tblProposalWorksheet' with a field in it named 'fLngProposalNo' and a record or two where the fLngProposalNo equals '1993074', is there any reason it should not run? Thanks in advance.

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    Don't turn off the warnings and see what happens.

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    I think that "DoCmd.RunSQL" can only be used to run an Action query. Yours does not look like an action query to me.
    Perhaps you should try:
    DoCmd.OpenQuery
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    From Help:
    SQLStatement Required Variant. A string
    expression
    that's a valid SQL statement for an action query or a data-definition query. It uses an INSERT INTO, DELETE,
    SELECT...INTO, UPDATE, CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX, or
    DROP INDEX statement. Include an IN clause if you want to
    access another database.
    It really needs to be an Action query and not a passive Select query. Look into:
    CurrentDB.Execute SQL

  5. #5
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    216
    Neither DoCmd.OpenQuery or turning off the warnings has made any difference. Any other ideas?

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    As I said, CurrentDB.Execute

  7. #7
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    216
    Ok. I created and saved a query and used 'CurrentDB.Execute myQuery'. It said "Can not excute a select query".

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The commands "DoCmd.RunSQL " and "CurrentDB.Execute" only work with Action queries.

    "DoCmd.OpenQuery" only works with saved queries (a valid name of a query in the current database). It will open a Select query or execute Action queries.

    ---------------------------------------------------------------------------------

    Using VBA, you would use something like:
    Code:
    Public Sub test()
        Dim d As DAO.Database
        Dim r As DAO.Recordset
        Dim strQue As String
    
        Set d = CurrentDb
    
        strQue = "SELECT tblProposalWorksheet.fLngProposalNo " _
                 & "FROM tblProposalWorksheet " _
                 & "WHERE (((tblProposalWorksheet.fLngProposalNo)=1993074)); "
    
    
        Set r = d.OpenRecordset(strQue)
        
        If Not (r.BOF And r.EOF) Then
    
            'Do something
    
        End If
    
        r.Close
        Set r = Nothing
        Set d = Nothing
    End Sub

    ------------------------------------------------------------

    Create a query, switch to SQL view and paste in:
    Code:
    SELECT tblProposalWorksheet.fLngProposalNo
    FROM tblProposalWorksheet
    WHERE (((tblProposalWorksheet.fLngProposalNo)=1993074));
    Save the query as "Query3".

    Then run this code:
    Code:
    Public Sub test2()
        Dim strQue As String
    
        strQue = "Query3"
    
        DoCmd.OpenQuery (strQue)
    
    End Sub
    The query opens and is visible..... You can add new data and do edits, but not very useful (IMO - no control on how data is entered - no validation). Using a form & filter the record source would be much better....


    My $0.02

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    Steve's got it nailed. Why do you want to run a Select query this way anyway?

  10. #10
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    216
    I looked back at my other queries, and they are all action queries. My thinking was just wrong. Thanks for your persistence with a hard headed newbie.

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    Everyone has to start somewhere. Have fun. I marked this thread as Solved for you.

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

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