语法错误动态创建SQL查询

时间:2012-08-03 15:23:14

标签: sql ms-access vb6

我正在使用VB6和MS Access。我的sql命令是

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

运行时命令的输出是

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

这是BatchInfo表的架构

BatchID   AutoNumber
BName     Text
BDate     Date/Time
Currency  Text

我找不到任何语法错误。请帮忙。

3 个答案:

答案 0 :(得分:6)

Currency是一个保留词,因此逃避它;

insert into BatchInfo (BName, BDate, [Currency]) values (...

答案 1 :(得分:2)

MS Access通常在其日期周围标有#个标志:

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

答案 2 :(得分:0)

您使用的是单引号,而不是双引号。

试试这个

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