VB中对access模糊查找后,MSHFlexGrid里面显示不全
OptionExplicitDimconnAsADODB.ConnectionDimrsAsADODB.RecordsetDimcmdAsADODB.CommandPri...
Option Explicit
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Private Sub Command1_Click()
Dim s As Integer
If Combo1.Text = "编号" Then
cmd.CommandText = "select * from sharetable where ID=" & Val(Text1.Text)
ElseIf Combo1.Text = "文件名" Then
cmd.CommandText = "select * from sharetable where 文件名 like '%%" & Text1.Text & "%%'"
Else
'cmd.CommandText = "select * from sharetable where IP地址 like '%%" & Text1.Text & "%%'"
cmd.CommandText = "select * from sharetable where IP地址 = '" & Text1.Text & "'"
End If
Set rs = cmd.Execute
Set MSHFlexGrid1.DataSource = rs
rs.Close
conn.Close
End Sub
Private Sub Form_Load()
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 & "\share.mdb;" & "Persist Security Info=False;"
conn.Open
cmd.CommandType = adCmdText
cmd.ActiveConnection = conn
Combo1.AddItem "编号"
Combo1.AddItem "文件名"
Combo1.AddItem "IP地址"
End Sub 展开
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Private Sub Command1_Click()
Dim s As Integer
If Combo1.Text = "编号" Then
cmd.CommandText = "select * from sharetable where ID=" & Val(Text1.Text)
ElseIf Combo1.Text = "文件名" Then
cmd.CommandText = "select * from sharetable where 文件名 like '%%" & Text1.Text & "%%'"
Else
'cmd.CommandText = "select * from sharetable where IP地址 like '%%" & Text1.Text & "%%'"
cmd.CommandText = "select * from sharetable where IP地址 = '" & Text1.Text & "'"
End If
Set rs = cmd.Execute
Set MSHFlexGrid1.DataSource = rs
rs.Close
conn.Close
End Sub
Private Sub Form_Load()
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 & "\share.mdb;" & "Persist Security Info=False;"
conn.Open
cmd.CommandType = adCmdText
cmd.ActiveConnection = conn
Combo1.AddItem "编号"
Combo1.AddItem "文件名"
Combo1.AddItem "IP地址"
End Sub 展开
1个回答
展开全部
Option Explicit
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cmd As String
Private Sub Command1_Click()
Dim s As Integer, i As Integer
If Combo1.Text = "编号" Then
cmd = "select * from sharetable where ID=" & Val(Text1.Text)
ElseIf Combo1.Text = "文件名" Then
cmd = "select * from sharetable where 文件名 like '%" & Text1.Text & "%'"
Else
cmd = "select * from sharetable where IP地址 like '%" & Text1.Text & "%'"
End If
rs.Open cmd, conn, 3, 2
s = rs.RecordCount
With MSHFlexGrid1
.Cols = 3 '这个可以设置,你需要显示的列数
.Rows = s + 1
.TextMatrix(0, 0) = "编号" '设置列名称
.TextMatrix(0, 1) = "文件名"
.TextMatrix(0, 2) = "IP地址"
For i = 1 To s
.TextMatrix(i, 0) = rs!ID '读取数据表字段的值
.TextMatrix(i, 1) = rs!文件名
.TextMatrix(i, 2) = rs!IP地址
rs.MoveNext
Next i
End With
rs.Close
End Sub
Private Sub Form_Load()
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\share.mdb;" & "Persist Security Info=False;"
conn.Open
Combo1.AddItem "编号"
Combo1.AddItem "文件名"
Combo1.AddItem "IP地址"
End Sub
Private Sub Form_Unload(Cancel As Integer)
conn.Close
End Sub
问题1:模糊查询关键字Like后面是一个百分号,不能是二个百分号。
2、显示全部记录必须浏览全部记录集,你原来的代码只能显示一条记录。
3、其实使用数据库连接对象conn,记录集对象rs已经足够了,不需要cmd对象,我已经将cmd修改为字符串变量了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询