Access 2007记录集未自动填入文本框

时间:2015-03-13 23:19:27

标签: vba ms-access access-vba ms-access-2007

我有一个里面有一个程序的模块,如下所示:

Public Sub OpenRecordset()

Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs("QOff2")
qdf.Parameters(0).Value = [Forms]![Form]![Text10]

Dim db As Database
Dim rs As Recordset
Dim StrBusinesses As String

Set rs = qdf.OpenRecordset
If rs.EOF And rs.BOF Then
MsgBox ("No businesses exist for this Customer")
Exit Sub
Else
rs.MoveFirst
End If
StrBusinesses = ""
Do While Not rs.EOF
StrBusinesses = StrBusinesses & rs!Fnam & ", "
rs.MoveNext
Loop

rs.Close
StrBusinesses = Left(StrBusinesses, Len(StrBusinesses) - 2)
Forms!Form.Badge = StrBusinesses
Set rs = Nothing



End Sub

我正在尝试让这个模块将查询结果输入到一个文本框(forms!form.badge)中,但我似乎无法像其他5个dlookup函数那样使用它。当我打开模块并按下绿色播放按钮时,它会显示在正确的文本框上,但也会显示在其他记录上。它也不会自动显示,也不会在您输入参数时更新。是不是一个模块应该帮助自动将大量变量自动填充到文本框中代替dlookup以获取多个值?

1 个答案:

答案 0 :(得分:0)

没有。如果Forms!Form!Badge是未绑定的文本框,则为所有记录显示分配给它的值。 要个性化,您需要一个查找函数,它将记录的ID或其他唯一值作为参数(添加到文本框):

=LookupBadges([Forms]![Form]![Text10])


Public Function LookupBadges(ByVal Value As Variant) As Variant

    Dim db As Database
    Dim qd As QueryDef    
    Dim rs As Recordset

    Dim Businesses As String

    Set db = CurrentDb
    Set qd = db.QueryDefs("QOff2")

    qd.Parameters(0).Value = Nz(Value)
    Set rs = qd.OpenRecordset
    If rs.RecordCount > 0 Then
        rs.MoveFirst
        Do While Not rs.EOF
            Businesses = Businesses & rs!Fnam.Value & ", "
            rs.MoveNext
        Loop
    End If
    rs.Close

    Businesses = Left(Businesses, Len(Businesses) - 2)
    LookupBadges = Businesses

    Set rs = Nothing
    Set qd = Nothing
    Set db = Nothing

End Function