Link to home
Start Free TrialLog in
Avatar of TechGuise
TechGuiseFlag for United States of America

asked on

Append data through RunSQL statement, need to format as text.

My problem is a field that hold period information formats the data as yyyy-mm

When I use the statement below, "2013-01" becomes "2012"  (2012 minus 1)

I've tried Cstr(stPeriod), Trim, ???

Here is what I have used

DoCmd.RunSQL "INSERT INTO tblInvTransactions ( [TicketNo], [Period], [TranDate], [InvItem], [QtyIn], [Notes])" _
& "Values (" & stTicketNo & ", " & stPeriod & ", " & "#" & stTranDate & "#" & ", " & stInvItem & ", " & stQtyIn & ", " & stNotes & ");"

Thanks for any help
ASKER CERTIFIED SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This syntax includes a single quote  before and after your yyyy-mm string, so it looks like:

'yyyy-mm'
Either what fyed said or if you had assigned the value of stPeriod previously from a date, you can just use the Format function in your statement instead.

DoCmd.RunSQL "INSERT INTO tblInvTransactions ( [TicketNo], [Period], [TranDate], [InvItem], [QtyIn], [Notes])" _
& "Values (" & stTicketNo & ", " & Format([Period,  "yyyy-mm") & ", " & "#" & stTranDate & "#" & ", " & stInvItem & ", " & stQtyIn & ", " & stNotes & ");"
The other thing, I would not use the runsql command.  I use the execute method, which has a syntax:

Currentdb.execute strSQL, [options]

And if you use dbFailOnError as the option, then it will generate an error if there is a syntax error or some other error is encountered during the operation.

You can add error handling to deal with those.

The other advantage is that this method avoids the warnings that you get with action queries.
Avatar of TechGuise

ASKER

That did it.   I wish a had a better grasp of when to put things in quotes, single quotes, etc...

I would have thought that would just enter the text "stPeriod" instead of the value.

Thanks......... A LOT!!!
If you had done the following it would have entered 'stPeriod'

& ", 'stPeriod' ," &

The rule  is, if you want to insert a value that is stored in a a string variable (I do it for dates too) it must be wrapped.  For strings, you use single quote ( generally), for dates, you use #.