Access 2016 VBA 文本框为空
Access 2016 VBA TextBox is Null
我很久没有使用 VBA....我在 Access 2016 中有这个表格
当我尝试通过 Me.Controls 集合访问各种 TextBox 并将其转换为 TextBox 对象时,我得到一个 Null 引用 但它的某些属性是有效的(例如 tb.Name)
Private Sub Form_Load()
Dim ctrl As Control
Dim tb As TextBox
Dim evTb As clsEventTextBox
Set m_TbColl = New Collection
For Each ctrl In Me.Controls
If Left$(ctrl.Name, 4) = "Txt_" Then
Set tb = ctrl
'Create the TextBox wrapper
Set evTb = New clsEventTextBox
Set evTb.EventsHandler = Me
Set evTb.InnerTextBox = tb <----- HERE tb Is NULL
m_TbColl.Add evTb, ctrl.Name
End If
Next
End Sub
我想念什么?
另外,有没有办法获取控件的类型而不是使用
Left$(ctrl.Name, 4) = "Txt_"
要获取类型,请像这样使用 TypeName
:
If TypeName(ctrl) = "TextBox" Then
并确保 tb
采用 Textbox
对象的形式,使用此
Set tb = Controls(ctrl.Name)
您没有显示您正在使用的 class,但假设它看起来像这样:
Private WithEvents f_EH As Access.Form
Private WithEvents f_TB As Access.TextBox
Public Property Set EventsHandler(frm As Access.Form)
Set f_EH = frm
End Property
Public Property Set InnerTextBox(ctl As Access.TextBox)
Set f_TB = ctl
End Property
如果我使用具有该结构的 class,则您的 post 中的代码可以正常工作。但是请注意,我已经明确地将 InnerTextBox
属性 的预期类型设置为 Access.TextBox
.
但是您的代码进行了不必要的强制转换,使用了匈牙利语命名(糟糕!),并且依赖于名称的前 4 个字符 "Txt_" 并且可以写成:
Dim ctrl As Control
Dim evTb As clsEventTextBox
Set m_TbColl = New Collection
For Each ctrl In Me.Controls
If TypeOf ctrl Is Access.TextBox Then
'Create the TextBox wrapper
Set evTb = New clsEventTextBox
Set evTb.EventsHandler = Me
Set evTb.InnerTextBox = ctrl 'Just pass the ctrl reference without casting
m_TbColl.Add evTb, ctrl.Name
End If
Next
注意在If TypeOf ctrl Is Access.TextBox Then
中使用TypeOf
判断控件是否为TextBox
.
我很久没有使用 VBA....我在 Access 2016 中有这个表格
当我尝试通过 Me.Controls 集合访问各种 TextBox 并将其转换为 TextBox 对象时,我得到一个 Null 引用 但它的某些属性是有效的(例如 tb.Name)
Private Sub Form_Load()
Dim ctrl As Control
Dim tb As TextBox
Dim evTb As clsEventTextBox
Set m_TbColl = New Collection
For Each ctrl In Me.Controls
If Left$(ctrl.Name, 4) = "Txt_" Then
Set tb = ctrl
'Create the TextBox wrapper
Set evTb = New clsEventTextBox
Set evTb.EventsHandler = Me
Set evTb.InnerTextBox = tb <----- HERE tb Is NULL
m_TbColl.Add evTb, ctrl.Name
End If
Next
End Sub
我想念什么?
另外,有没有办法获取控件的类型而不是使用
Left$(ctrl.Name, 4) = "Txt_"
要获取类型,请像这样使用 TypeName
:
If TypeName(ctrl) = "TextBox" Then
并确保 tb
采用 Textbox
对象的形式,使用此
Set tb = Controls(ctrl.Name)
您没有显示您正在使用的 class,但假设它看起来像这样:
Private WithEvents f_EH As Access.Form
Private WithEvents f_TB As Access.TextBox
Public Property Set EventsHandler(frm As Access.Form)
Set f_EH = frm
End Property
Public Property Set InnerTextBox(ctl As Access.TextBox)
Set f_TB = ctl
End Property
如果我使用具有该结构的 class,则您的 post 中的代码可以正常工作。但是请注意,我已经明确地将 InnerTextBox
属性 的预期类型设置为 Access.TextBox
.
但是您的代码进行了不必要的强制转换,使用了匈牙利语命名(糟糕!),并且依赖于名称的前 4 个字符 "Txt_" 并且可以写成:
Dim ctrl As Control
Dim evTb As clsEventTextBox
Set m_TbColl = New Collection
For Each ctrl In Me.Controls
If TypeOf ctrl Is Access.TextBox Then
'Create the TextBox wrapper
Set evTb = New clsEventTextBox
Set evTb.EventsHandler = Me
Set evTb.InnerTextBox = ctrl 'Just pass the ctrl reference without casting
m_TbColl.Add evTb, ctrl.Name
End If
Next
注意在If TypeOf ctrl Is Access.TextBox Then
中使用TypeOf
判断控件是否为TextBox
.