0

我对 VBA 和 MS Access 完全陌生!

  • 我在一个表单中有一个子表单,其中包含所有具有“设备 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,但我不知道为什么尽管谷歌搜索。我觉得它应该很简单,但我知道我可能必须做一些完全不同的事情!任何意见,将不胜感激 :)
4

0 回答 0