?
Solved

Export table from Access Database- Getting a compile error

Posted on 2013-02-07
12
Medium Priority
?
713 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 85
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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
 
LVL 2

Author Comment

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

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 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 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

765 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