Solved

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

Posted on 2013-01-19
6
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 48

Accepted Solution

by:
Dale Fye earned 500 total points
ID: 38797440
Try it with

", '" & stPeriod & "', "
0
 
LVL 48

Expert Comment

by:Dale Fye
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 48

Expert Comment

by:Dale Fye
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 48

Expert Comment

by:Dale Fye
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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