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

时间:2015-05-13 08:11:13

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

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

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

  2. RecordSet获取第一行,并将lastname_combo.value填入其中。

  3. Access目前正确设置RowSource属性,但拒绝填充控件的.Value属性。所以我在ComboBox中得到一个空白的lastname(使用正确的RowSource)。 请注意,每个ComboBox都已设置为"根据我在组合框中选择的值"在我的From上查找记录。

1 个答案:

答案 0 :(得分:0)

看起来我发现了问题 我的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

现在它按预期工作。