Link to home
Start Free TrialLog in
Avatar of Addie Baker
Addie BakerFlag for United States of America

asked on

Export table from Access Database- Getting a compile error

I have a button setup on my access form to export a table. This process used to work, but is giving me the following error

Compile Error:
Expected user-defined type, not project

The is the code that the button is calling
Private Sub ExportOneTable()

'EXPORTS TABLE IN ACCESS DATABASE TO EXCEL
'REFERENCE TO DAO IS REQUIRED

Dim strExcelFile As String
Dim strWorksheet As String
Dim strDB As String
Dim strTable As String
Dim objDB As Database

'Change Based on your needs, or use
'as parameters to the sub
strExcelFile = "C:\EquipmentDatabase.xls"
strWorksheet = "sheet1"
strDB = "Z:\Database\Machinery\MachineryDatabase.accdb"
strTable = "sheet1"

Set objDB = OpenDatabase(strDB)

 'If excel file already exists, you can delete it here
 If Dir(strExcelFile) <> "" Then Kill strExcelFile

objDB.Execute _
  "SELECT * INTO [Excel 8.0;DATABASE=" & strExcelFile & _
   "].[" & strWorksheet & "] FROM " & "[" & strTable & "]"
objDB.Close
Set objDB = Nothing
    MsgBox "Exported to C:", , "Done"

End Sub

Open in new window

Avatar of mbizup
mbizup
Flag of Kazakhstan image

Your code looks fine and works perfectly for me -- tested.

So something has to be wrong with how you are implementing it...

Where have you placed the code and how are you calling it?




-  If it is not in the same form as the command button, remove the Private from the sub declaration.

-  Make sure that the file paths you have specified are valid

-  If the code is in a module, make sure that the module does not have the same name as your sub.  For example, give your module a name like "modExportFunctions"

-  If that doesn't help, try compact/repairing your database.
Try explicitly referring to the Database object:

Dim objDB As DAO.Database

If that doesn't work, try to Compact your database. If that doesn't work, compile the database: From the VBA Editor, click Debug - Compile, and fix any errors.

If that doesn't work, try importing everything into a new, blank database.

Also be sure that your Office and Windows installations are fully up to date.
In the Visual Basic Editor, you'll probably see an item Database Properties... in the Tools menu.

Select this item, change the Project Name to something different than Database(MachineryDatabase) (for example My Database), then click OK.
Avatar of Addie Baker

ASKER

I made this change

Dim objDB As DAO.Database

as suggested and it moves past that now.

But now im getting this error

Also, I tried to rename the database to my database.
New-Picture.bmp
This is what is calling it

Private Sub cmdExport_Click()
 Call ExportOneTable
End Sub
try changing this

objDB.Execute _
  "SELECT * INTO [Excel 8.0;DATABASE=" & strExcelFile & _
   "].[" & strWorksheet & "] FROM " & "[" & strTable & "]"

with

objDB.docmd.transferspreadsheet acexport,8, strTable, strExcelfile,true,strworksheet

see if that will give you the same error, if it did, you may have a permission issue writing to C: drive, try changing the path to the excel file
Now i get this error
Untitled-picture.png
try this revised codes

Private Sub ExportOneTable()

'EXPORTS TABLE IN ACCESS DATABASE TO EXCEL
'REFERENCE TO DAO IS REQUIRED

Dim strExcelFile As String
Dim strWorksheet As String
Dim strDB As String
Dim strTable As String
    'Dim objDB As Database

Dim objDB As Object

'Change Based on your needs, or use
'as parameters to the sub
strExcelFile = "C:\EquipmentDatabase.xls"
strWorksheet = "sheet1"
strDB = "Z:\Database\Machinery\MachineryDatabase.accdb"
strTable = "sheet1"

    'Set objDB = OpenDatabase(strDB)
Set objDB = OpenCurrentDatabase(strDB)
 'If excel file already exists, you can delete it here
 If Dir(strExcelFile) <> "" Then Kill strExcelFile

'        objDB.Execute _
'          "SELECT * INTO [Excel 8.0;DATABASE=" & strExcelFile & _
'           "].[" & strWorksheet & "] FROM " & "[" & strTable & "]"
   
objDB.DoCmd.TransferSpreadsheet acExport, 8, strTable, strExcelFile, True, strWorksheet

objDB.CloseCurrentDatabase
Set objDB = Nothing
    MsgBox "Exported to C:", , "Done"

End Sub

Open in new window

No i get this error
Untitled-picture.png
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Still get compile error
Untitled-picture.png
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial