Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-01-19
6
Medium Priority
?
344 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

722 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