Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert into does keep the same value

Status
Not open for further replies.

ping99

Technical User
Mar 16, 2006
45
CA
Hi all,

I try to insert a string value date into a temptable ACCESS table but changed to unexpected value : WHY ?

dim strdate as string
dim strsql as string

strdate = "2006-04-30"

strsql = " INSERT INTO TEMPTABLE(VDATE) VALUES ("&strdate&")"

docmd.runsql(strsql)

but always gave me the value 1972 in my temptable for
the field vdate is the text field not date ? instead of 2006-04-30

Please assist or explain why ? it changed from 2006-04-30 to 1972.

Thank in advance,
ping



 
Let's see.

If you evaluate strSql in your code, it will be this string:
Code:
INSERT INTO TEMPTABLE(VDATE) VALUES (2006-04-30)

Now what you're really saying here is to insert into the vdate field the value 2006 minus 4 minus 30, or 1972. So, it's performing the arithmetic and inputting the result. :)

To fix this, you'll want to amend your SQL statement as follows:
Code:
INSERT INTO TEMPTABLE(VDATE) VALUES ([COLOR=red]'[/color]2006-04-30[COLOR=red]'[/color])
Which will say to enter 2006-04-30 as a literal string. To accomplish this from your VB code:
Code:
strsql = " INSERT INTO TEMPTABLE(VDATE) VALUES ([COLOR=red]'[/color]"&strdate&"[COLOR=red]'[/color])"
Should do the trick, assuming that your data type in your SQL table can automatically convert a string to a date.

HTH

Bob
 
Oops, forgot I was in Access. I believe you need double quotes instead of single quotes then? If so, you need to put
Code:
strsql = " INSERT INTO TEMPTABLE(VDATE) VALUES ([COLOR=red]""[/color]"&strdate&"[COLOR=red]""[/color])"
instead. In VB or VBA, to include a literal quotation mark in a string, you have to double it. This can get a bit confusing when you have quote marks around the string as well:
Code:
dim x as string
x = "I said ""Hello Joe, how are you?"" and Joe responded ""I'm fine, and you?"""
debug.print x
would show
Code:
I said "Hello Joe, how are you?" and Joe responded "I'm fine, and you?"
in the debug window.

HTH

Bob
 
Thanks a lot BOB,

You are right because I forgot a single quote ('"&strdate&"').

It works fine.

 
Suggestion: use # instead of quotes and format the date as d-mmm-yyyy to avoid date vs month confusion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top