我用vb和access做个小系统,怎样通过查询一个字段把该条记录显示在界面的文本框里呢

2025-01-07 06:27:31
推荐回答(2个)
回答1:

Private Sub CX_Click()
Dim rs As New ADODB.Recordset
Dim strsql As String
Dim cn As New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb"
strsql = "select 学号,姓名,性别,院系,专业 from 学生基本信息表 where 学号= " & Text11.Text & " order by 学号"
rs.Open strsql, cn, adOpenStatic, adLockReadOnly, adCmdText
Text1.Text = "" & rs.Fields("学号")
Text2.Text = "" & rs.Fields("姓名")
Text6.Text = "" & rs.Fields("院系")
Text7.Text = "" & rs.Fields("专业")
Text8.Text = "" & rs.Fields("年级")
Text9.Text = "" & rs.Fields("班级")
rs.Close
End Sub

Private Sub Form_Load()
cmbCX.AddItem "按学号查询"
cmbCX.AddItem "按姓名查询"
End Sub

回答2:

Private Sub CX_Click()
Dim rs As New ADODB.Recordset
Dim strsql As String
Dim cn As New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb"
strsql = "select * from 学生基本信息表 where 学号='" & Text11.Text & "'"
rs.Open strsql, cn, adOpenStatic, adOpenKeyset, adLockOptimistic
Text1.Text = rs.Fields("学号")
Text2.Text = rs.Fields("姓名")
Text6.Text = rs.Fields("院系")
Text7.Text = rs.Fields("专业")
Text8.Text = rs.Fields("年级")
Text9.Text = rs.Fields("班级")
rs.Close
End Sub