0

我正在制作一个 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
4

1 回答 1

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
于 2013-08-15T10:10:57.943 回答