如何在MS Access中动态引用控件名称 [英] How to reference control names dynamically in MS Access

查看:31
本文介绍了如何在MS Access中动态引用控件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MS Access窗体对象中有以下代码。

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找不到我引用的"Field"。我已尝试将.Controls更改为.Fields,但也不起作用。

我不想为我需要运行的每种语言重复相同的代码。如何在MS Access中动态引用控件名称?我错过了什么?

推荐答案

我认为您需要添加一些基本故障排除。答案可能比你想象的要简单。很可能您只是试图查找名称拼写错误的文本框,或者在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

这篇关于如何在MS Access中动态引用控件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆