I wanted a code that would cycle thru the names of text box controls on a form whose Visible = true. I know this code is constructed incorrectly, but I need to ask if anyone can point me in the right direction.
Public Sub TxtBoxNamevisible(ByRef pfrm As Form)
Dim ctl As Control
Dim txtbx As TextBox
For Each txtbx In pfrm.Controls.visible = true
MsgBox txtbx.Name
Next txtbx
End Sub
pfrm.Controls.visible does not compile because Controls is a collection. Visible is a supported property for members of that collection, but not supported on the collection itself.
Loop through pfrm.Controls, check whether each of them is a Visible text box, and MsgBox the names of those which are ...
Dim ctl As Control
For Each ctl In pfrm.Controls
If ctl.ControlType = acTextBox And ctl.Visible = True Then
MsgBox ctl.Name
End If
Next ctl
Related
I have a form with a number of controls on it. However, if the user marks an option button as "Not Due", many of these fields are set to not be visible. Ultimately I don't want these hidden fields to store any value, but I don't want the values to disappear until the form is saved (that way if someone accidentally hits "not due", it doesn't erase all of the information). This is the code I have-- set to run on all controls before the form updates.
It's telling me that the "Object doesn't support this property or method" and highlights ctl.Value = null when I debug. Any idea what I can do to make this work?
Dim ctl As Control
If Me.optPayDue = 2 Then
For Each ctl In Me.Controls
If ctl.Visible = False Then ctl.Value = Null
Next ctl
End If
The reason you're getting this error is because things included in the Controls collections are like Subforms, Labels, EmptyCells, etc. which do not have a Value property.
You want to check that the thing for which you are setting the value has a Value property before acting on it.
Dim ValueControls As New Dictionary
ValueControls.Add "TextBox", "TextBox"
ValueControls.Add "CheckBox", "CheckBox"
ValueControls.Add "ComboBox", "ComboBox"
ValueControls.Add "ListBox", "ListBox"
ValueControls.Add "OptionButton", "OptionButton"
Dim ctl As Control
If Me.optPayDue = 2 Then
For Each ctl In Me.Controls
If Not ctl.Visible and ValueControls.Exists(TypeName(ctl)) Then
ctl.Value = Null
End If
Next ctl
End If
To use the Dictionary class you need to add a reference to Microsoft Scripting Runtime (C:\Windows\SysWOW64\scrrun.dll)
Another way to do this is to weed out any controls that don't have a .Value in your for loop. Assuming it is just textboxes you can do the following
Dim ctl As Control
If Me.optPayDue = 2 Then
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox and ctl.Visible = False Then
ctl.Value = Null
Next ctl
End If
For something like a checkbox the control type will be acCheckBox and the value would be set to 0.
I've looked all over, even scoured documentation, and I'm having trouble finding an answer to a seemingly simple problem.
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType <> acComboBox Then
If ctl.Name <> "SrchVal" Then
ctl.Enabled = False
End If
End If
Next ctl
I have a ton of textbox controls on my form which are being used for data entry. They should all be locked (disabled) until a certain button is clicked, an event I'll handle later.
However, there are a few combo box controls and one text box control I don't want disable, titled SrchVal.
I know that the Control object doesn't have an Enabled property, so how do I solve this?
Try the code below:
Dim ctl As Control
For Each ctl In Me.Controls
Debug.Print TypeName(ctl)
If TypeName(ctl) <> "ComboBox" Then ' <-- check if control is not a Combo-Box
Debug.Print ctl.Name
If ctl.Name <> "SrchVal" Then
ctl.Enabled = False
End If
End If
Next ctl
I have a sub that opens a report "eBouchTest". I loop through controls and change the controls' backcolors. I'm simplifying my question, eventually this will only happen based on a condition but for now I want every combobox, listbox or textbox colored grey.
When running the following piece of code, page 1 of my report does not have it's control's colors changed, but all the other pages do. Why is that so and how can I fix it? Clicking a button launches the following sub with no errors:
Sub ViewReport()
Dim formname As String
Dim ctl As Control
Dim frm As Report
formname = "eBouchTest"
Set frm = Application.Reports(formname)
DoCmd.openreport formname, acViewPreview
For Each ctl In frm.Controls
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox Then
ctl.BackColor = RGB(200, 200, 200)
Debug.Print ctl.Name & " " & ctl.BackColor
End If
Next ctl
Set ctl = Nothing
End Sub
Page 1 of the report (partial picture only for size purpose):
following pages:
How can I make page 1 change colors as well?
Use the code of formatting control in the detail_format event of report.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim formname As String
Dim ctl As Control
Dim frm As Report
formname = "eBouchTest"
Set frm = Application.Reports(formname)
For Each ctl In frm.Controls
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox Then
ctl.BackColor = RGB(200, 200, 200)
Debug.Print ctl.Name & " " & ctl.BackColor
End If
Next ctl
Set ctl = Nothing
End Sub
Also use a global variable to set the formatting of control dynamically.
AFIK, only way to effectively control dynamic appearance of controls on report with VBA is code in Format event of section the controls are in. Format events run only for Normal and Preview not Report view.
Can use custom Global variable or intrinsic OpenArgs to pass conditional value to the report. DoCmd.openreport formname, acViewPreview, , , , "some value"
Maybe Conditional Formatting would work for you. This will allow display in Report view. A CF rule can reference the OpenArgs parameter passed to report. Ex:
Can simultaneously select all controls desire to have this rule and set the condition for all in one action.
CF is really the only way to change control appearance between records because VBA code sets property for ALL instances of the control. So if the condition depends on data in each record, use CF.
The first page will also be coloured if I refresh it in one way or another. In my case, the reports are displayed in Landscape mode in print preview, so at the end of the code, I just add
frm.Printer.Orientation = acPRORLandscape
which really doesn't change anything, but for some reason it colors the fields in without adding or changing anything else to the code.
I am going over all control elemtens in my form and want to passt all ListBoxes to another function.
The Code looks like this:
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acListBox Then
Save (ctl)
End If
Next ctl
Private Sub Save(list As ListBox)
'Do something with list
End Sub
It gives me 424 Runtime Error, telling me that an object is needed.
When I try to use
Save (ctl.Object)
I'll get an 2455 Runtime Error.
EDIT: I also tryed casting the ctl Control Object to a ListBox Object with
Dim list As ListBox
Set list = ctl
Then it tells me the same as in my first example (Error 424)
Get rid of the brackets, so:
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acListBox Then
Save ctl
End If
Next ctl
VBA is very fussy about brackets.
I strongly recommend that you use something other than Save as the name of your function, it is very easy to disrupt VBA.
I would like to clear all the controls in an Access 2013 form. I found the following script on this site by Johan Godfried and it works very well.
Private Sub resetForm()
Dim ctl As Control
For Each ctl In Me.Controls
Select Case TypeName(ctl)
Case "TextBox"
ctl.value = ""
Case "CheckBox", "ToggleButton"
ctl.value = False
Case "OptionGroup"
ctl = Null
Case "OptionButton"
' Do not reset an optionbutton if it is part of an OptionGroup
If TypeName(ctl.Parent) <> "OptionGroup" Then ctl.value = False
Case "ComboBox", "ListBox"
ctl.RowSource = vbNullString
End Select
Next ctl
End Sub
Except that when the iteration selects a calculated control I receive the following error:
You can't assign a value to this object.
Is there a property that can be used to by-pass calculated controls?
try Me.myControl.controlsource="" to unbound that control
and then call resetForm()