Solved

Export table from Access Database- Getting a compile error

Posted on 2013-02-07
12
703 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
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

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)

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

749 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