[RESOLVED] Loop for text and number
I was helped this weekend by this code...and a lot other stuff. My next question is this
How can i put a loop in place for both nummers as txt.. the example below is only for numbers, what i want is that it is also for different letters behind Chk
For example Chkbrep / Chkol / chk1 / chktt / chk 5 etc..
Code:
Dim TempName as string
For i = 1 to 12
TempName = "Chk" & i
If ActiveCell <> "" Then
ActiveCell.Offset(0, 16).Select
Me.Controls(TempName).Caption = ActiveCell.Value
Me.Controls(TempName).Enabled = True
Else
'More code
End If
Next
Re: Loop for text and number
you would need to loop through all the controls on the form and if the type of control is chkbox then do whatever, but this is considerably more difficult as you can not be sure that the next control is the correct one
another possibility is to get a list of the checkbox names into an array that can then be sorted, you can then loop through the checkboxes using the name in the array
vb Code:
for i = 0 to ubound(chknamearray)
me.controls(chknamearray(i)).caption= somevalue
next
Re: Loop for text and number
As i understand it, the above code is to sort the list of tekstboxes and in my case to set the value at False. If i define all checkboxes in the array then there is no problem.
But when i make a new one, it would be not resetting to false due to the fact that it isn't added to the array.
All my checkboxes start with Chk***, so couldn't there be a different code that in future if checkboxes are added or deleted i do not have to think of the array.
Re: Loop for text and number
You could do some up front legwork and create a second array so your code would be as such:
Code:
Dim TempName as string
Dim TempArray(11) as string
TempArray(0) = "brep"
TempArray(1) = "ol"
TempArray(2) = "1"
TempArray(3) = "tt"
TempArray(4) = "5"
'continue...
For i = 0 to 11
TempName = "Chk" & TempArray(i)
If ActiveCell <> "" Then
ActiveCell.Offset(0, 16).Select
Me.Controls(TempName).Caption = ActiveCell.Value
Me.Controls(TempName).Enabled = True
Else
'More code
End If
Next
Please note, that you'd have to do the legwork of manually entering everything into the temp array first so that you can loop through that.
Re: Loop for text and number
as i said above you can loop through all the controls and if they are of type checkbox then do something, but the difficulty is to know what order they will be returned, so as to match that to the correct cell value
if you can figure a way to have some property (such as the tab order), match the order of the cell, that contains the value then very easy
Re: Loop for text and number
Acually in the initialize of the form i would want that all the checkboxes are False (unchecked) when the forms pop up. Due to the many checkboxes on this form (with multipages) i 'm searching a way to set them all false with a loop. Checkboxes are named CHK.... Now it is possible that in future new checkboxes are added and i want them to be false also when the forms popup. It's also needed when another person adds a checkbox and forget to change the array. I do not want to change the initialize code eachtime, so to define the array it is quit some work and i thought that a loop was a way to do so without defining an array. Also a smaller code is more readable then a long time consuming code.
What the controls means, i do not understand that part. I do not use tabs or so. It all happens in the initialise
Re: Loop for text and number
try like this
vb Code:
For Each c In Me.Controls
If Left(c.Name, 3) = "CHK" Then c.Value = False
Next
though as far as i know all checkbox values are false by default, when form loads, until changed
value is checked or unchecked
enabled is whether the user can change the vale by clicking
you can set the default value or enabled property of the checkbox on load by setting the property at design time in the properties window
Quote:
What the controls means, i do not understand that part. I do not use tabs or so. It all happens in the initialise
all command buttons, text boxes, labels, check boxes, etc are controls on the form, each can have specific properties, but several properties are common to all, many have a TabStop and TabIndex property that tells, if the user tabs which order they are selected, or if they are skipped
Re: Loop for text and number
Thx the above solution works perfectly.. i put this thread on solved. Thanks everybody..