我正在制作一个VB6程序,我想从数据库中获取信息。
但我只需要表格中一行的信息
目前我将此作为我的查询
SELECT CertNo FROM STICKER
这会在列表框中填入CertNo中的所有项目, 有没有办法让信息依赖于变量,比如
SELECT CertNo FROM STICKER WHERE CertNo = 10000
它将从CertNo = 10000
的行中获取所有列获取数据的代码:
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Datalist.Clear
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Users\User\Desktop\test\dbAdd.mdb;"
Set rs = New ADODB.Recordset
rs.Open "SELECT CertNo FROM STICKER WHERE CertNo = '10303';", cn, adOpenStatic, adLockOptimistic
rs.MoveFirst
While Not rs.EOF
Datalist.AddItem rs.Fields(0)
rs.MoveNext
Wend
Set rs = Nothing
Set cn = Nothing
答案 0 :(得分:2)
如果您需要获取Data1
rs.Open "SELECT CertNo FROM STICKER WHERE CertNo = '"& Data1 &"';", cn, adOpenStatic, adLockOptimistic
如果你想从sql返回多个列,请执行以下操作
rs.Open "SELECT CertNo, IssueDate FROM STICKER WHERE CertNo = '"& Data1 &"';", cn, adOpenStatic, adLockOptimistic
并在循环中
While Not rs.EOF
Datalist.AddItem rs.Fields(0) & rs.Fields(1)
rs.MoveNext
Wend