|
-
Aug 24th, 2008, 05:19 PM
#1
Thread Starter
New Member
[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
-
Aug 25th, 2008, 04:29 AM
#2
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 25th, 2008, 03:07 PM
#3
Thread Starter
New Member
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.
-
Aug 25th, 2008, 03:49 PM
#4
Addicted Member
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.
-
Aug 25th, 2008, 04:36 PM
#5
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 27th, 2008, 06:59 AM
#6
Thread Starter
New Member
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
-
Aug 27th, 2008, 04:26 PM
#7
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
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 28th, 2008, 12:15 PM
#8
Thread Starter
New Member
Re: Loop for text and number
Thx the above solution works perfectly.. i put this thread on solved. Thanks everybody..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|