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

    Delete Query From VBA

    My code does not throw an error, but it is not deleting the query either. What is the proper way to delete a query if it exists in VBA?



    Code:
        Dim qdf As QueryDef
        Dim queryName As String
        
        queryName = "vbaExportQuery"
    
    
        For Each qdf In CurrentDb.QueryDefs
            If qdf.Name = "queryName" Then
                CurrentDb.QueryDefs.Delete queryName
            End If
        Next

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,543
    Remove the quotes from

    If qdf.Name = "queryName" Then

    Use:
    If qdf.Name = queryName Then
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Or
    .delete qdf.name

  4. #4
    knarfreppep is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Feb 2015
    Location
    Adelaide, Australia
    Posts
    106

    Cut to the chase ...

    Quote Originally Posted by jo15765 View Post
    My code does not throw an error, but it is not deleting the query either. What is the proper way to delete a query if it exists in VBA?

    Code:
        Dim qdf As QueryDef
        Dim queryName As String
        
        queryName = "vbaExportQuery"
    
    
        For Each qdf In CurrentDb.QueryDefs
            If qdf.Name = "queryName" Then
                CurrentDb.QueryDefs.Delete queryName
            End If
        Next
    If you know the query exists, delete ALL this unnecessary code and use just CurrentDb.QueryDefs.Delete "vbaExportQuery" ... if you're not sure the query exists, use the same code but trap/ignore Error 3265.

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

Similar Threads

  1. Delete query to delete records that meet criteria
    By neill_long in forum Queries
    Replies: 1
    Last Post: 06-11-2018, 02:41 PM
  2. Replies: 7
    Last Post: 09-16-2017, 04:01 PM
  3. Delete Button vs. Delete Query
    By JennyL in forum Access
    Replies: 4
    Last Post: 02-11-2017, 10:11 PM
  4. Replies: 2
    Last Post: 01-24-2012, 02:16 PM
  5. Trying to Delete record using delete query
    By MooseOTL in forum Access
    Replies: 13
    Last Post: 10-04-2011, 02:30 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