Solved

Export table from Access Database- Getting a compile error

Posted on 2013-02-07
12
709 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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 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 120

Assisted Solution

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

Technology Partners: 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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

690 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