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.
Related
I have a form with textboxes for adding new Event to the database. Some of the textboxes are mandatory while the rest not. Within this also is a subform for adding details of participants.
I don't want the subform to be visible if one or more of the mandatory fields have nothing. I have tried with this but the subform stays invisible regardless whether or not there is data in the mandatory fields:
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If Not (IsNull(ctl.Value) Or ctl.Value = "") Then
Me.subfrmParticipants.Visible = False
Else
Me.subfrmParticipants.Visible = True
End If
End If
Next ctl
You're using a double negative instead of just checking for NULL value in your text box.
Change this
If Not (IsNull(ctl.Value) Or ctl.Value = "") Then
To This:
If (IsNull(ctl.Value) Or ctl.Value = "") Then
Then if the text box is assigned a database NULL value or is empty,
then the hide subform command will kick in
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 huge form in access which needs to prevent users from editing the details in it.. I have done that by setting Me.Allowedits to false. But there is one field which I need to keep as open to editing. To do this I have jus placed the code to open the field after the allowedits part in two places where the allowedit code occurs. the flow on debugging was like this
Load
Me.allowedits =false
if condition=true
field.locked=false
field.enalble=true
The same thing I have replicated in current event of the form.Basically I have just kept the code wherever the allowedits was set to fasle.
But my field is still locked and cannot edit it.Is this because once we set allowedits=false the fields in the page cannot be made editable again.
Is there any other alternative?
Appreciate the help.
Then you can't use a form-level setting.
If this is constant (the fields are always locked), simply set all other controls to Locked = True.
If it's dynamic, use a procedure like this:
Private Sub SetEditable(EnableEdit As Boolean)
Dim ctl As Control
For Each ctl In Me.Controls
' The main editable control types (add more if they occur on your form)
If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or _
ctl.ControlType = acCheckBox Or ctl.ControlType = acSubform Then
If ctl.Name = "MySpecialDateField" Then
' Always editable
ctl.Locked = False
Else
ctl.Locked = Not EnableEdit
' If you want to provide a visual feedback:
' 0 = Flat (locked), 2 = Sunken (editable)
ctl.SpecialEffect = IIf(EnableEdit, 2, 0)
End If
End If
End If
Next ctl
End Sub
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
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()