1

我有一个带有 project_ID 字段组合框和其他几个字段的 MS Access 表单。一旦用户选择了 project_ID 字段,表单上的大部分后续字段都会自动填充。我正在尝试在表单上添加一个字段,该字段不仅基于 project_ID 还基于 Trans_ID 显示信息。问题是我希望 Trans_ID 成为表单上的一个文本框,用户可以在其中简单地输入 Trans_ID,然后在另一个文本框中显示 Error_DTL_1 字段。这是我到目前为止生成的 VBA 代码:

Private Sub cboProjectID_Change()

Dim VarComboKey As Integer 
Dim VarObjective As Variant 
Dim VarStartDate As Variant 
Dim VarEndDate As Variant 
Dim VarRiskCategory As Variant 
Dim VarTarDatSet As Variant

Dim VarErrorCount As Variant 
Dim VarErrorCode As Variant

Dim VarErrorDTL As Variant

VarComboKey = Me.cboProjectID.Value

VarObjective = DLookup("[Objective]", "[Project_HDR_T]", "[Project_ID]= " & VarComboKey) 
Me.txtObjective = VarObjective

VarStartDate = DLookup("[Start_Date]", "[Project_HDR_T]", "[Project_ID] = " & VarComboKey) 
Me.txtStartDate = VarStartDate

VarEndDate = DLookup("[End_Date]", "[Project_HDR_T]", "[Project_ID] = " & VarComboKey) 
Me.txtEndDate = VarEndDate

VarRiskCategory = DLookup("[Risk_Category]", "[Project_HDR_T]", "[Project_ID] = " & VarComboKey) 
Me.txtRiskCategory = VarRiskCategory

VartxtTarDatSet = DLookup("[Targeted_Dataset]", "[Project_Targeted_Dataset]", "[Project_ID] = " & VarComboKey)
Me.txtTarDatSet = VartxtTarDatSet

VarErrorCount = DLookup("[Count_Error_Codes]", "[Project_Error_Final]", "[project_ID] = " & VarComboKey)
Me.txtErrorCount = VarErrorCount

VarErrorCode = DLookup("[ErrorCode]", "[Project_Error_Final]", "[project_ID] = " & VarComboKey) 
Me.txtErrorCode = VarErrorCode

VarErrorDTL = DLookup("[Error_DTL_1]", "[Project_DTA_REV_T]", "[project_ID] = " & VarComboKey And "[Trans_ID] = forms![Quality Risk Assessment]!me.stTransID") 
Me.txtErrorDTL = VarErrorDTL

End Sub  

“End Sub”之前的两行是我攻击这段代码的尝试。但是每次我在表单上的 Project_ID 组合框中进行选择时,都会收到错误消息“运行时错误 13,类型不匹配”。

任何人都可以帮忙吗?

4

2 回答 2

3

一个记录集:

Dim rs As DAO.Recordset

sSQL = "SELECT p.Objective, p.Start_Date, p.End_Date FROM Project_HDR_T p " _
     & "WHERE p.Project_ID = " & VarComboKey
Set rs = CurrentDb.OpenRecordset(sSQL)

If rs.EOF Then
    MsgBox "oops"
Else
    VarObjective = rs!Objective
    VarStartDate = rs!Start_Date
    VarEndDate = rs!End_Date
End If

并且鉴于您的所有表都包含 Project_ID,应该可以创建一个包含所有表的查询,此外,可以保存查询并在代码中使用参数引用。

另请参阅:
什么是 VBA 中的记录集?...它有什么用途?
记录集对象

于 2013-03-14T20:08:18.443 回答
3

在行...

VarErrorDTL = DLookup("[Error_DTL_1]", "[Project_DTA_REV_T]", "[project_ID] = " & VarComboKey And "[Trans_ID] = forms![Quality Risk Assessment]!me.stTransID") 

...“和”在引号之外,第二个子句似乎混合了引用方式Forms!me.引用方式。尝试...

VarErrorDTL = DLookup("[Error_DTL_1]", "[Project_DTA_REV_T]", "[project_ID] = " & VarComboKey & " And [Trans_ID] = forms![Quality Risk Assessment]!stTransID.Value") 

...看看它是否效果更好。或者,您可以尝试...

VarErrorDTL = DLookup("[Error_DTL_1]", "[Project_DTA_REV_T]", "[project_ID] = " & VarComboKey & " And [Trans_ID] = " & me.stTransID.Value) 
于 2013-03-14T19:26:59.900 回答