Hello all,
I have a database with a table called RawData. I have a query that takes a parameter called "Date of Comm" and the query appends the filtered results into a table called Comms2. The parameter obtained by the query is passed as a formatted date mmmm yyyy to my table. I only need the month and year because this is a monthly report. Anyhow, this query works no problem. The report that works off of Comms2 works as well.
The report I mentioned has a few subreports each reading from a different table for various information. I'd like to automate how these tables are appended with info from the different queries by putting it all into one vba module. I'm testing this out with one query because if I can get one to work then I'm sure I can copy the same pattern and get them all to work. The queries mainly use the same parameter of Month Year.
I started by going to my query that appends to Comms2 and I copied the SQL code. I started a new sub which is below. I've broken the SQL into multiple lines so that I can read/follow it easier.
If I run the code below as is, and enter February 2022 as my date, then it posts to Comms2 as December 1899. I tried a different month/year combination and got the same result.
Within the SQL string, I tried using Format(commDate, "mmmm yyyy"). When I run it I get the inputbox to fill in, but then I get an error message: Run-time error '3075': Syntax error (missing operator) in query expression 'February 2022'.
Private Sub TestingQuerySQL()
Dim commDate As Date
commDate = InputBox("Enter Date of Commission", "Determine Commission Period", "Month Year")
Dim strQuery As String
strQuery = "INSERT INTO Comms2 ( DateofComm, [Last Name], [N/U], Instrument, Manufacturer," & _
"Model, SerialNumber, [Trade-In], Cost, CreditCardCost, ServiceCost, GoodsCost, Sale, TotalCost," & _
"GP, GPPercent, SP1, Store, [Store%], SP2, Store2, [Store%2] )" & _
"SELECT " & commDate & " AS DateofComm, RawData.[Last Name], RawData.[N/U], RawData.Instrument," & _
"RawData.Manufacturer, RawData.Model, RawData.SerialNumber, RawData.[Trade-In], RawData.Cost," & _
"RawData.CreditCardCost, RawData.ServiceCost, RawData.GoodsCost, RawData.Sale," & _
"Nz([Cost],0)+Nz([CreditCardCost],0)+Nz([ServiceCost],0)+Nz([GoodsCost],0) AS TotalCost," & _
"[Sale]-[TotalCost] AS GP, [GP]/[Sale] AS GPPercent, RawData.SP1, RawData.Store," & _
"RawData.[Store%], RawData.SP2, RawData.Store2, RawData.[Store%2]" & _
"FROM RawData " & _
"GROUP BY " & commDate & ", RawData.[Last Name], RawData.[N/U], RawData.Instrument, RawData.Manufacturer," & _
"RawData.Model, RawData.SerialNumber, RawData.[Trade-In], RawData.Cost, RawData.CreditCardCost," & _
"RawData.ServiceCost, RawData.GoodsCost, RawData.Sale, RawData.SP1, RawData.Store, RawData.[Store%], RawData.SP2, RawData.Store2, RawData.[Store%2];"
DoCmd.RunSQL strQuery
End Sub
So the question is how do I get vba to format my input response into MMMM YYYY and append it to my table?
Thanks,
John