设置在 VBA 中显示 Me.ComBox.Value 和 Me.ComboBox.RowSource 属性

Setting showing Me.ComBox.Value and Me.ComboBox.RowSource properties in VBA

我正在尝试在 Microsoft Access 中创建搜索表单。搜索表单将从客户端 table.
查找值 我在 ComboBox 的 OnUpdate 事件中有以下代码,它设置为在更新后自动更新表单数据。

Private Sub firstname_combo_Change()
Dim stringSQL As String
Dim RecordSt As Recordset
Dim dBase As Database
Dim strWhere As String
Dim varLname As Variant
Dim varClientID As Database
If Not IsNull(Me.firstname_combo.Column(1)) Then
    strWhere = "WHERE [Client_Data].[firstname]='" & Me.firstname_combo.Column(1) & "'"
    Me.lastname_cmbo.RowSource = "SELECT [Client_Data].[clientid], [Client_Data].[lastname] FROM Client_Data " & strWhere & ";"
    stringSQL = "SELECT TOP 1 [Client_Data].[lastname] FROM Client_Data " & varWhere & " ORDER BY [Client_Data].[lastname];"
    Set RecordSt = CurrentDb.OpenRecordset(stringSQL)
    RecordSt.MoveFirst
        'PLease note there would be multiple rows in the recordset but I need to select only the first row. However, varLname is populated correctly
    varClientID  = RecordSt.Fields("cleintid").Value
    varLname  = RecordSt.Fields("lastname").Value
    Me.lastname_cmbo.Value = varLname
         'MsgBox (varLname)
End If
End Sub

我在这里要做的是:

  1. OnChange of firstname, VBA 将查找所有匹配 firstnamelastnames 并将其显示为可用选项lastname 个字段。

  2. RecordSet 中取出第一行并填充 lastname_combo.value

Access 当前正确设置 RowSource 属性,但拒绝填充控件的 .Value 属性。所以我在 ComboBox 中得到一个空白 lastname(具有正确的 RowSource)。 请注意,每个 ComboBox 都已设置为 "Find a record on my From based on the value selected in my combo box".

看来我找到问题了。
我的 RowSource 台词是这样的

Me.lastname_cmbo.RowSource = "SELECT [Client_Data].[clientid], [Client_Data].[lastname] FROM Client_Data " & strWhere & ";"

现在这意味着访问正在使用 [Client_Data].[clientid] 字段作为 ComboBox

中每个元素的值

已替换

Me.lastname_cmbo.Value = varLname

Me.lastname_cmbo.Value = varClientID

现在它按预期工作了。