Results 1 to 3 of 3
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    VBA Not Building Query

    This is my syntax and I am attempting to


    1) Delete query if already exists
    2) Create new query

    My syntax presents no errors, but the query is not created.
    Code:
    Private Sub Ctl_Click()
    Dim strsql As String, qdf As QueryDef
    
    
        'Deleting query if exists
        On Error Resume Next
        CurrentDb.QueryDefs.Delete ("EB")
        
        strsql = "Select firstbase, secondbase, thirdbase, so, h, w from baseballstats"
        Set qdf = CurrentDb.CreateQueryDef("EB", strsql)
    
    
        'Open the Report
        DoCmd.OpenForm "EB", acNormal
    End Sub
    What do I need to change so that tthe query is created?

  2. #2
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    862
    Here's a start:
    Code:
    Function fcnCustomizeSQL(qName As String, strPassedSQL As String) As Boolean
        Dim qthisQuery As DAO.QueryDef
        If TempVars!tvEnableErrorHandling = True Then On Error GoTo fcnCustomizeSQL_Error   'if the query has been deleted, create it
        If DCount("Name", "MSysObjects", "[Name] = " & Chr$(39) & qName & Chr$(39)) = 0 Then
            Set qthisQuery = CurrentDb.CreateQueryDef(qName, strPassedSQL)
            Set qthisQuery = Nothing
            Exit Function
        End If
    
        Set qthisQuery = CurrentDb.QueryDefs(qName)
        qthisQuery.sql = strPassedSQL
    fcnCustomizeSQL_Exit:
        On Error Resume Next
        Set qthisQuery = Nothing
        Exit Function
    fcnCustomizeSQL_Error:
        MsgBox Err.Number & ", " & Err.Description & ", fcnCustomizeSQL"
        Resume fcnCustomizeSQL_Exit
    End Function

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    you may need to force a save to get the query to appear in the navigation pane, or manually refresh it through the right click menus.

    Though I would question why you are replacing this query, this seems a useless step when your query should produce the same result every time.

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

Similar Threads

  1. Trouble with building query in VBA...
    By sstiebinger in forum Programming
    Replies: 4
    Last Post: 08-21-2015, 09:13 AM
  2. Replies: 5
    Last Post: 08-11-2014, 10:08 AM
  3. Replies: 6
    Last Post: 06-25-2014, 12:40 PM
  4. Replies: 7
    Last Post: 08-30-2013, 03:43 PM
  5. Building a Query that can extract data and sum
    By Sara Bellum in forum Queries
    Replies: 5
    Last Post: 03-15-2013, 08:57 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