访问:将值传递给使用VBA的参数查询?
问题描述:
我有一个参数查询(粉红色)的Access窗体。这里是我的形式:访问:将值传递给使用VBA的参数查询?
当用户选择一个物种,Access使用由选项组(称为“speciesSelection”)在表中查找该品种名称产生的号码,它的工作原理。但是,我想将物种名称传递给参数查询,以便记录集可以是突出显示的组合框(Combo12)的数据源。但是,当我选择一个物种时,组合框是空白的。这里是我的代码:
Private Sub speciesSelection_AfterUpdate()
Dim dbs As Database
Dim qdf As QueryDef
Dim rst As Recordset
Set dbs = CurrentDb
'Get species name of the current Cases instance'
Dim speciesChosen As String
speciesChosen = DLookup("Species", "tblSpeciesList", "ID=" & speciesSelection)
'Get the parameter query
Set qdf = dbs.QueryDefs("qryClinicalObservations")
'Supply the parameter value
qdf.Parameters("enterSpecies") = speciesChosen
'Open a Recordset based on the parameter query
Forms!inputForm.Controls!Combo12.RowSource = qdf.OpenRecordset()
End Sub
我使用向导创建了我的查询。这里是一个快照:
在条件部分,我可以提示(例如,“猫”)时手动输入一个物种,和它的工作原理。但不是用我的VBA代码...
是否有明显的错误?看来Combo12不被识别。
编辑:
这是我的新代码。实际上,Combo12是一个名为viewsSubform的子表单。这是我的代码和新形式。正如你所看到的,在下拉菜单中,但选项是不可见的:
Private Sub speciesSelection_AfterUpdate()
Dim dbs As Database
Dim qdf As QueryDef
Dim rst As Recordset
Set dbs = CurrentDb
'Get species name of the current Cases instance'
Dim speciesChosen As String
speciesChosen = DLookup("Species", "tblSpeciesList", "ID=" & speciesSelection)
MsgBox (speciesChosen)
'Get the parameter query
Set qdf = dbs.QueryDefs("qryClinicalObservations")
'Supply the parameter value
qdf!enterSpecies = speciesChosen
Set Me!observationsSubform!Combo12.Recordset = qdf.OpenRecordset()
答
组合框的RowSource
是一个字符串属性,所以你不能指定一个Recordset
对象它。改为将Recordset
分配给组合的Recordset
属性。
由于这是一个对象分配,请使用Set
关键字。
Set Forms!inputForm!Combo12.Recordset = qdf.OpenRecordset()
如果Combo12
和speciesSelection
都包含在相同的形式(inputForm
),你可以用这个来代替......
Set Me!Combo12.Recordset = qdf.OpenRecordset()
看起来你的组合框有2列,第一个无形,但是你的查询只返回一列,所以可见的组合框列是空的。 – Andre
@Andre它的工作!像上次! :) – Johnathan