I hope someone will be there to help me. I have a table with 1000 records. I want to generate an Excel 2007 workbook(.xlsx) every time when user hit a button on an Access form. I managed to do it by using following code but it allows me to create only a .xls file. I tried changing the file extension and “Excel 8.0” to “Excel 12.0” in the SQL statement but it gives me the runtime error 3734. Deeply appreciate if someone could kindly tell me how to create a .xlsx file.
Note: Below code is working without selecting DAO as mentioned in the code. When I try to select Microsoft DAO 3.6 Object Library from the reference list then I get a message saying "Name conflicts with existing module, project, or object library". I have no knowledge in the area to understand the issue. I have attached an screen shot to show you the references this db is using at the moment.
Private Sub cmdExcelOutput_Click()
'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:\MyProject\Output\Result.xls"
strWorksheet = "WorkSheet1"
strDB = "C:\MyProject\Output\MyDatabase.accdb"
strTable = "tblMasterData"
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
End Sub
Note: Below code is working without selecting DAO as mentioned in the code. When I try to select Microsoft DAO 3.6 Object Library from the reference list then I get a message saying "Name conflicts with existing module, project, or object library". I have no knowledge in the area to understand the issue. I have attached an screen shot to show you the references this db is using at the moment.
Private Sub cmdExcelOutput_Click()
'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:\MyProject\Output\Result.xls"
strWorksheet = "WorkSheet1"
strDB = "C:\MyProject\Output\MyDatabase.accdb"
strTable = "tblMasterData"
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
End Sub