0

我有一个参数查询(粉红色)的Access窗体。这里是我的形式:访问:将值传递给使用VBA的参数查询?

enter image description here

当用户选择一个物种,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 

我使用向导创建了我的查询。这里是一个快照:

enter image description here

在条件部分,我可以提示(例如,“猫”)时手动输入一个物种,和它的工作原理。但不是用我的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() 

enter image description here

+0

看起来你的组合框有2列,第一个无形,但是你的查询只返回一列,所以可见的组合框列是空的。 – Andre

+0

@Andre它的工作!像上次! :) – Johnathan

回答

0

组合框的RowSource是一个字符串属性,所以你不能指定一个Recordset对象它。改为将Recordset分配给组合的Recordset属性。

由于这是一个对象分配,请使用Set关键字。

Set Forms!inputForm!Combo12.Recordset = qdf.OpenRecordset() 

如果Combo12speciesSelection都包含在相同的形式(inputForm),你可以用这个来代替......

Set Me!Combo12.Recordset = qdf.OpenRecordset() 
+0

非常感谢您的回答!我刚刚意识到组合12在一个名为viewsSubform的子表单中。 Combo12现在可以识别,下拉菜单可以工作,但我看不到任何条目。那么,我几乎在那里?你能看看编辑?谢谢! :) – Johnathan

+0

你是否检查过'qdf.OpenRecordset()'是否返回任何行? – HansUp

+0

其实,它工作得很好,它只是一个列编辑问题! :)现在,它完美的工作!再一次非常感谢你! – Johnathan