Solved

Export table from Access Database- Getting a compile error

Posted on 2013-02-07
12
685 Views
Last Modified: 2013-02-28
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

0
Comment
Question by:bignadad
12 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 38863786
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.
0
 
LVL 84
ID: 38863791
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.
0
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 38863804
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.
0
 
LVL 2

Author Comment

by:bignadad
ID: 38863869
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
0
 
LVL 2

Author Comment

by:bignadad
ID: 38863928
This is what is calling it

Private Sub cmdExport_Click()
 Call ExportOneTable
End Sub
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 38863984
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
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:bignadad
ID: 38864224
Now i get this error
Untitled-picture.png
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 38864260
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

0
 
LVL 2

Author Comment

by:bignadad
ID: 38864295
No i get this error
Untitled-picture.png
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 38864342
oops, sorry try this one

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 = CreateObject("Access.Application")
    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

0
 
LVL 2

Author Comment

by:bignadad
ID: 38864407
Still get compile error
Untitled-picture.png
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 500 total points
ID: 38864534
ok, i need more coffee..

change this line

   objDB = OpenCurrentDatabase(strDB)

with

   objDB.OpenCurrentDatabase(strDB)

or
    objDB.OpenCurrentDatabase strDB
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now