Results 1 to 8 of 8
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    Remove querydef

    I'm using code below to make whatever the record source is an excel spreadsheet - two functions are to work together and make it easier for me to do export to.

    I can't find the fault in the error. I have this picking out multiple subforms and I get errors or the wrong recordsource (if it doesn't delete the original object querydef it uses the same one over for some reason)

    help :O

    Code:
    'save as dialog box
    Public Function SaveAsLocation(Optional filename As String) As String
    Dim strButtonCaption As String
    Dim strDialogTitle As String, strfilename As String
     
    'Define your own Captions if necessary
    strButtonCaption = "Select a Folder"
    strDialogTitle = "Folder Selection Dialog"
    If Not IsMissing(filename) Then
    strfilename = filename
    Else
    strfilename = ""
    End If
     
    With Application.FileDialog(msoFileDialogSaveAs)
      .ButtonName = strButtonCaption
      .InitialView = msoFileDialogViewDetails     'Detailed View
      .title = strDialogTitle
      .InitialFileName = strfilename
         If .SHOW Then
          SaveAsLocation = .SelectedItems(1)         'Write Folder Path to Field
         End If
    End With
    End Function
    Code:
    'gives you excel of records
    Public Sub ExportToExcel(RecordSource As String, filename As String)
    If FindQuery("QueryExportToExcel") Then
    CurrentDb.QueryDefs.Delete "QueryExportToExcel"
    End If
    
    
    Dim qryname As String
    Dim qdfNew As QueryDef
    On Error GoTo ErrorHandler
    Dim strSave As String
    
    
    strSave = SaveAsLocation(filename)
    qryname = "QueryExportToExcel"
    Set qdfNew = CurrentDb.CreateQueryDef(qryname, RecordSource)
    
    
    Debug.Print strSave
    DoCmd.TransferSpreadsheet acExport, 8, qryname, strSave & ".xls", True
    Debug.Print RecordSource
    CurrentDb.QueryDefs.Delete qdfNew.Name 'cleanup
    ErrorHandler:
       Resume Next
    End Sub
    Code:
    'find query
    Function FindQuery(strQueryName) As Boolean
    Dim dbs As dao.Database
    Dim found As Boolean
    Set dbs = CurrentDb
    
    
    On Error GoTo fail
    'next line succeeeds without error if qry exists
    found = Len(dbs.QueryDefs(strQueryName).Name) > 0
    
    
    exithere:
    FindQuery = found
    
    
    
    
    fail:
    'no query of that name
    found = False
    Resume exithere
    End Function


  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    You have an error (two errors, actually) in the FindQuery function - it is returning False every time it is called. Right now if the Found test succeeds, the code sets FindQuery = Found. That really should be FindQuery = True, but it still works because any non-zero boolean value is True.

    But that isn't the main problem - after setting Findquery = True, the code continues, sets Found = False, then sets
    FindQuery = Found.

    So FindQuery is always false.

    Put Exit Function after the FindQuery = Found (Findquery = True).

    John

  3. #3
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by John_G View Post
    Hi -

    You have an error (two errors, actually) in the FindQuery function - it is returning False every time it is called. Right now if the Found test succeeds, the code sets FindQuery = Found. That really should be FindQuery = True, but it still works because any non-zero boolean value is True.

    But that isn't the main problem - after setting Findquery = True, the code continues, sets Found = False, then sets
    FindQuery = Found.

    So FindQuery is always false.

    Put Exit Function after the FindQuery = Found (Findquery = True).

    John
    Thanks - the problem I get in that at the exporttoexcel function

    If FindQuery("QueryExportToExcel") Then
    CurrentDb.QueryDefs.Delete "QueryExportToExcel"
    End If

    I get an error at
    CurrentDb.QueryDefs.Delete "QueryExportToExcel"

  4. #4
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I changed
    If FindQuery("QueryExportToExcel") = false Then

    and it skipped the error... not sure as to why

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    If FindQuery("QueryExportToExcel") Then ...
    and
    If FindQuery("QueryExportToExcel") = false Then...

    are the opposite of each other.

    If FindQuery("QueryExportToExcel") is the same as If FindQuery("QueryExportToExcel") = True






  6. #6
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by John_G View Post
    Hi -

    If FindQuery("QueryExportToExcel") Then ...
    and
    If FindQuery("QueryExportToExcel") = false Then...

    are the opposite of each other.

    If FindQuery("QueryExportToExcel") is the same as If FindQuery("QueryExportToExcel") = True




    Yes but I thought the way that I made it that i it finds a query def of name then = true

    however it is of false

    I guess I could swap the function bool around in the function...

  7. #7
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Did you fix the FindQuery function with the addition of the Exit Function line?

    It should be OK otherwise.

    What is the error that CurrentDb.QueryDefs.Delete "QueryExportToExcel" gives you?

  8. #8
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by John_G View Post
    Hi -

    Did you fix the FindQuery function with the addition of the Exit Function line?

    It should be OK otherwise.

    What is the error that CurrentDb.QueryDefs.Delete "QueryExportToExcel" gives you?
    Can't quite remember but I think it was missing object error - yeah updated the code as specified in your post. Thanks

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

Similar Threads

  1. Updating a QueryDef
    By The Professor in forum Queries
    Replies: 3
    Last Post: 08-05-2014, 03:57 PM
  2. QueryDef
    By crowegreg in forum Programming
    Replies: 2
    Last Post: 09-18-2012, 09:31 AM
  3. Can someone explain querydef?
    By roguex20 in forum Access
    Replies: 2
    Last Post: 07-22-2012, 11:57 AM
  4. Exporting to Excel with Automation using QueryDef
    By Niezels in forum Import/Export Data
    Replies: 3
    Last Post: 09-26-2010, 05:55 PM
  5. Adding Totals to a query using queryDef
    By jrickels in forum Programming
    Replies: 7
    Last Post: 04-06-2010, 07:47 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