vba 循环访问访问表单上的文本框并更改数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16317200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loop through textboxes on access form and change the data
提问by user1643333
I am trying to change data when an access form opens. At the minute data says True and I would like to change that to say Yes instead. I have given it a go but I am new to vba and don't really know what I am doing. I have given all the textboxes the same Tag of 'QtTxtBox' hoping this would help but it hasn't. Below is what I've got so far, can anyone help me?
我试图在访问表单打开时更改数据。此刻数据显示为 True,我想将其更改为 Yes。我已经试过了,但我是 vba 新手,我真的不知道我在做什么。我给所有的文本框都加上了“QtTxtBox”的相同标签,希望这会有所帮助,但没有。以下是我到目前为止所得到的,有人可以帮助我吗?
Dim ctlVar As Control
For Each ctlVar In Me.Controls
If ctlVar.ControlType = acTextBox Then
If acTextBox.text = "True" Then
acTextBox.text = "yes"
End If
End If
回答by Tom Collins
Your problem is in your textbox reference. acTextBox is not a textbox. It's just a value showing that a control type is a textbox.
So, when you say 'If ctlVar.ControlType = acTextBox Then', that is correct.
But when you say 'If acTextBox.text = "True" Then', that is incorrect. You're not referencing the control any longer. It should be 'If ctlVar.text = "True" Then'. ctlVar is your reference to the control.
您的问题出在您的文本框参考中。acTextBox 不是文本框。它只是显示控件类型是文本框的值。
所以,当你说' If ctlVar.ControlType = acTextBox Then'时,这是正确的。
但是当你说' If acTextBox.text = "True" Then'时,那是不正确的。您不再引用控件。它应该是 ' If ctlVar.text = "True" Then'。ctlVar 是您对控件的引用。
Also, you need to set the focus onto the textbox before changing the value. Here's what your code should look like:
此外,您需要在更改值之前将焦点设置在文本框上。您的代码应如下所示:
For Each ctlVar In Me.Controls
If ctlVar.ControlType = acTextBox Then
If ctlVar.Value = "True" Then
ctlVar.Value = "yes"
End If
End If
回答by Gary Chernipeski
there are a few things packed into this little chunk of code -- perhaps even your issue -- after two days of figuring this stuff out...
经过两天的研究,这一小块代码中包含了一些东西——甚至可能是你的问题——
Function disorder()
For c = 0 To 9
s = Screen.ActiveForm.Controls("" & "[" & c & "]" & "").Caption
r = Int(9 * Rnd)
Screen.ActiveForm.Controls("" & "[" & c & "]" & "").Caption = Screen.ActiveForm.Controls("" & "[" & r & "]" & "").Caption
Screen.ActiveForm.Controls("" & "[" & r & "]" & "").Caption = s
Next
End Function
To make sure focus is on the form I want I used SendKeys "{ENTER}" which is picked up by a default button on the form. In this case I have several controls named [1] and [2] etc to make it easier to loop through them but they could be named anything.
为了确保焦点在我想要的表单上,我使用了 SendKeys "{ENTER}",它由表单上的默认按钮拾取。在这种情况下,我有几个名为 [1] 和 [2] 等的控件,以便更轻松地遍历它们,但它们可以命名为任何名称。
to loop through all the controls on a form...
遍历表单上的所有控件...
For Each c In Screen.ActiveForm.Controls
With c
If Caption = "" Then Caption = "New"
End With
Next
回答by Katana24
So - when you constructed the form you set the textbox caption to say True - correct? Well if you want to change that when the form loads then do the following:
所以 - 当你构建表单时,你将文本框标题设置为 True - 正确吗?好吧,如果您想在表单加载时更改它,请执行以下操作:
Private Sub Form_Load()
acTextBox.Caption = "Yes"
End Sub