你试试吧,好用的 记得要引用adoDim WithEvents adoPrimaryRS As Recordset '数据库连接对象 Private Sub Command1_Click() 'strsql 是你的查询语句 你可以order by 排序的字段 'strsql="select xh as 学号 from 表" 这里的学号就是你数据表中字段的标题 strsql="select 字段 from 表 where 字段='" & text1.text & "'" Set Db = New Connection Db.CursorLocation = adUseClient '下面的连接数据字符串你要修改一下 Db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\计划管理系统.mdb;Persist Security Info=False" Set adoPrimaryRS = New Recordset adoPrimaryRS.Open strsql, Db, adOpenStatic, adLockOptimistic Set DataGrid1.DataSource = adoPrimaryRS end sub
'引用ADO(Microsoft ActiveX Data Objects 2.X Library) Private Sub Command1_Click() On Error GoTo err Dim cn As New ADODB.Connection, rs 'SQL数据库 'cn.ConnectionString = "Provider = SQLOLEDB.1;Password = ; Persist Security Info = True;User ID = sa;Initial Catalog = 数据库; Data Source = yourServer" 'Access数据库 cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\数据库名.mdb;Persist Security Info=False" cn.Open rs.CursorLocation=adUseClient'设置客户端游标 rs.Open "select * from 表名 where 字段='" & Text1.Text & "'", cn, 3, 2 Set DataGrid1.DataSource = rs Exit Sub err: MsgBox err.Description End Sub
Private Sub Command1_Click() Dim cboField As String, cboOperator As String, txtValue As String 'cbo为字段名,cboOperator为运算符,txtValue为值cboField = Combo1.Text cboOperator = Combo1.Text txtValue = Trim(Text1.Text)Dim cn As New ADODB.Connection, rs As New ADODB.Recordset cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=S:\A30\YS0951\VB\批号系统.mdb" cn.Open rs.CursorLocation = adUseClient rs.Open "select * from 用户登记表 where cboField & cboOperator& '"txtValue"'",cn,3,2 Set DataGrid1.DataSource = rs End Sub 我这段程序要求的功能是根据用户选择字段,再选择运算符,再输入字段值,然后点击查询 datagrid1就显示查询结果,该如何写程序?
记得要引用adoDim WithEvents adoPrimaryRS As Recordset '数据库连接对象
Private Sub Command1_Click()
'strsql 是你的查询语句 你可以order by 排序的字段
'strsql="select xh as 学号 from 表" 这里的学号就是你数据表中字段的标题
strsql="select 字段 from 表 where 字段='" & text1.text & "'"
Set Db = New Connection
Db.CursorLocation = adUseClient
'下面的连接数据字符串你要修改一下
Db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\计划管理系统.mdb;Persist Security Info=False"
Set adoPrimaryRS = New Recordset
adoPrimaryRS.Open strsql, Db, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = adoPrimaryRS
end sub
Private Sub Command1_Click()
On Error GoTo err
Dim cn As New ADODB.Connection, rs
'SQL数据库
'cn.ConnectionString = "Provider = SQLOLEDB.1;Password = ; Persist Security Info = True;User ID = sa;Initial Catalog = 数据库; Data Source = yourServer"
'Access数据库
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\数据库名.mdb;Persist Security Info=False"
cn.Open
rs.CursorLocation=adUseClient'设置客户端游标
rs.Open "select * from 表名 where 字段='" & Text1.Text & "'", cn, 3, 2
Set DataGrid1.DataSource = rs
Exit Sub
err:
MsgBox err.Description
End Sub
Dim cboField As String, cboOperator As String, txtValue As String
'cbo为字段名,cboOperator为运算符,txtValue为值cboField = Combo1.Text
cboOperator = Combo1.Text
txtValue = Trim(Text1.Text)Dim cn As New ADODB.Connection, rs As New ADODB.Recordset
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=S:\A30\YS0951\VB\批号系统.mdb"
cn.Open
rs.CursorLocation = adUseClient
rs.Open "select * from 用户登记表 where cboField & cboOperator& '"txtValue"'",cn,3,2
Set DataGrid1.DataSource = rs
End Sub
我这段程序要求的功能是根据用户选择字段,再选择运算符,再输入字段值,然后点击查询
datagrid1就显示查询结果,该如何写程序?