Solved

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

Posted on 2013-01-19
6
292 Views
Last Modified: 2013-01-20
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
0
Comment
Question by:TechGuise
  • 4
6 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 38797440
Try it with

", '" & stPeriod & "', "
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38797442
This syntax includes a single quote  before and after your yyyy-mm string, so it looks like:

'yyyy-mm'
0
 
LVL 29

Expert Comment

by:IrogSinta
ID: 38797459
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 & ");"
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38797467
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.
0
 

Author Closing Comment

by:TechGuise
ID: 38798366
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!!!
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38798395
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 #.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now