[英]How to loop through all controls in a form, including controls in a subform - Access 2013
I found this code on this site and, by description, it is exactly what I need (plus I'll need to expand it to subforms) but I can't get it to work.我在这个网站上找到了这段代码,根据描述,它正是我所需要的(另外我需要将它扩展到子表单),但我无法让它工作。 I tried it in the Form's VBA area and in a separate module.
我在 Form 的 VBA 区域和一个单独的模块中尝试了它。 I also changed 'fmr As Form' to 'frm as Form_Submissions' to specify which form.
我还将“fmr As Form”更改为“frm as Form_Submissions”以指定哪个表单。
I have a command button with click event code 'Application.Run "colCtrlReq"' and the colCtrlReq code saved in a module.我有一个带有单击事件代码“Application.Run“colCtrlReq””和保存在模块中的 colCtrlReq 代码的命令按钮。 The 'frm As Form' prevents the code from being recognized/found so I get error 449/argument not optional.
'frm As Form' 阻止代码被识别/找到,所以我得到错误 449/argument not optional。
I tried removing 'Option Compare Database' but nope.我尝试删除“选项比较数据库”,但没有。 I tried removing 'frm As Form' from the sub name and adding 'Dim frm As Form' to the code but then get other errors.
我尝试从子名称中删除“frm As Form”并将“Dim frm As Form”添加到代码中,但随后出现其他错误。 (I'm new to Access so grasping at straws.)
(我是 Access 的新手,所以抓住了稻草。)
Other Errors:其他错误:
-on acTextBox.Value I get a Compile error/invalid qualifier for acTextBox -on acTextBox.Value 我收到 acTextBox 的编译错误/无效限定符
-on For Each ctl In frm.Controls I get run-time error 91/object variable or with variable not set (probably cuz I removed 'frm As Form' from the name) -on For Each ctl 在 frm.Controls 中,我收到运行时错误 91/object 变量或未设置变量(可能是因为我从名称中删除了“frm As Form”)
My goal is this:我的目标是这样的:
-If it's a text box or combo box, required and blank = change background color - 如果是文本框或组合框,必填和空白 = 更改背景颜色
-If it's a check box or option button, required and false = change background color - 如果是复选框或选项按钮,则为必填和 false = 更改背景颜色
Any help getting this to work and expanding it to subforms would be greatly appreciated!任何帮助使其工作并将其扩展到子表单的帮助将不胜感激!
Option Compare Database选项比较数据库
Public Sub colCtrlReq(frm As Form)
' Sets background color for required field -> Tag = *
Dim setColor As String
setColor = RGB(255, 244, 164)
Dim ctl As Control
For Each ctl In frm.Controls
'If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox Then
If ctl.ControlType = acTextBox And acTextBox.Value = "" And InStr(1, ctl.Tag, "*") <> 0 Then
ctl.BackColor = setColor
End If
Next ctl
Set ctl = Nothing
End Sub
Sub Validate()
Dim ctrl As Control
Dim l As Control
setColor = RGB(255, 0, 0)
foundrequired = 0
For Each ctrl In Form_Submissions.Controls
If InStr(1, ctrl.Tag, "*") <> 0 Then
If ctrl.ControlType = acTextBox Or ctrl.ControlType = acComboBox Then
If Nz(ctrl.Value, vbNullString) = "" Then
ctrl.BackColor = setColor
foundrequired = foundrequired + 1
Else
ctrl.BackColor = 16777215
End If
End If
If ctrl.ControlType = acCheckBox Or ctrl.ControlType = acOptionButton Then
If Nz(ctrl.Value) = 0 Then
For Each l In Form_Submissions.Controls
If l.ControlType = acLabel And Right(l.Name, 3) = Right(ctrl.Name, 3) Then
l.BackColor = setColor
foundrequired = foundrequired + 1
End If
Next l
Else
For Each l In Form_Submissions.Controls
If l.ControlType = acLabel And Right(l.Name, 3) = Right(ctrl.Name, 3) Then
l.BackColor = 16777215
End If
Next l
End If
End If
End If
Next ctrl
If foundrequired > 0 Then
MsgBox "Please fill in the required fields before submitting.", vbCritical, "HOTEL CHECKS"
Exit Sub
End If
'continue code if passes validation
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.