I am just a beginner of VB6 but I know there are plenty of you here in this forum to support people like me.
I wanted to write a code so that when I click on a command button it shows a random number in the text box. The random number I want will depen on 3 check boxes.
If the first check box is checked, I want the random number from 1 to 3. If the second check box is checked, I want the random number from 4 to 6. And if the third check box is checked, I want the random number from 7 to 9.
Then how can I write a code so that when the first and the third check boxes are checked a random number shows in my text box varies either 1, 2, 3, 7, 8 and 9 (but not 4, 5 and 6)?
There's some complexity to your problem, which makes it interesting. Here's a fully functional solution:
Code:
Option Explicit
Private mlngArray() As Long
Private mlngRange As Long
Private Sub Form_Load()
Randomize
End Sub
Private Sub Command1_Click()
Dim lngNumber As Long
Dim i As Long
Me.txtDisplay.Text = vbNullString
ReDim mlngArray(0)
mlngRange = -1
For i = 0 To 2
If Me.chkRange(i).Value = vbChecked Then AddNumbers i
Next
If mlngRange = -1 Then
MsgBox "Check at least one checkbox", vbInformation, "Notice"
Else
lngNumber = Int((mlngRange + 1) * Rnd)
Me.txtDisplay.Text = mlngArray(lngNumber)
End If
End Sub
Private Sub AddNumbers(plngIndex As Long)
Dim i As Long
mlngRange = mlngRange + 3
ReDim Preserve mlngArray(mlngRange)
For i = 0 To 2
mlngArray(mlngRange - 2 + i) = plngIndex * 3 + i + 1
Next
End Sub
Let me know if you have any questions.
Last edited by Ellis Dee; Dec 18th, 2008 at 07:58 AM.
Here is the way I would do it. Again you should use a control array of checkboxes to make things easy
Code:
Private Sub Command1_Click()
Dim i As Integer
Dim strNumString As String
Dim strNums() As String
'see which checkboxes are checked and
'add the possible numbers to a string
For i = 0 To 2
If Check1(i).Value = vbChecked Then
strNumString = strNumString & Check1(i).Tag
End If
Next i
'Spliting the number string on the space will
'add all the possiblities to an array
strNums = Split(strNumString, " ")
'Select a random index from the array
Randomize
i = Int(Rnd() * (UBound(strNums)))
'display the results
Text1.Text = strNums(i)
End Sub
Private Sub Form_Load()
'add the values for each checkbox
'note the trailing space is important
Check1(0).Tag = "1 2 3 "
Check1(1).Tag = "4 5 6 "
Check1(2).Tag = "7 8 9 "
Check1(0).Caption = "1-3"
Check1(1).Caption = "4-6"
Check1(2).Caption = "7-9"
End Sub
I am very surprised to get in my office early in the morning and found some replies to my thread just posted 12 hours ago (just over night). Many thanks to Jmacp, Hack, Ellis Dec and MarkT for their kind and quick support.
Although I haven't tried any of these yet but I am sure they are very helpfull. I will try it and let you know the result.
Thanks again for all supporters.
All the best to you all,
Viratha
Your code worked perfectly for me. Thank you very much for your support.
Best regards,
Viratha
Originally Posted by MarkT
Here is the way I would do it. Again you should use a control array of checkboxes to make things easy
Code:
Private Sub Command1_Click()
Dim i As Integer
Dim strNumString As String
Dim strNums() As String
'see which checkboxes are checked and
'add the possible numbers to a string
For i = 0 To 2
If Check1(i).Value = vbChecked Then
strNumString = strNumString & Check1(i).Tag
End If
Next i
'Spliting the number string on the space will
'add all the possiblities to an array
strNums = Split(strNumString, " ")
'Select a random index from the array
Randomize
i = Int(Rnd() * (UBound(strNums)))
'display the results
Text1.Text = strNums(i)
End Sub
Private Sub Form_Load()
'add the values for each checkbox
'note the trailing space is important
Check1(0).Tag = "1 2 3 "
Check1(1).Tag = "4 5 6 "
Check1(2).Tag = "7 8 9 "
Check1(0).Caption = "1-3"
Check1(1).Caption = "4-6"
Check1(2).Caption = "7-9"
End Sub
Mark's code will never pick the highest number. To correct this bug, change this line:
i = Int(Rnd() * (UBound(strNums)))
To this:
i = Int((UBound(strNums) + 1) * Rnd)
EDIT: On second look, the above is not true. His array has an extra unused element, and that's the one that never gets selected. Nice and elegant, Mark.
To repeat, do not make this change.
EDIT 2: You shouldn't call Randomize each time. It would be better in the Form_Load event.
Last edited by Ellis Dee; Dec 19th, 2008 at 12:36 AM.
What do you mean that Mark's code will never pick the highest number?
I am also having difficulty understanding the rest of your instruction. That because I am new to VB. Sorry
I attached the form which I developed base on Mark's code for the example. Please have a quick look at that and advise.
By the way there is an imorvement require for this form. If the command button is clicked while no checkbox is checked there is an error message.
It read "Run-time eror '9'. Subscription out of range". I want to get rid of this and want a message to tell users to at least check one checkbox.
Hope you could help.
Thanks,
Viratha
Originally Posted by Ellis Dee
Mark's code will never pick the highest number. To correct this bug, change this line:
i = Int(Rnd() * (UBound(strNums)))
To this:
i = Int((UBound(strNums) + 1) * Rnd)
EDIT: On second look, the above is not true. His array has an extra unused element, and that's the one that never gets selected. Nice and elegant, Mark.
To repeat, do not make this change.
EDIT 2: You shouldn't call Randomize each time. It would be better in the Form_Load event.
I clarified that there wasn't a problem about picking the highest number.
Immediately after the For...Next loop add the following block:
Code:
For i = 0 To 2
If Check1(i).Value = vbChecked Then
strNumString = strNumString & Check1(i).Tag
End If
Next i
If Len(strNumString) = 0 Then
MsgBox "Check at least one checkbox", vbInformation, "Notice"
Exit Sub
End If
'Spliting the number string on the space will
Build a form with a text box, a command button, and an array of three check boxes (indexed 0 to 2). Then apply this code:
Code:
Private Sub Command1_Click()
Dim MaxVal As Integer, MinVal As Integer
Text1.Text = vbNullString
MinVal = 1
For I = 0 To 2
MaxVal = MaxVal + 3
Check1(I).Caption = MinVal & " to " & MaxVal
If Check1(I).Value = vbChecked Then
Text1.Text = Text1.Text & Int((MaxVal - MinVal + 1) * Rnd + MinVal)
End If
MinVal = MinVal + 3
Next
End Sub
Private Sub Form_Load()
Randomize
Command1.Value = True
End Sub
Doc, he needs the result to be a single digit number within any of the checked ranges. Note the example from the OP, where if the first and last are checked, the set of possible numbers is 1,2,3,7,8,9.
Doc, he needs the result to be a single digit number within any of the checked ranges. Note the example from the OP, where if the first and last are checked, the set of possible numbers is 1,2,3,7,8,9.
Thanks, Ellis. He wants just one number to appear in the text box, so I revised the code by adding a line after the loop:
Code:
Private Sub Command1_Click()
Dim MaxVal As Integer, MinVal As Integer
Text1.Text = vbNullString
MinVal = 1
For I = 0 To 2
MaxVal = MaxVal + 3
Check1(I).Caption = MinVal & " to " & MaxVal
If Check1(I).Value = vbChecked Then
Text1.Text = Text1.Text & Int((MaxVal - MinVal + 1) * Rnd + MinVal)
End If
MinVal = MinVal + 3
Next
If Len(Text1.Text) Then Text1.Text = Mid$(Text1.Text, Int(Rnd * (Len(Text1.Text)) + 1), 1)
End Sub
Private Sub Form_Load()
Randomize
Command1.Value = True
End Sub
Mine is a bit different approach. All I did was select one random digit from the elements of the sets of those permitted by the check box constraints.
As I told you in my previous message your code worked perfectly for me.
But now when I want the first checkbox random number range from say 1 to 100 or even bigger, how can I make a short cut instead of "Check1(0).Tag = "1 2 3 4 5 6 7 8 9 10...100..." ?
Private Sub Form_Load()
Dim i as Integer
'add the values for each checkbox
'note the trailing space is important
For i = 1 to 100
' 1 to 100
Check1(0).Tag = Check1(0).Tag & i & " "
' 101 to 200
Check1(0).Tag = Check1(0).Tag & i + 100 & " "
' 201 to 300
Check1(0).Tag = Check1(0).Tag & i + 200 & " "
Next i
End Sub
I think I'd take a different approach and create 3 random numbers right at the start, one for each CheckBox range, then randomly select one of them depending upon the combination of selected CheckBoxes.
Something like this:
Code:
Option Explicit
Private intNumbers(2) As Integer
Private intRanges(2, 1) As Integer
Private intTemp(2) As Integer
Private Function CreateRandom(intMin As Integer, intMax As Integer) As Integer
CreateRandom = Int((intMax - intMin + 1) * Rnd + intMin)
End Function
Private Sub CmdCreate_Click()
Dim intI As Integer
Dim intJ As Integer
'
' Create 3 Random Numbers in the appropriate ranges
'
For intI = 0 To 2
intNumbers(intI) = CreateRandom(intRanges(intI, 0), intRanges(intI, 1))
Next intI
For intI = 0 To 2
'
' If the CheckBox is selected then copy its Random number
' into the next free element of the temporary array
'
If Check1(intI).Value = 1 Then
intTemp(intJ) = intNumbers(intI)
intJ = intJ + 1
End If
Next intI
If intJ <> 0 Then
'
' Randomly select one of the temporary array elements
' and assign it to the TextBox
'
txtResult.Text = intTemp(CreateRandom(0, intJ - 1))
Else
MsgBox "Please select one of the CheckBoxes"
End If
End Sub
Private Sub Form_Load()
Randomize
intRanges(0, 0) = 1 'Minimun for CheckBox(0)
intRanges(0, 1) = 3 'Maximum for CheckBox(0)
intRanges(1, 0) = 4 'Minimum for CheckBox(1)
intRanges(1, 1) = 6 'Maximum for CheckBox(1)
intRanges(2, 0) = 7 'Minimum for CheckBox(2)
intRanges(2, 1) = 10 'Maximum for CheckBox(2)
End Sub
Souped up version - there's no need for the temporary array....
Code:
Option Explicit
Private intNumbers(2) As Integer
Private intRanges(2, 1) As Integer
Private Function CreateRandom(intMin As Integer, intMax As Integer) As Integer
CreateRandom = Int((intMax - intMin + 1) * Rnd + intMin)
End Function
Private Sub CmdCreate_Click()
Dim intI As Integer
Dim intJ As Integer
'
' If the CheckBox element is selected
' create a Random Number in the appropriate range
' and assign it to the next free intNumbers element
'
For intI = 0 To 2
If Check1(intI).Value = 1 Then
intNumbers(intJ) = CreateRandom(intRanges(intI, 0), intRanges(intI, 1))
intJ = intJ + 1
End If
Next intI
If intJ <> 0 Then
'
' Randomly select one of the array elements
' and assign it to the TextBox
'
txtResult.Text = intNumbers(CreateRandom(0, intJ - 1))
Else
MsgBox "Please select one of the CheckBoxes"
End If
End Sub
Private Sub Form_Load()
Randomize
intRanges(0, 0) = 1 'Minimun for CheckBox(0)
intRanges(0, 1) = 3 'Maximum for CheckBox(0)
intRanges(1, 0) = 4 'Minimum for CheckBox(1)
intRanges(1, 1) = 6 'Maximum for CheckBox(1)
intRanges(2, 0) = 7 'Minimum for CheckBox(2)
intRanges(2, 1) = 10 'Maximum for CheckBox(2)
End Sub
Ellis - you're absolutely correct. The skew is very significant, probably too much so to be of much use! (Unless, as you say, that's the intention. ie there's an equal probability of selecting a number from any of the selected CheckBoxes)
On a trial of 1000 runs using your ranges, 330 were between 1 and 3 when all 3 CheckBoxes were selected (fancy that - nearly a third - goes to show the Random Number Geneator is fairly Random )
Last edited by Doogle; Dec 21st, 2008 at 01:44 AM.
As I told you in my previous message your code worked perfectly for me.
But now when I want the first checkbox random number range from say 1 to 100 or even bigger, how can I make a short cut instead of "Check1(0).Tag = "1 2 3 4 5 6 7 8 9 10...100..." ?
Thanks in advance for your kind support.
Viratha
Before you change the constraint on the first check box, you need to define what you want the other two check boxes to do as well. How do the second and third check boxes influence the first and vice versa?
Methinks you are leading the VB6 forum into a snake pit.
Mark, thanks for your kind support and quick response. I've tried a bit on your code but sorry it doesn't seem to work the way I want this time.
(1) if I followed exactly your code, check the first checkbox is ok but check the second and third checkbox will induce a message "Check at least one checkbox".
(2) I then changed the number in the parenthesis like below:
For i = 1 to 100
' 1 to 100
Check1(0).Tag = Check1(0).Tag & i & " "
' 101 to 200
Check1(1).Tag = Check1(1).Tag & i + 100 & " "
' 201 to 300
Check1(2).Tag = Check1(2).Tag & i + 200 & " "
Next i
After I changed these it seems to work ok.
I am very sorry for all the inconvenients so far for you and for the forum. In fact I should have given you the whole picture of what I want to do from the beginning. Instead I've invented a simple question with a hope that it be easier understood and to received a simple answer. Then I will work on my own base on the supported codes I got from the forum. However, thing is not as simple as I thought...on the one hand a final product that I want kept changing over time as thing progress...on the other hand the more I've learnt about VB the more complicated interface I want it to be. But isn't it a sign of progress as the result of your support and others in this forum?
I was just passing by when this thread caught my interest. Of all the solutions, I like mine best
(This is for you, Ellis Dee. I trust you can figure it out.)
Code:
Option Explicit
Private Type Range
Min As Long
Max As Long
End Type
Private AllRanges(2) As Range
Private Sub Form_Load()
Dim i As Long
Randomize
AllRanges(0).Min = 1
AllRanges(0).Max = 3
AllRanges(1).Min = 4
AllRanges(1).Max = 6
AllRanges(2).Min = 7
AllRanges(2).Max = 9
For i = 0 To 2
Check1(i).Caption = AllRanges(i).Min & " to " & AllRanges(i).Max
Next i
End Sub
Private Sub Command1_Click()
Dim n As Long
Dim i As Long
Dim r As Long
Dim x As Long
For i = 0 To 2
If Check1(i).Value = vbChecked Then
n = n + 1 + AllRanges(i).Max - AllRanges(i).Min
End If
Next i
If n <> 0 Then
r = Int(Rnd * n)
For i = 0 To 2
If Check1(i).Value = vbChecked Then
If AllRanges(i).Min + r > AllRanges(i).Max Then
r = r - 1 - AllRanges(i).Max + AllRanges(i).Min
Else
x = AllRanges(i).Min + r
Exit For
End If
End If
Next i
Label1.Caption = x
Else
Label1.Caption = "-"
End If
End Sub
I was just passing by when this thread caught my interest. Of all the solutions, I like mine best
(This is for you, Ellis Dee. I trust you can figure it out.)
Yes, it is far and away the best solution. Any size ranges are supported, and it requires no wasted memory on an array holding each possible result. Very nice.
Though it would have been nice to stuff the Min,Max values in the Tag property of each checkbox to make it easier for the programmer to maintain.
Your code bellow as I have informed you worked perfectly for me when I use checkbox with random number.
Now that I want to change from "checkbox" to "option"…I’ve created an array of option and replaced the word ‘Check1’ with ‘Option1’ in your code…but unfortunately this doesn’t work. I don’t know why but perhaps a required code for checkbox and option in this case is not the same. Would you please advise. Viratha
Originally Posted by MarkT
Here is the way I would do it. Again you should use a control array of checkboxes to make things easy
Code:
Private Sub Command1_Click()
Dim i As Integer
Dim strNumString As String
Dim strNums() As String
'see which checkboxes are checked and
'add the possible numbers to a string
For i = 0 To 2
If Check1(i).Value = vbChecked Then
strNumString = strNumString & Check1(i).Tag
End If
Next i
'Spliting the number string on the space will
'add all the possiblities to an array
strNums = Split(strNumString, " ")
'Select a random index from the array
Randomize
i = Int(Rnd() * (UBound(strNums)))
'display the results
Text1.Text = strNums(i)
End Sub
Private Sub Form_Load()
'add the values for each checkbox
'note the trailing space is important
Check1(0).Tag = "1 2 3 "
Check1(1).Tag = "4 5 6 "
Check1(2).Tag = "7 8 9 "
Check1(0).Caption = "1-3"
Check1(1).Caption = "4-6"
Check1(2).Caption = "7-9"
End Sub
Dear all, with supports from this forum I am now able to achieve what I wanted. Thanks to Mark for the random number code, to Ellis for the message box code and to Edgemeal for the changing from checkbox to option code. Wow, what a great support. I can’t imagine that I can do this that fast.
Now I want to improve a product I have achieved. Again need your support (please refer to the attached form).
If user ticks on anyone of the option in the ‘Subject’ frame the corresponding comboBox will appear bellow the subject frame showing all lessons under that subject. Let say user ticked on 'Geography' subject, lessons in the comboBox are 'Lesson_Geo All, Lesson_Geo 1 and Lesson_Geo n'. In this case (example) only 2 questions (Q4 and Q5) for Lesson_Geo 1 and 1 question (Q6) for Lesson_Geo n.
After the command ‘Question’ is clicked, what should be a code for:
1. I want random Q4 or Q5 in the ‘Question’ frame if ‘Lesson_Geo 1’ was selected.
2. I want Q6 in the ‘Question’ frame if ‘Lesson_Geo n’ was selected.
3. I want random Q4, Q5 or Q6 in the ‘Question’ frame if ‘Lesson_Geo All’ was selected.
4. I want all random questions from Q1 to Q12 under all subjects in the ‘Question’ frame if ‘All Subjects’ in the subject frame is selected.
5. As you would note in the ‘File menu’ there is a ‘Print record’ submenu. What should be a code to print all questions and answers that appear from the start of the interface till the ‘Print record’ is selected?
6. Apart from the above questions, as a programmer, I want to know how often each question appears as a result of the command ‘Question’ clicked. One way to control this is to record the question codes. How can I code this and view it whenever I want?
7. Last but not least, what should be a code to control the same random number from being appeared more than once? Forget this question if it is too complicated.
By the way please note that a random number in the attached form link only with subject in the 'subject' frame but not with lesson in the comboBox.
Thanks in advance for your kind support. Viratha
Edit 1: Sorry, question 4 should read "I want all random questions from Q1 to Q12 under all subjects in the ‘Subject’ frame if ‘All Subjects’ in the subject frame is selected."
Last edited by Viratha; Jan 5th, 2009 at 05:28 AM.
I understand that the questions I posted on 5 January is a bit annoying...too long. Therefore, I should make it short and simpler this time.
Would anyone please tell me how to write a code so that a range of random number (example 1 to 10) be displayed in the text box when its corresponding item is selected from a comboBox (please refer to the form posted on 5 Jan).
See post #31. Previous replies were helpful and were appreciated. Viratha's comment in post #33 was likely referring to the lack of further support regarding the additional requests made in post #31.
I am very sorry to see some people upset to my comment post #33.
In fact my thank in post #33 was real and sincere thank. Don’t look at it in a negative way.
Even when I was not helped I still look at it as positive. Because no one offered help, it is a chance for me to help myself…I have learnt a lot base on my capacity with a trials and errors. This is why I thank for not helping me. It is really a sincere thank, I repeat.
By nature I am always acknowledge, appreciate and grateful to all help. Look at questions and answers in the form attached to post #33, you will find more about my genuineness.
Thanks.
Viratha
Last edited by Viratha; Jan 15th, 2009 at 04:52 AM.