2

I'm using VB6 and MS Access. my sql command is

insert into BatchInfo (BName,BDate,Currency) values('" & Me.txtBatchName.Text & "','" & Me.dtpBatchDate.Value & "','" & Me.cboCurrency.Text & "')

the output of the command at run time is

"insert into BatchInfo (BName,BDate,Currency) values('batch1','8/2/2012','AED')"

here is the schema of the BatchInfo Table

BatchID   AutoNumber
BName     Text
BDate     Date/Time
Currency  Text

I cannot find any syntax error. Please help.

1
  • 3
    If you're going to build up SQL strings, PLEASE PLEASE PLEASE sanitise your database input otherwise you WILL get problems. Commented Aug 3, 2012 at 16:00

3 Answers 3

6

Currency is a reserved word, escape it thusly;

insert into BatchInfo (BName, BDate, [Currency]) values (...
Sign up to request clarification or add additional context in comments.

Comments

2

MS Access typically likes # signs around its dates:

insert into BatchInfo (BName,BDate,Currency) 
values('" & Me.txtBatchName.Text & "','#" & Me.dtpBatchDate.Value & "#','" & Me.cboCurrency.Text & "')

9 Comments

@Deanna I would strongly suggest year, month, day formats (yyyy-mm-dd, yyyy/mm/dd) rather than American, especially for people in non-US locales.
I am sorry but Allen Browne is not telling the whole story in this case, a very rare thing for Allen Browne. Access does indeed use American dates as default which leads to problems for people based in other locales, but what Access needs is an unambiguous date, and 4 digit year, month, day is the most unambiguous date that you can get.
Sadly MSDN is failing me and I can;t find a definitive reference, but this page on query criteria uses #mm/dd/yyyy# in the examples examples.
One of the problems for people working in non-US locales is that Access does not always need a US date, for example, in the query design window (v.2010), if you type #2012/07/05# Access itself it will change it to #05/07/2012#, not #07/05/2012#, so people working outside US locale are generally best using year,month,day. It is far less confusing for everyone.
|
0

You are using single quotes, not double quotes.

Try this

insert into BatchInfo (BName,BDate,Currency) values(""" & Me.txtBatchName.Text & """,#" & Me.dtpBatchDate.Value & #",""" & Me.cboCurrency.Text & """)

3 Comments

but this statement works fine. "insert into Admission_Master values('" & id & "','" & Me.txtstdcode.Text & "','" & Me.cmbcourse.Text & "','" & modules & "','" & Me.dtpadmtd.Value & "','" & cfees1 & "','" & Me.dtpsdt.Value & "','" & Me.dtpedt.Value & "','N/A'," & CBool(False) & ",'" & cfees1 & "')"
Some RDBMS accept both ' and " delimeters. Others (e.g. mysql) only likes one of them
I've always just used double quotes "" in Access. Good to know there's another way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.