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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass Control

Status
Not open for further replies.

AlanJordan

Programmer
Sep 15, 2005
139
US
As I increase the number of text boxes that I have on a form, it becomes obvious to me that code like this for each text box is inefficient.
Code:
    With Me("txtNonSchHours" & i)
        .Value = ""
        .Format = "Fixed"
        .DecimalPlaces = 2
    End With

    With Me("txtCode" & i)
        .Value = ""
        .Format = "Fixed"
        .DecimalPlaces = 2
    End With

. . . etc.
Can anyone suggest the best way to make this more generic, passing a control.
 
Yes, use the controls collection of the form loop through them to set the control properties. Example...


For each ctl in me.controls
with ctl
.value = ""
.format = "Fixed"
.decimalplaces = 2
end with
next

Gary
gwinn7
 
How are ya AlanJordan . . .

[blue]gwinn7[/blue] in on target with the [blue]controls collection[/blue].

Another technique is to set [purple]Tag[/purple] property of all controls of interest to say a question mark [blue]?[/blue]. This allows you to pick out only those controls you need. Then its a loop thru the [blue]controls collection[/blue]:
Code:
[blue]   Dim ctl As Control
   
   For Each ctl In Me.Controls
      If ctl.Tag = "?" Then
          With ctl
            .Value = ""
            .Format = "Fixed"
            .DecimalPlaces = 2
         End With
      End If
   Next[/blue]
[purple]Your thoughts . . .[/purple]

Calvin.gif
See Ya! . . . . . .
 
Hi Gwinn7,

Thanks for your continued feedback. I was just about to upload the code that I implemented, which is a hybrid of your suggestions:

Code:
Sub ClearCurrentInfo()
On Error GoTo err_CCI
Dim ctrl As Control
Dim i As Integer
For i = 0 To 6
    Me!cboMyNameIs.Value = ""
Next i
Me!lblOtherExplan.Caption = ""
For Each ctrl In Me.Controls
  If TypeOf ctrl Is TextBox Then
     Select Case ctrl.Tag
        Case "DNR"
            'Take no action
        Case "DNR1"
            ctrl.Value = ""
        Case Else
             With ctrl
                .Value = ""
                .Format = "Fixed"
                .DecimalPlaces = 2
            End With
        End Select
     End If
Next ctrl
Exit Sub
err_CCI:
    ErrBox ("The problem originated in ClearCurrentInformation in frmTimecard.")
End Sub

Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top