I have developed a form from which I would like to export the data to Excel.
Of course, this could simply be done by clicking External data > Export > Excel, but then the whole table will be exported.
What I would like is that only the data of the current record, so the record I have selected in the form, will be exported to Excel. Therefore, I have found a code, but it seems not to work.
This code should select all data from tblProductions for which counts that ID is equal to the ID selected in the form. All selected data should then be pasted into the newly created table Temp and from there the data should be exported to the file Test.xls, which is in the same directory as the database.
However, the table Temp and the file Test.xls are indeed created, yet they are completely empty, while there should be data in it, since tblProductions does also contain data.
Of course, this could simply be done by clicking External data > Export > Excel, but then the whole table will be exported.
What I would like is that only the data of the current record, so the record I have selected in the form, will be exported to Excel. Therefore, I have found a code, but it seems not to work.
Private Sub Export_Click()
Dim db As Database
Dim cn As ADODB.Connection
Dim strSQL As String
Dim pad As String
Set cn = New ADODB.Connection
Set db = CurrentDb()
pad = CurrentProject.path
If Right(pad, 1) <> "\" Then pad = pad & "\"
' Selecteer alles (*) naar tabel Temp
' vanuit tabel tblProductions
' waar geldt: ID is gelijk aan ID huidige record
strSQL = "SELECT * INTO Temp " & vbCrLf
strSQL = strSQL & "FROM [tblProductions] " & vbCrLf
strSQL = strSQL & "WHERE ([ID]=" & Me.[ID] & ");"
On Error Resume Next
Kill pad & "\Test.xls"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
On Error GoTo 0
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Temp", pad & "Test.xls", True
End Sub
This code should select all data from tblProductions for which counts that ID is equal to the ID selected in the form. All selected data should then be pasted into the newly created table Temp and from there the data should be exported to the file Test.xls, which is in the same directory as the database.
However, the table Temp and the file Test.xls are indeed created, yet they are completely empty, while there should be data in it, since tblProductions does also contain data.