MS Access VBA:从查询中获取数据,该数据与子表单表中所选行的ID字段相对应

问题描述

我对VBA和MS Access完全陌生!

  • 我在表单中有一个子表单,其中包含有关所有具有“设备ID”字段的项目的数据。
  • 使用来自不同表的数据,我有一个查询来计算与每个项目关联的“故障”的数量。此查询还具有“设备ID”字段。
  • 我还有第二个表单,其中包含文本框,可以通过按钮进行访问。单击按钮后,将打开该表单,并将子表单中所选当前行中的数据传递到文本框中。我已经将原始数据表分配给了表单控制源。

我想做什么:

  • 我在查询时遇到问题。我一直在尝试链接子表单和查询,以便在选择子表单上的行时,从中获取“设备ID”,并可以从其对应的查询中找到“故障数量” “设备ID”。

这是到目前为止的代码(以及我想在注释中发生的事情):

Private Sub Button_Click()  

 Dim db As Database 
 Dim rs As DAO.Recordset 
 Dim EqIDfrm As String  'Equipment ID from the subform    
 
 'Open up the Faults Query    
 Set db = CurrentDb
 Set rs = db.OpenRecordset("qryFaultTotal") 

 'open up the second form  
 DoCmd.OpenForm "SecondForm"    

 'Grab the "Year" from the selected row in the sub form and assign it to SelectYear. The text   
 'box value in the second form then becomes the SelectYear value.      
 SelectYear = [Forms]![Form1]![tblAsset Subform].[Form]![Year]  
 [Forms]![Form2]![txtYear] = SelectYear    
 ' etc. for other fields  

'grab the equipment ID from the subform and from the Query  
EqIDfrm = [Forms]![Form1]![tblAsset Subform].[Form]![Equipment_ID]      
EqIDqry = rs![Equipment_ID] 
'<--- this seems to be part of the problem  

'Lookup the Total number of faults from the FaultTotal Query,where the Equipment ID in the Query is   
 the same as the equipment ID on the subform from the selected row.   
 
Fault = DLookup("[TotalFault]","[FaultTotal]","[EqIDqry] = " & [EqIDfrm] & "")  


End Sub
  • 使用查找时,我总是收到某种错误(类型不匹配,运行时错误),但我无法弄清。它似乎不喜欢EqIDqry,但尽管使用Google搜索,但我不知道为什么。我觉得应该很简单,但是我知道我可能必须做一些完全不同的事情!任何建议,将不胜感激:)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...