Hello all,
My Access 2010 database, using DAO formats, needs to export data to an existing Excel spreadsheet.
I found some VBA code which works great, if I give it a single clear-cut file name, but I have numerous projects in the database, and depending on which one is selected by a form, the d/b needs to export one out of potential many. Importantly, I need to send the data to an existing Excel file.
I have tried dimming a variable of ProjExport, but when I place it in the filename, I get errors, like syntax, etc. depending on different attempts.
Would anyone know how to correctly place a variable inside the file name? I using the same ProjExport
variable also, to depict a recordset, which would be the Access table of data I am exporting.
I am totally lost. Hopefully, this might be an easy one for an expert, thanks.
My Access 2010 database, using DAO formats, needs to export data to an existing Excel spreadsheet.
I found some VBA code which works great, if I give it a single clear-cut file name, but I have numerous projects in the database, and depending on which one is selected by a form, the d/b needs to export one out of potential many. Importantly, I need to send the data to an existing Excel file.
I have tried dimming a variable of ProjExport, but when I place it in the filename, I get errors, like syntax, etc. depending on different attempts.
Would anyone know how to correctly place a variable inside the file name? I using the same ProjExport
variable also, to depict a recordset, which would be the Access table of data I am exporting.
I am totally lost. Hopefully, this might be an easy one for an expert, thanks.
Code:
Private Sub Click_For_Excel_File_Export_Click()
Dim oExcel As Excel.Application
Dim oWorkBook As Excel.Workbook
Dim oWorksheet As Excel.Worksheet
Dim ProjFileName As String
'There are numerous projects kept inside the database, whcih can be selected by a form:
Dim ProjExport As String
Dim sTbl2 As String
sTbl2 = Nz(Me.ExistingProjNum, "")
If sTbl2 <> "" Then
ProjExport = Me.ExistingProjNum
ElseIf Me.NewProjNum <> "" Then
ProjExport = Me.NewProjNum
End If
'Would like to use a variable in the Excel workbook name, since there are numerous projects:
Set oWorkBook = oExcel.Workbooks.Open(Application.CurrentProject.Path \ "ProjExport & .xlsm")
'The specified worksheet to store project data
Set oWorksheet = oWorkBook.Worksheets("TotalHistory")
'The specified table which relates to the selected project:
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("ProjExport")
'The specified table
oWorksheet.Range("A5").CopyFromRecordset rs
rs.Close
oWorkBook.Save
oWorkBook.Close
oExcel.Quit
Set rs = Nothing
Set oWorksheet = Nothing
Set oWorkBook = Nothing
Set oExcel = Nothing
End Sub
Last edited by a moderator: