VB高手支招~~~语法错误

来源:百度知道 编辑:UC知道 时间:2024/06/08 08:14:58
Private Sub label10_click()
Dim StrSQL As String
Frame3.Visible = True '显示frame3
Frame2.Visible = False
StrSQL = "select * from lentinfo where 读者编号= '" & Text1.Text & "' and 借书日期>= #" & DTPicker1.Value & "# and 借书日期 <= #" & DTPicker2.Value & "#"
Set g_rs = g_db.OpenRecordset(StrSQL)
If Not g_rs.EOF Then
g_rs.MoveLast '指针移动到最后一项,以获得借书量
Text2.Text = g_rs.RecordCount
Else
Text2.Text = "0"
End If
在上面语句中提示StrSQL = "select * from lentinfo where 读者编号= '" & Text1.Text & "' and 借书日期>= #" & DTPicker1.Value & "# and 借书日期 <= #" & DTPicker2.Value & "#"
Set g_rs = g_db.OpenRecordset(StrSQL)这据有语法错误。请高手帮忙啊?

StrSQL = "select * from lentinfo where 读者编号= '" & Text1.Text & "' and 借书日期>= #" & DTPicker1.Value & "# and 借书日期 <= #" & DTPicker2.Value & "#"
改成:
StrSQL = "select * from lentinfo where 读者编号= '"
StrSQL=StrSQL+Text1.Text +"' and 借书日期>= #"
StrSQL=StrSQL+DTPicker1.Value+"# and 借书日期 <= #"
StrSQL=StrSQL+DTPicker2.Value +"#"

如果数据库里该字段(借书日期)不是日期型的话可以考虑把DTPicker1.Value 转换成字符串后用"'"连接.用# 进行日期型查询很容易出错.

你用的是什么数据库?如果是sql server那么将全部#改成'号就行,access才是用#连接日期型。还有g_db.OpenRecordset(strsql)是不是一个类模块,如果还没解决就把这个代码贴上来

两种方法,你试式:
1. 将日期两端的#改为'
2. 放个中断在这一行上,看看DTPicker1.Value的值格式是怎样的