使用MS-Access VBA从记录集中选择值

时间:2014-10-08 21:53:04

标签: vba ms-access dao recordset

我想为名称创建一个搜索文本框。我使用DAO记录集来获取与输入的名称类似的记录。当返回多个记录时,我想向用户显示结果并让他从列表中选择一个名称(记录)。

Dim dbs As DAO.Database
Dim Rs As DAO.Recordset
Dim strSQL As String
Dim SQL As String

Set dbs = CurrentDb()

Dim diverFirstName As String
diverFirstName = CStr(Me.txtSearchFirst) + "*"

strSQL = " SELECT tblDiver.* " & _
         " FROM tblDiver " & _
         " WHERE tblDiver.firstName LIKE '" & diverFirstName & "' ;"

Set Rs = dbs.OpenRecordset(strSQL, dbOpenDynaset)
Rs.MoveLast

If Rs.RecordCount > 1 Then
     *** code to display a table with the returned records so that the user can choose here ***
Exit Sub

我该如何处理?

1 个答案:

答案 0 :(得分:1)

您可以使用以下命令循环记录结果:

rs.moveFirst
do until rs.eof
    'add to a listbox or something
    me.listbox1.additem rs.fields("firstname").value & " " & rs.fields("lastname").value

    rs.moveNext
loop