Well, I'm flummoxed here. I'm trying to execute an append query in which I use an input box to get a date I'm adding to the data.
When I run the query outside of VBA, I am prompted for the date and it works as expected. Not so much when trying to execute it through VBA. I'm trying to import credit card transactions from an Excel file, then add the Statement Closing Date.
Here's my code:
Code:
Private Sub cmdImportsAmexTx_Click()
Dim xls As Excel.Application
Dim wbk As Excel.Workbook
Dim xlRange As Excel.Range
Dim sFile As String
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim dtClose As String
Dim lRows As Long
Set db = CurrentDb
On Error GoTo Err_ImportAMEXTx
db.Execute "qryAmexTxTempDELETE"
[COLOR="Green"]'[Code that gets the Excel file][/COLOR]
Do
dtClose = InputBox("Enter the Statement Closing Date as mm/dd/yyyy", "Input Required")
If Len(dtClose) = 0 Then
MsgBox "The import process has been cancelled.", vbExclamation + vbOKOnly
GoTo Exit_ImportAMEXTx
End If
If Not IsDate(dtClose) Then
If vbCancel = MsgBox("You need to enter a proper date.", vbExclamation + vbOKCancel, "Invalid Input") Then GoTo Exit_ImportAMEXTx
End If
Loop While Not (IsDate(dtClose))
[COLOR="green"]'[Code to prep the Excel file][/COLOR]
[COLOR="green"]'---Import the file into a TEMP folder - Becuase the fields don't match up with the final table---[/COLOR]
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tblAmexTransactionsTEMP", sFile, True
Set qdf = db.QueryDefs("qryAmexTransactionsAppend")
qdf.Parameters(0) = Format(dtClose, "Short Date")
[COLOR="green"]'I've also tried qdf.Parameters(0) = cDate(dtClose), and qdf.Parameters(0) = "#" & dtClose & "#" and declaring DIM dtClose as Date initially.
'Before I added in the ClosingDate field, the update query worked, but then I was just doing a db.execute as I had no parameters. I also added a ClosingDate field to the TEMP table and formatted it as a date, which did not resolve the error.[/COLOR]
[COLOR="green"]'---This next line is where it fails---[/COLOR]
[COLOR="Red"]qdf.Execute "qryAmexTransactionsAppend"[/COLOR]
lRows = qdf.RecordsAffected
MsgBox "You added " & lRows & " records."
Exit_ImportAMEXTx:
Set qdf = Nothing
Set db = Nothing
Exit Sub
Err_ImportAMEXTx:
If Err.Number = 5 Then 'For cancelling the selection of the Excel file
GoTo Exit_ImportAMEXTx
Else
MsgBox Err.Number & " - " & Err.Description, vbCritical
Resume Exit_ImportAMEXTx
End If
End Sub
I just don't know what's amiss here.
This is the SQL-version of the Append Query, in case it's of use:
INSERT INTO tblAmexTransactions ( [Date], Receipt, Description, Amount, ReceiptURL, ExtendedDetails, DoingBusinessAs, ParentCompany, Address, PostCodeCountry, Reference, Category, ClosingDate )
SELECT tblAmexTransactionsTEMP.Date, tblAmexTransactionsTEMP.Receipt, tblAmexTransactionsTEMP.Description, tblAmexTransactionsTEMP.Amount, tblAmexTransactionsTEMP.[Receipt URL], tblAmexTransactionsTEMP.[Extended Details], tblAmexTransactionsTEMP.[Doing Business As], tblAmexTransactionsTEMP.[Parent Company], tblAmexTransactionsTEMP.[Street Address], tblAmexTransactionsTEMP.[City, State Zip], tblAmexTransactionsTEMP.Reference, tblAmexTransactionsTEMP.Category, [Input] AS ClosingDate
FROM tblAmexTransactionsTEMP
WHERE ((([Input])=[Input]));
Thanks in advance!
Last edited: