我在MS Access Form对象中有以下代码。
Private Sub UpdatePMText(sLang As String)
'Used to pass both Mandate and Language Info to called Sub that will execute the queries
Dim iMandate As Integer
'Check if there is text in the box.
If Me.Controls("txtInput_PM_" & sLang & "_DRAFT") = Null Or Me.Controls("txtInput_PM_" & sLang & "_DRAFT") = "" Then
MsgBox ("There is no text in the " & sLang & " DRAFT Field." & vbNewLine & "The operation will not be executed.")
Exit Sub
End If
iMandate = Me.txtMandateID
Call modUpdateText.macro_PMText(iMandate, sLang)
End Sub
如果我直接引用控件并简单地输入表单的名称,例如txtInput_PM_EN_DRAFT
,则代码按预期工作。
我得到的错误消息是,当我在第一个IF
语句行时,Access无法找到我所指的“字段”。我尝试将.Controls
更改为.Fields
,但这也无效。
我不想为我需要运行的每种语言重复相同的代码。如何在MS Access中动态引用控件名称?我错过了什么?
答案 0 :(得分:5)
我认为您需要添加一些基本的故障排除。答案可能比你想象的要简单。您可能只是尝试查找带有错误名称的文本框,或者它在Null比较中失败(如@HansUp建议的那样)
我会尝试修改你的基本子并用这个子程序测试它。只要您的代码处于当前形式且您没有引用子表单,您的方法就会起作用。
Private Sub UpdatePMText(sLang As String)
Const ERR_MISSING_CONTROL As Long = 2465
On Error GoTo Err_UpdatePMText
Dim sTextBox As String
sTextBox = "txtInput_PM_" & sLang & "_DRAFT"
'Check if there is text in the box.
If Nz(Me.Controls(sTextBox).Value, "") = "" Then
MsgBox ("There is no text in the " & sLang & " DRAFT Field." & vbNewLine & "The operation will not be executed.")
Exit Sub
End If
Exit Sub
Err_UpdatePMText:
If Err.Number = ERR_MISSING_CONTROL Then
MsgBox "Error: No Such Textbox: " & sTextBox
Else
MsgBox "Error Looking Up Textbox: """ & sTextBox & """" & vbCrLf & Err.Description
End If
End Sub