Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clear Fields and Reset Defaults 3

Status
Not open for further replies.

waubain

Technical User
Dec 13, 2011
200
0
0
US
I have looked for a sub or func to reset property defaults back to the default in the Property Sheet, but without any luck. Similar to what would happen if you closed the form and reopened it, except I do not want to close the form. I have a different sub that sets the controls to NULL.

This works but looks less than ideal.

Code:
Private Sub SetDefaults()

	Me.txtUnit1.Enabled = True
        Me.txtUnit2.Enabled = True
        Me.txtUnit3.Enabled = True
        Me.txtUnit4.Enabled = True
        Me.txtUnit5.Enabled = True
        Me.txtUnit6.Enabled = True
        Me.txtUnit7.Enabled = True
        Me.txtUnit8.Enabled = True
        Me.txtUnit9.Enabled = True
        Me.txtUnit10.Enabled = True
        Me.txtUnit11.Enabled = True
        Me.txtUnit12.Enabled = True

End Sub

I tried to simplify this sub with following

Code:
Private Sub SetDefaults()
Dim i As Integer
Dim regNumber As Integer
Dim strControlName As String

strControlName = "txtUnit"
regNumber = 12

For i = 1 To regNumber
  
          Me."strControlName" & "i".Enabled = True

Next i
End Sub

but does stops at
Code:
Me."strControlName" & "i".Enabled = True
The error message is Syntax Error. I tried to enclose in [] and () or just one "...", but Syntax Error at the same line.
Thanks


 
Me.Controls("strControlName" & i).Enabled = True

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
me.controls(strControlName & i).enabled

strControlName is the variable
or
me.controls("txtUnit" & i).enabled
 

You may want to try something like this:
Code:
Dim cntr As Control

For Each cntr in Me.Controls
    If TypeOf cntr Is UserForm.TextBox Then
        [blue]If Left(cntr.Name, 7) = "txtUnit" Then[/blue]
            cntr.Enabled = True
        [blue]End If[/blue]
    End If
Next cntr
If you do not have other textboxes on your Form, just the txtUnit(s), you may skip the blue part of the code.

Have fun.

---- Andy
 
Even easier. Select them all and put a tag in the tag field.

For Each cntr in Me.Controls
If cntr.tag = "someTag"
cntr.Enabled = True
End If
Next cntr
 
Thanks everyone for your ideas and suggestions. I have never heard of some of what each of you suggested, but gets me thinking about other possibilities.

I may go with MajP suggestion, as most controls are enable = True there are a few others that are = False, so I could use a Else: in that sub.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top