I have a query reside in a access file(.mdb), I would like to call this query in VBA, and store the result into a existing table. The previous content of the table should be deleted before the result was inserted.
Any idea? In my existing code, the query name “genInboundCdr” was execute via DoCmd.TransferSpreadsheet and result was stored into a excel file.
Private Sub BtnExecuteQuery_Click()
If IsNull(txtOutputPath.value) Then
MsgBox "Please enter a valid output file location."
Else
If ObjectExists("Query", "genInboundCdr") Then
strPathToSave = txtOutputPath.value
MsgBox "About to extract inbound cdr..." & vbCrLf & _
"Please notice that the query may take longer time " & _
"( > 20 minutes ) if the linked tables contains a lot " & _
"of records."
txtStatus.value = txtStatus.value & _
"About to extract inbound cdr..." & vbCrLf & _
"Please notice that the query may take longer time " & _
"( > 20 minutes ) if the linked tables contains a lot " & _
"of records." & vbCrLf
DoCmd.TransferSpreadsheet acExport, _
acSpreadsheetTypeExcel9, _
"genInboundCdr", _
strPathToSave, _
True
MsgBox ("Inbound Cdr generated.")
txtStatus.value = txtStatus.value & "Inbound Cdr generated." & vbCrLf
Else
MsgBox ("Query does not exist! Please review your steps.")
End If
End If
End Sub
I end up doing like this:
1) query and store the result into a excel file.
2) import the excel file using transferspreadsheet.