Results 1 to 11 of 11
  1. #1
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183

    Object invalid or no longer set

    I have been using this export function for the longest time and today it is giving me an error:


    Object invalid or no longer set.

    Export code:


    Code:
    On Error GoTo Err_cmdTest_Click
    'Must 1st set a Reference to the Microsoft Office XX.X Object Library
    Dim dlgOpen As FileDialog
    Dim strExportPath As String
    Const conOBJECT_TO_EXPORT As String = "EXPORT"
    
    
    Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
    
    
    With dlgOpen
      .ButtonName = "Export To"
      .InitialView = msoFileDialogViewLargeIcons
      .InitialFileName = CurrentProject.Path
         If .Show = -1 Then
           'Allow for Root Directory selection: C:\, D:\, etc.
           strExportPath = Replace(.SelectedItems(1) & "\", "\\", "\")
     Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
                                   TableName:=conOBJECT_TO_EXPORT, _
                                   FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
                                                                   
      Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
                                   TableName:="EXPORT_CapacityBuilding_DATA", _
                                   FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
        
    
    
    
    
           MsgBox "[" & conOBJECT_TO_EXPORT & "] has been Exported to " & strExportPath & _
                   conOBJECT_TO_EXPORT & ".xls", vbInformation, "Export Complete"
         End If
    End With
    
    
    'Set the Object Variable to Nothing.
    Set dlgOpen = Nothing
    
    
    DoCmd.Close
    Exit_cmdTest_Click:
      Exit Sub
    
    
    Err_cmdTest_Click:
      MsgBox Err.Description, vbExclamation, "Error in cmdTest_Click()"
        Resume Exit_cmdTest_Click

    any thoughts?


    thanks in advance

  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Are you able to tell which line of code is causing the error? (Click "Debug" at the error message, and the line of code causing the error should be highlighted).
    Last edited by John_G; 02-24-2015 at 02:31 PM. Reason: Change the wording

  3. #3
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    Quote Originally Posted by John_G View Post
    Are you able to tell which line of code is causing the error? (Click "Debug" at the error message, and the line of code causing the error should be highlighted).
    I did not get any Debug option (see attached image). I also tried to use 'Debug --> step into' in the visual basic and it did not give me any error as well *confused*
    Attached Thumbnails Attached Thumbnails Region Capture.jpg  

  4. #4
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    It's because you have the On Error Go To statement.

    Comment out that line, and run the code again - you should be able to see the debug then.

  5. #5
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    Quote Originally Posted by John_G View Post
    It's because you have the On Error Go To statement.

    Comment out that line, and run the code again - you should be able to see the debug then.
    Thanks again. I got an error on this:
    Code:
    Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
                                   TableName:=conOBJECT_TO_EXPORT, _
                                   FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
    also see attached imageClick image for larger version. 

Name:	123.jpg 
Views:	14 
Size:	168.0 KB 
ID:	19826

  6. #6
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    The only thing I can think of at the moment is that "Export" is not the name of a table in your database.

    If you comment out the first TransferToSpreadsheet command, do you still get the error when you run it?

    Also, I have just noticed that both your TransferToSpreadsheet commands have the same destination -
    FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls"


    Surely that's not right?

  7. #7
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    Quote Originally Posted by John_G View Post
    The only thing I can think of at the moment is that "Export" is not the name of a table in your database.

    If you comment out the first TransferToSpreadsheet command, do you still get the error when you run it?

    Also, I have just noticed that both your TransferToSpreadsheet commands have the same destination -
    FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls"


    Surely that's not right?
    Yes, you are right. Both my TransferToSpreadsheet commands have same destination because they are two different queries and I would like them to be exported in one excel file but different spreadsheet. The codes used to work. I am not sure what happened today that it stop working and giving me an error.

    I tried to remove the line call docmd. I do get an option to export but nothing gets exported even when it says my file has been exported to the location selected. Nothing as in I do not see any Excel file in my folder.

  8. #8
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    I'm not familiar with Call, but try running it without "Call", i.e. just use docmd.transferspreadsheet .....

  9. #9
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    Quote Originally Posted by John_G View Post
    I'm not familiar with Call, but try running it without "Call", i.e. just use docmd.transferspreadsheet .....
    Thanks again John_G.

    I tried using docmd and it won't work

    Click image for larger version. 

Name:	docmd.jpg 
Views:	13 
Size:	101.1 KB 
ID:	19832

  10. #10
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Remove the brackets from the parameter list:

    DoCmd.TransferSpreadsheet TransferType:=acExport, _
    TableName:=conOBJECT_TO_EXPORT, _
    FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls"

  11. #11
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    Quote Originally Posted by John_G View Post
    Remove the brackets from the parameter list:

    DoCmd.TransferSpreadsheet TransferType:=acExport, _
    TableName:=conOBJECT_TO_EXPORT, _
    FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls"

    Thank you so much for your help. I am not sure why the codes seem to be working fine now.

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

Similar Threads

  1. Replies: 5
    Last Post: 07-22-2014, 06:58 AM
  2. "Invalid Database Object Reference"
    By jgelpi16 in forum Queries
    Replies: 4
    Last Post: 06-28-2011, 06:39 AM
  3. invalid database object reference
    By survivo01 in forum Access
    Replies: 1
    Last Post: 06-02-2011, 04:02 PM
  4. Invalid database object reference.
    By jgelpi16 in forum Programming
    Replies: 4
    Last Post: 04-11-2011, 02:48 PM
  5. docmd.openform Invalid database object reference
    By snoopy2003 in forum Programming
    Replies: 5
    Last Post: 03-15-2011, 09:11 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