我正在尝试在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
我在这里要做的是:
OnChange
firstname
,VBA会查找与lastnames
匹配的所有firstname
,并将其显示为lastname
字段中的可用选项。
从RecordSet
获取第一行,并将lastname_combo.value
填入其中。
Access目前正确设置RowSource
属性,但拒绝填充控件的.Value
属性。所以我在ComboBox中得到一个空白的lastname
(使用正确的RowSource)。
请注意,每个ComboBox都已设置为"根据我在组合框中选择的值"在我的From上查找记录。
答案 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
现在它按预期工作。