str = "and ([发站] like '广州') and ([到站] like '重庆')"
"select * from [车票] where (1 = 1) " & str
1.为什么用中文字段和表名?在一些系统中可能产生不稳定因素,也可能因为英文版的MDAC、SQL SERVER 等环节产生问题。
2.特殊字符、保留字作为字段、表名的时候,用“[]”括起来。
3.where 后面如果多表达式,每个表达式用“()”括起来,小心优先级问题。好像 and 的优先级很高的说(虽然比不过 not)
用trim函数可以把空格的问题解决了,
但是程序还是没有查出结果,仍然提示:在SELECT附近有语法错误,
不知还有什么问题,改后的程序如下:
Private Sub Command1_Click()
Dim str, a, b
a = Trim(DataCombo1.Text)
b = Trim(DataCombo2.Text)
If a <> "" Then
str = "and 发站 like '" & a & "'"
End If
If b <> "" Then
str = str & " and 到站 like '" & b & "'"
End If
If str <> "" Then
Adodc1.RecordSource = "select * from 车票 where 1=1 " & str
Adodc1.Refresh
End If
End Sub
在Adodc1.RecordSource = "select * from 车票 where 1=1 " & str处做了一个断点,
str的值为:"and 发站 like '广州' and 到站 like '重庆'"
a的值为:"广州"
b的值为:"重庆"
请求帮助.....
这样改一下:
Private Sub Command1_Click()
Dim str
If DataCombo1.BoundText <> "" Then
str = " and 发站 like '" & DataCombo1.BoundText & "'" /*移动"and"*/
End If
If DataCombo2.BoundText <> "" Then
str = str & " and 到站 like '" & DataCombo2.BoundText & "'" /*添加"and"*/
End If
If str <> "" Then
Adodc1.RecordSource = "select * from 车票 where 1=1 " & str /*增加1=1条件*/
Adodc1.Refresh
End If
End Sub
Private Sub Command1_Click()
Dim str
If DataCombo1.BoundText <> "" Then
str = " and 发站 like '%" & DataCombo1.BoundText & "%' "
End If
If DataCombo2.BoundText <> "" Then
str = str & " and 到站 like '%" & DataCombo2.BoundText & "%' "
End If
If str <> "" Then
Adodc1.RecordSource = " select * from 车票 where 1=1 " & str
Adodc1.Refresh
End If
End Sub
为了具体说明我把程序附上:
Private Sub Command1_Click()
Dim str
If DataCombo1.BoundText <> "" Then
str = "发站 like '" & DataCombo1.BoundText & "'and "
End If
If DataCombo2.BoundText <> "" Then
str = str & "到站 like '" & DataCombo2.BoundText & "'"
End If
If str <> "" Then
Adodc1.RecordSource = "select * from 车票 where " & str
Adodc1.Refresh
End If
End Sub
以上是一个查询的代码,
本来是两个查询的功能,但是作用都一样,只写了一个的代码,
界面上有四个datacombo,一个dataGrid,三个adodc,两个commond
分别进行了链接,
在运行后,会提示:
在select附近有语法错误,但是在SQL的查询分析器中可以查询啊?
能否给些详细的说明?