关于模糊查询的一个小问题

zhujiebo 2011-06-12 03:03:04
下面这段结合vb编写的语言要如何改为模糊查询

Private Sub cmdInquire_Click()
Dim txtSQL As String
Dim MsgText As String
Dim dd(4) As Boolean
Dim mrc As ADODB.Recordset


txtSQL = "select * from result_Info where "
If Check1(0).Value Then
If Trim(txtSID.Text) = "" Then
sMeg = "学号不能为空"
MsgBox sMeg, vbOKOnly + vbExclamation, "警告"
txtSID.SetFocus
Exit Sub
Else
If Not IsNumeric(Trim(txtSID.Text)) Then
MsgBox "请输入数字!", vbOKOnly + vbExclamation, "警告"
Exit Sub
txtSID.SetFocus
End If
dd(0) = True
txtSQL = txtSQL & "student_ID = '" & Trim(txtSID.Text) & "'"
End If
End If

If Check1(1).Value Then
If Trim(txtName.Text) = "" Then
sMeg = "姓名不能为空"
MsgBox sMeg, vbOKOnly + vbExclamation, "警告"
txtName.SetFocus
Exit Sub
Else
dd(1) = True
If dd(0) Then
txtSQL = txtSQL & "and student_Name = '" & txtName.Text & "'"
Else
txtSQL = txtSQL & "student_Name = '" & txtName.Text & "'"
End If
End If
End If

If Check1(2).Value Then
If Trim(txtCourse.Text) = "" Then
sMeg = "课程不能为空"
MsgBox sMeg, vbOKOnly + vbExclamation, "警告"
txtCourse.SetFocus
Exit Sub
Else
dd(2) = True
If dd(0) Or dd(1) Then
txtSQL = txtSQL & "and course_Name = '" & txtCourse.Text & "'"
Else
txtSQL = txtSQL & "course_Name = '" & txtCourse.Text & "'"
End If
End If
End If

If Not (dd(0) Or dd(1) Or dd(2) Or dd(3)) Then
MsgBox "请设置查询方式!", vbOKOnly + vbExclamation, "警告"
Exit Sub
End If

txtSQL = txtSQL & " order by student_ID "
Set mrc = ExecuteSQL(txtSQL, MsgText)

With myflexgrid
.Rows = 2
.CellAlignment = 4
.TextMatrix(1, 0) = "考试编号"
.TextMatrix(1, 1) = "学号"
.TextMatrix(1, 2) = "姓名"
.TextMatrix(1, 3) = "班号"
.TextMatrix(1, 4) = "课程名称"
.TextMatrix(1, 5) = "分数"


Do While Not mrc.EOF
.Rows = .Rows + 1
.CellAlignment = 4
.TextMatrix(.Rows - 1, 0) = mrc.Fields(0)
.TextMatrix(.Rows - 1, 1) = mrc.Fields(1)
.TextMatrix(.Rows - 1, 2) = mrc.Fields(2)
.TextMatrix(.Rows - 1, 3) = mrc.Fields(3)
.TextMatrix(.Rows - 1, 4) = mrc.Fields(4)
.TextMatrix(.Rows - 1, 5) = mrc.Fields(5)
mrc.MoveNext
Loop

End With

mrc.Close
End Sub
...全文
62 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2011-06-12
  • 打赏
  • 举报
回复
模糊查询应该是算法问题,更应该从VB程序设计想办法.
打一壶酱油 2011-06-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 maco_wang 的回复:]
VB code
txtSQL = txtSQL & "and course_Name like '" & "%" & txtCourse.Text & "%" & "'"
[/Quote]

+1
叶子 2011-06-12
  • 打赏
  • 举报
回复
txtSQL = txtSQL & "and course_Name like '" & "%" &  txtCourse.Text & "%" & "'"
叶子 2011-06-12
  • 打赏
  • 举报
回复
把等号改成like 然后在后面的变量前后加上%

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧