Using Variables in private sub headings (1 Viewer)

sam1fletcher

Registered User.
Local time
Today, 07:48
Joined
Jan 28, 2013
Messages
40
I have a range of 52 textboxs named Ctl1-Ctl52. now instead of creating 52 event procedures for each text box i would like to create a loop.
I dont know if this is possible but would save alot of time if it is can someone help??

so i expect something like this

Dim i as int
i = 0
While i <=52 then
Private sub Ctl(i)_CLick()
.....Do event
i++
End Sub
wend


is something like this possible??
 

pr2-eugin

Super Moderator
Local time
Today, 07:48
Joined
Nov 30, 2011
Messages
8,494
Hello Sam,
is something like this possible??
Yes it is possible.. You are almost there.. just needs a bit more specifics.. it should be something along the lines of..
Code:
Public Sub TextBoxNames(pfrm As Form)
    Dim ctl As Control
    For Each ctl In pfrm.Controls
        If ctl.ControlType = acTextBox Then
            Debug.Print ctl.Name
        End If
    Next ctl
    Set ctl = Nothing
End Sub
So to call this function, from a click of a button on the form..
Code:
Private Sub someButton_Click()
    TextBoxNames(Me)
End Sub
 
Last edited:

sam1fletcher

Registered User.
Local time
Today, 07:48
Joined
Jan 28, 2013
Messages
40
Thanks Eugin for the reply.

This isnt quite what i was looking for. maybe i didnt explain it correctly. each textbox when clicked changes its colour. every 52 textboxes have been conditionally formatted so if their value is 1 then it turns blue. so instead of creating 52 "on click" event procedures which if clicked sets Textbox value to 1. i wanted a loop that does this. thinking about it myself i dont thinks is possible so ill just have to change every event procedure when i come up with abit of new code

sam
 

pr2-eugin

Super Moderator
Local time
Today, 07:48
Joined
Nov 30, 2011
Messages
8,494
Well if you could show more code like what is the conditional format you have it might help..

I would not give up yet !!

I have created a small sample.. In which I have a form, with 12 text boxes named Text1....Text12, however the naming is not of great importance here, as we are using Control object not basing it on Names.. regardless.. there is a button which on click would turn all text boxes that have an even number to YELLOW.. Similar code, small change..
Code:
Public Sub TextBoxNames(pfrm As Form)
    Dim ctl As Control
    For Each ctl In pfrm.Controls
        If ctl.ControlType = acTextBox Then
            [COLOR=Blue][B]If pfrm.Controls(ctl.Name) Mod 2 = 0 Then pfrm.Controls(ctl.Name).BackColor = vbYellow[/B][/COLOR]
        End If
    Next ctl
    Set ctl = Nothing
End Sub
 

Attachments

  • SampleDB.mdb
    336 KB · Views: 56

sam1fletcher

Registered User.
Local time
Today, 07:48
Joined
Jan 28, 2013
Messages
40
hey eugin i can see what you are doing there:

hopefully better explained.

Code:
Private Sub Ctl2_Click()
If Me.Ctl2.Value = 1 Then
Me.Ctl2.Value = 0
Else
Me.Ctl2.Value = 1
End If
End Sub

Private Sub Ctl3_Click()
If Me.Ctl3.Value = 1 Then
Me.Ctl3.Value = 0
Else
Me.Ctl3.Value = 1
End If
End Sub
Private Sub Ctl4_Click()
If Me.Ctl4.Value = 1 Then
Me.Ctl4.Value = 0
Else
Me.Ctl4.Value = 1
End If
End Sub
Private Sub Ctl5_Click()
If Me.Ctl5.Value = 1 Then
Me.Ctl5.Value = 0
Else
Me.Ctl5.Value = 1
End If
End Sub

So this is 4 textboxes i have and when i click on one it changes the value which then turns it blue if it = 1...
instead of doing this 52 times because a have 52 texboxes the same. the idea was to loop through them. i realise this cannot happen as its the event procedure of _click that is needed for the code to function.
 

pr2-eugin

Super Moderator
Local time
Today, 07:48
Joined
Nov 30, 2011
Messages
8,494
Again... You can have a button and code that button to call the function, and change the code as..
Code:
Public Sub changeColors(pfrm As Form)
    Dim ctl As Control
    For Each ctl In pfrm.Controls
        If ctl.ControlType = acTextBox Then
            If pfrm.Controls(ctl.Name) = 1 Then pfrm.Controls(ctl.Name) = 0
        End If
    Next ctl
    Set ctl = Nothing
End Sub
 

ChrisO

Registered User.
Local time
Today, 16:48
Joined
Apr 30, 2003
Messages
3,202
Sam.

Code:
Private Sub Form_Open(Cancel As Integer)
    Dim lngIndex As Long
    Dim strName  As String
    
    For lngIndex = 1 To 52
        strName = "Ctl" & CStr(lngIndex)
        Me(strName).OnClick = "=HandleClick([" & strName & "])"
    Next lngIndex
    
End Sub


Private Function HandleClick(ctl As Control)

    ctl.Value = IIf(ctl.Value, 0, 1)
    
End Function

Chris.
 

Users who are viewing this thread

Top Bottom