设置显示me.combox.value和me.com.bobox.rowsource属性在VBA中



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

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. firstnameOnChange,VBA将寻找与firstname匹配的所有lastnames,并在lastname字段中显示为可用选项。

  2. 将第一行从RecordSet拿出来,然后将lastname_combo.value填充到它。

访问当前正确设置了RowSource属性,但拒绝填充控件的.Value属性。因此,我在COMBOBOX中获得了空白的lastname(带有正确的行扣)。请注意,每个组合都设置为"根据我的组合框中选择的值在我的来自我的记录上找到记录"。

看起来我发现了问题。
我的 RowSource行就像这样

me.lastname_cmbo.rowsource =" select [client_data]。[clientid],[client_data]。[lastName]来自client_data"&strwhere&strwhere&";"

现在,这意味着访问使用[Client_Data].[clientid]字段作为ComboBox中每个元素的值

替换

me.lastname_cmbo.value = varlname 
 me.lastname_cmbo.value = varclientid 

现在它正常工作。

最新更新