用vb做access的查询在MSHFlexGrid控件上只能显示出一个满足条件的选项
’我想把所有符合要求的都显示出来OptionExplicitDimconnAsADODB.ConnectionDimrsAsADODB.RecordsetDimcmdAs...
’我想把所有符合要求的都显示出来
Option Explicit
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Private Sub cmdquery_Click()
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\db2.mdb;" & "Persist Security Info=False"
conn.Open
cmd.CommandType = adCmdText
cmd.ActiveConnection = conn
If cboSelect.Text = "学号" Then
cmd.CommandText = "select*from emptable where 学号 like" & Val(txtquery.Text)
ElseIf cboSelect.Text = "姓名" Then
cmd.CommandText = "select*from emptable where 姓名 ='" & txtquery.Text & " ' "
ElseIf cboSelect.Text = "性别" Then
cmd.CommandText = "select*from emptable where 性别 ='" & txtquery.Text & " ' "
End If
Set rs = cmd.Execute '执行命令
Set MSHFlexGrid1.DataSource = rs '显示查询结果
Refresh
rs.Close
conn.Close
End Sub
Private Sub form_load()
cboSelect.AddItem "学号"
cboSelect.AddItem "姓名"
cboSelect.AddItem "性别"
End Sub 展开
Option Explicit
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Private Sub cmdquery_Click()
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\db2.mdb;" & "Persist Security Info=False"
conn.Open
cmd.CommandType = adCmdText
cmd.ActiveConnection = conn
If cboSelect.Text = "学号" Then
cmd.CommandText = "select*from emptable where 学号 like" & Val(txtquery.Text)
ElseIf cboSelect.Text = "姓名" Then
cmd.CommandText = "select*from emptable where 姓名 ='" & txtquery.Text & " ' "
ElseIf cboSelect.Text = "性别" Then
cmd.CommandText = "select*from emptable where 性别 ='" & txtquery.Text & " ' "
End If
Set rs = cmd.Execute '执行命令
Set MSHFlexGrid1.DataSource = rs '显示查询结果
Refresh
rs.Close
conn.Close
End Sub
Private Sub form_load()
cboSelect.AddItem "学号"
cboSelect.AddItem "姓名"
cboSelect.AddItem "性别"
End Sub 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询