Ms access 如何在Access中将组合框作为参数传递?

Ms access 如何在Access中将组合框作为参数传递?,ms-access,Ms Access,我在几个组合框的几个事件上有以下示例代码: Private Sub cbo_oc_tours_all_Enter() ' Find the record that matches the control. Dim rs As Object Set rs = Me.Recordset.Clone rs.FindFirst "[ID_T_OC] = " & Str(Nz(Me![cbo_oc_tours_all], Null)) If Not rs.EO

我在几个组合框的几个事件上有以下示例代码:

Private Sub cbo_oc_tours_all_Enter()
' Find the record that matches the control.
Dim rs As Object

Set rs = Me.Recordset.Clone
    rs.FindFirst "[ID_T_OC] = " & Str(Nz(Me![cbo_oc_tours_all], Null))
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark
    
Me.cbo_oc_tours_today.Value = Str(Nz(Me![cbo_oc_tours_all], Null))

End Sub
我想把它放到一个函数中,并从每个事件过程(?)调用它。我在函数中使用了以下代码:

Sub goto_record(goto_rec As ComboBox, update_rec As ComboBox)

Dim rs As Object

Set rs = Me.Recordset.Clone
    rs.FindFirst "[ID_T_OC] = " & Str(Nz(Me![goto_rec], Null))
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark
    
update_rec.Value = Str(Nz(Me![goto_rec], ""))

End Sub
我试着用以下方式来称呼它:

Sub cbo_oc_tours_all_Enter()

Call goto_record(cbo_oc_tours_all, cbo_oc_tours_today)
    
End Sub

但是,我收到一个错误消息,Microsoft Access找不到表达式中引用的字段“goto_rec”。我显然没有以正确的方式传递组合框,但我已经有一段时间没有使用VBA了。我的问题是什么?

不要将组合框作为对象传递,只需在组合框中传递值即可。以下是一个例子:

Sub cbo_oc_tours_all_Enter()

Call goto_record(me.cbo_oc_tours_all, me.cbo_oc_tours_today)

End Sub
假设这些是组合框本身的名称。那么你的另一个功能就是

'Using a data type of variant for example. Replace with the datatype you are using
Sub goto_record(goto_rec As Variant, update_rec As Variant)

Dim rs As Recordset

Set rs = Me.Recordset.Clone
   rs.FindFirst "[ID_T_OC] = " & Str(Nz(goto_rec,0))
   If Not rs.EOF Then Me.Bookmark = rs.Bookmark

'You can change this line to this if the function is in the same module as the form with the combobox
me.cbo_oc_tours_today = goto_rec

End Sub

因为您直接传递控件,所以只需访问它的值。请尝试:
rs.FindFirst“[ID\u T\u OC]=”&Nz([goto\u rec].Value,vbNullString)
。您还应该检查
.NoMatch
属性,而不是
.EOF
。只传递值,而不是传递combobox对象@这看起来是一个你可以写出来的有效答案。