sql的错误

来源:百度知道 编辑:UC知道 时间:2024/09/21 06:15:17
Dim sql As String

Dim rs2 As New ADODB.Recordset
ch1 = ""
ch2 = ""
ch3 = ""
ch4 = ""

If Check1.Value = 1 And Len(Text1.Text) <> 0 Then

ch1 = "学号 like '%" & Text1.Text & "%'and "
End If
If Check3.Value = 1 And Len(Combo1.Text) <> 0 Then
ch2 = "班级 like '%" & Combo1.Text & "%'and "
End If
If Check2.Value = 1 Then
ch3 = " between #" & DTP2.Value & "# and #" & DTP2.Value & "#and "
End If
If Check4.Value = 1 And Len(Text23.Text) <> 0 Then

ch4 = "姓名 like '%" & Text23.Text & "%'and "
End If
sql = ch1 & ch2 & ch3 & ch4
sql = Left(sql, Len(sql) - 4)
sql = "select * from dy where " & sql
rs2.CursorLocation = adUseClient '设置游标为客户端
rs2.Open sql, cn, 3, 1
If rs2.RecordCount >

感觉是当 Len(sql) 〈 4 时,Left(sql, -2)这样的时候就会报错吧
===========================
如果你上面的四个IF都没走。。那SQL的长度就是0呀。。。Left(sql,len(sql)-4)就是left(sql,-4),就会报错了呀。
建议你这么写
If Check1.Value = 1 And Len(Text1.Text) <> 0 Then
ch1 = " and 学号 like '%" & Text1.Text & "%'"
End If
If Check3.Value = 1 And Len(Combo1.Text) <> 0 Then
ch2 = " and 班级 like '%" & Combo1.Text & "%'"
End If
If Check2.Value = 1 Then
ch3 = " and between #" & DTP2.Value & "# and #" & DTP2.Value & "#"
End If
If Check4.Value = 1 And Len(Text23.Text) <> 0 Then
ch4 = " and 姓名 like '%" & Text23.Text & "%'"
End If
sql = ch1 & ch2 & ch3 & ch4
sql = "select * from dy where 1=1 " & sql

Dim sql As String

Dim rs2 As New ADODB.Recordset
ch1 = ""
ch2 = ""
ch3 = ""