12 Replies - 743 Views - Last Post: 31 December 2011 - 02:17 AM Rate Topic: -----

#1 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

Using CheckBoxes to Return Variable Data

Posted 21 December 2011 - 06:39 PM

I'm back again after an unnecessarily long absence, and of course the first thing I do is jump right into a question. Go figure. I understand that the topic is a bit vague, but I will explain extensively here.

I am wanting to use multiple checkboxes to select specific values and show them in textboxes in the order they are selected. At this time I have no working code to share because I have not been able to figure any of this out. Before anyone asks, I did take the time to search this problem on the web, I did go through the MSDN Library, and I also tried doing this with OptionButtons.

To add a bit more clarity, there are 18 checkboxes, and 6 sets of data I need returned. So, if a person were to click, in this order, 12, 15, 2, 18, 5, and 7, then i would need them listed in that exact order (txt1 = 12, txt2 = 15, and so on).

Any help would be greatly appreciated. Thanks in advance.



EDIT: I have been working on this and have gotten this much completed. I am including a copy of the actual Form I'm working on for your viewing pleasure. And yes, I do know that the code is bulky and unattractive. It's what I'm used to.
Attached File  TestRoll.zip (1.62K)
Number of downloads: 45

Private Sub cmdRollD20_Click()

chkStat(0).Value = vbUnchecked
chkStat(1).Value = vbUnchecked
chkStat(2).Value = vbUnchecked
chkStat(3).Value = vbUnchecked
chkStat(4).Value = vbUnchecked
chkStat(5).Value = vbUnchecked
chkStat(6).Value = vbUnchecked
chkStat(7).Value = vbUnchecked
chkStat(8).Value = vbUnchecked
chkStat(9).Value = vbUnchecked
chkStat(10).Value = vbUnchecked
chkStat(11).Value = vbUnchecked
chkStat(12).Value = vbUnchecked
chkStat(13).Value = vbUnchecked
chkStat(14).Value = vbUnchecked
chkStat(15).Value = vbUnchecked
chkStat(16).Value = vbUnchecked
chkStat(17).Value = vbUnchecked

Randomize
A = CInt((20 - 1 + 1) * Rnd + 1)
B = CInt((20 - 1 + 1) * Rnd + 1)
C = CInt((20 - 1 + 1) * Rnd + 1)
D = CInt((20 - 1 + 1) * Rnd + 1)
E = CInt((20 - 1 + 1) * Rnd + 1)
F = CInt((20 - 1 + 1) * Rnd + 1)
G = CInt((20 - 1 + 1) * Rnd + 1)
H = CInt((20 - 1 + 1) * Rnd + 1)
I = CInt((20 - 1 + 1) * Rnd + 1)
J = CInt((20 - 1 + 1) * Rnd + 1)
K = CInt((20 - 1 + 1) * Rnd + 1)
L = CInt((20 - 1 + 1) * Rnd + 1)
M = CInt((20 - 1 + 1) * Rnd + 1)
N = CInt((20 - 1 + 1) * Rnd + 1)
O = CInt((20 - 1 + 1) * Rnd + 1)
P = CInt((20 - 1 + 1) * Rnd + 1)
Q = CInt((20 - 1 + 1) * Rnd + 1)
R = CInt((20 - 1 + 1) * Rnd + 1)

If A >= 20 Then
    A = 20
End If
If B >= 20 Then
    B = 20
End If
If C >= 20 Then
    C = 20
End If
If D >= 20 Then
    D = 20
End If
If E >= 20 Then
    E = 20
End If
If F >= 20 Then
    F = 20
End If
If G >= 20 Then
    G = 20
End If
If H >= 20 Then
    H = 20
End If
If I >= 20 Then
    I = 20
End If
If J >= 20 Then
    J = 20
End If
If K >= 20 Then
    K = 20
End If
If L >= 20 Then
    L = 20
End If
If M >= 20 Then
    M = 20
End If
If N >= 20 Then
    N = 20
End If
If O >= 20 Then
    O = 20
End If
If P >= 20 Then
    P = 20
End If
If Q >= 20 Then
    Q = 20
End If
If R >= 20 Then
    R = 20
End If

txtStr001.Text = A
txtStr002.Text = B
txtStr003.Text = C
txtDex001.Text = D
txtDex002.Text = E
txtDex003.Text = F
txtCon001.Text = G
txtCon002.Text = H
txtCon003.Text = I
txtInt001.Text = J
txtInt002.Text = K
txtInt003.Text = L
txtWis001.Text = M
txtWis002.Text = N
txtWis003.Text = O
txtCha001.Text = P
txtCha002.Text = Q
txtCha003.Text = R


End Sub

Private Sub Form_Load()
Me.Visible = True
End Sub


This post has been edited by Jaalenn: 21 December 2011 - 07:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using CheckBoxes to Return Variable Data

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Re: Using CheckBoxes to Return Variable Data

Posted 22 December 2011 - 12:20 AM

If you need the list to be dynamic, then you need to look at using the Click event of the checkbox. You've set it up as an array, so you can easily determine which item has been checked and you can keep a running count of the order in a list.

The only thing you need to handle, is if the user unchecks an item, you will need to remove that from the list.
Was This Post Helpful? 0
  • +
  • -

#3 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

Re: Using CheckBoxes to Return Variable Data

Posted 22 December 2011 - 12:30 AM

Thanks for answering. My only problem now is figuring out how to do that. Shouldn't be too hard!

Thanks again!

Was This Post Helpful? 0
  • +
  • -

#4 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Re: Using CheckBoxes to Return Variable Data

Posted 22 December 2011 - 01:14 AM

You're right, it shouldn't be too hard.

Have a go at it and post back if you have any issues.
Was This Post Helpful? 1
  • +
  • -

#5 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Using CheckBoxes to Return Variable Data

Posted 22 December 2011 - 07:46 AM

you can replace
chkStat(0).Value = vbUnchecked
chkStat(1).Value = vbUnchecked
chkStat(2).Value = vbUnchecked
chkStat(3).Value = vbUnchecked
chkStat(4).Value = vbUnchecked
chkStat(5).Value = vbUnchecked
chkStat(6).Value = vbUnchecked
chkStat(7).Value = vbUnchecked
chkStat(8).Value = vbUnchecked
chkStat(9).Value = vbUnchecked
chkStat(10).Value = vbUnchecked
chkStat(11).Value = vbUnchecked
chkStat(12).Value = vbUnchecked
chkStat(13).Value = vbUnchecked
chkStat(14).Value = vbUnchecked
chkStat(15).Value = vbUnchecked
chkStat(16).Value = vbUnchecked
chkStat(17).Value = vbUnchecked

with
dim i as integer
for i = 0 to 17
     chkStat(i).Value = vbUnchecked
next i



You can also replace
Randomize
A = CInt((20 - 1 + 1) * Rnd + 1)
B = CInt((20 - 1 + 1) * Rnd + 1)
C = CInt((20 - 1 + 1) * Rnd + 1)
D = CInt((20 - 1 + 1) * Rnd + 1)
E = CInt((20 - 1 + 1) * Rnd + 1)
F = CInt((20 - 1 + 1) * Rnd + 1)
G = CInt((20 - 1 + 1) * Rnd + 1)
H = CInt((20 - 1 + 1) * Rnd + 1)
I = CInt((20 - 1 + 1) * Rnd + 1)
J = CInt((20 - 1 + 1) * Rnd + 1)
K = CInt((20 - 1 + 1) * Rnd + 1)
L = CInt((20 - 1 + 1) * Rnd + 1)
M = CInt((20 - 1 + 1) * Rnd + 1)
N = CInt((20 - 1 + 1) * Rnd + 1)
O = CInt((20 - 1 + 1) * Rnd + 1)
P = CInt((20 - 1 + 1) * Rnd + 1)
Q = CInt((20 - 1 + 1) * Rnd + 1)
R = CInt((20 - 1 + 1) * Rnd + 1)

If A >= 20 Then
    A = 20
End If
If B >= 20 Then
    B = 20
End If
If C >= 20 Then
    C = 20
End If
If D >= 20 Then
    D = 20
End If
If E >= 20 Then
    E = 20
End If
If F >= 20 Then
    F = 20
End If
If G >= 20 Then
    G = 20
End If
If H >= 20 Then
    H = 20
End If
If I >= 20 Then
    I = 20
End If
If J >= 20 Then
    J = 20
End If
If K >= 20 Then
    K = 20
End If
If L >= 20 Then
    L = 20
End If
If M >= 20 Then
    M = 20
End If
If N >= 20 Then
    N = 20
End If
If O >= 20 Then
    O = 20
End If
If P >= 20 Then
    P = 20
End If
If Q >= 20 Then
    Q = 20
End If
If R >= 20 Then
    R = 20
End If

txtStr001.Text = A
txtStr002.Text = B
txtStr003.Text = C
txtDex001.Text = D
txtDex002.Text = E
txtDex003.Text = F
txtCon001.Text = G
txtCon002.Text = H
txtCon003.Text = I
txtInt001.Text = J
txtInt002.Text = K
txtInt003.Text = L
txtWis001.Text = M
txtWis002.Text = N
txtWis003.Text = O
txtCha001.Text = P
txtCha002.Text = Q
txtCha003.Text = R

with
Randomize
txtStr001.Text cStr(Int(Rnd + 1) * 20)
txtStr002.Text = cStr(Int(Rnd + 1) * 20)
txtStr003.Text = cStr(Int(Rnd + 1) * 20)
txtDex001.Text = cStr(Int(Rnd + 1) * 20)
txtDex002.Text = cStr(Int(Rnd + 1) * 20)
txtDex003.Text = cStr(Int(Rnd + 1) * 20)
txtCon001.Text = cStr(Int(Rnd + 1) * 20)
txtCon002.Text = cStr(Int(Rnd + 1) * 20)
txtCon003.Text = cStr(Int(Rnd + 1) * 20)
txtInt001.Text = cStr(Int(Rnd + 1) * 20)
' and so on. 


Even better in this case would be to use a control array.

Perhaps it's time to get used to writing code that ISN'T "bulky and unattractive." It would better match your excellent command of the written word. :) You could go a long way toward this goal by asking yourself (or us) how you could improve repetitive code by using the various looping structures.

This post has been edited by BobRodes: 22 December 2011 - 07:48 AM

Was This Post Helpful? 1
  • +
  • -

#6 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

Re: Using CheckBoxes to Return Variable Data

Posted 22 December 2011 - 12:21 PM

Wow, Bob, almost all of that worked extremely well! As for repetitive code, I have no idea how to use looping statements, or many other funtions of VB I am sad to say. They never really taught us the full functionallity of it when I was in college. The only part of the code you wrote that didn't seem to work for me was this:
txtStr001.Text cStr(Int(Rnd + 1) * 20)

It wanted to return strict values of "20" for everything. As for the super repetitive If/Elseif statements, they are to ensure that the values returned are never less than 1 or more than 20 (if I removed the + 1 at the end it would be 20 or less, but would return values of 0). I truly hate admitting this, but this little project of mine is really showing me how much of a VB noob I really am. So now I'm left with:
Private Sub cmdRollD20_Click()
Dim II As Integer

2 For II = 0 To 17

3      chkStat(II).Value = vbUnchecked

4 Next II

Randomize
txtStr001.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtStr002.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtStr003.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtDex001.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtDex002.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtDex003.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtCon001.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtCon002.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtCon003.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtInt001.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtInt002.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtInt003.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtWis001.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtWis002.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtWis003.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtCha001.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtCha002.Text = CInt((20 - 1 + 1) * Rnd + 1)
txtCha003.Text = CInt((20 - 1 + 1) * Rnd + 1)

If txtStr001.Text >= 20 Then
    txtStr001.Text = 20
End If
If txtStr002.Text >= 20 Then
    txtStr002.Text = 20
End If
If txtStr003.Text >= 20 Then
    txtStr003.Text = 20
End If
If txtDex001.Text >= 20 Then
    txtDex001.Text = 20
End If
If txtDex002.Text >= 20 Then
    txtDex002.Text = 20
End If
If txtDex003.Text >= 20 Then
    txtDex003.Text = 20
End If
If txtCon001.Text >= 20 Then
    txtCon001.Text = 20
End If
If txtCon002.Text >= 20 Then
    txtCon002.Text = 20
End If
If txtCon003.Text >= 20 Then
    txtCon003.Text = 20
End If
If txtInt001.Text >= 20 Then
    txtInt001.Text = 20
End If
If txtInt002.Text >= 20 Then
    txtInt002.Text = 20
End If
If txtInt003.Text >= 20 Then
    txtInt003.Text = 20
End If
If txtWis001.Text >= 20 Then
    txtWis001.Text = 20
End If
If txtWis002.Text >= 20 Then
    txtWis002.Text = 20
End If
If txtWis003.Text >= 20 Then
    txtWis003.Text = 20
End If
If txtCha001.Text >= 20 Then
    txtCha001.Text = 20
End If
If txtCha002.Text >= 20 Then
    txtCha002.Text = 20
End If
If txtCha003.Text >= 20 Then
    txtCha003.Text = 20
End If

End Sub


Thanks for the replies, and more importantly, the help, maj, Bob!



((postscript) I still haven't figured out the CheckBoxes... Go figure...)

This post has been edited by Jaalenn: 22 December 2011 - 12:26 PM

Was This Post Helpful? 0
  • +
  • -

#7 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Re: Using CheckBoxes to Return Variable Data

Posted 22 December 2011 - 02:13 PM

Jaalenn, don't be afraid to have a go at things, and feel free to post back when you have issues.

Well done on spotting the typing mistake in Bob's code....it shows that you made use of Bob's suggestions to spot the error. ;)

As for using loops, take a look at the links below for some more information. They're quite simple once you understand them, but can save you a lot of work as Bob mentioned.

Link 1
Link 2
Was This Post Helpful? 1
  • +
  • -

#8 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

Re: Using CheckBoxes to Return Variable Data

Posted 22 December 2011 - 09:48 PM

Very interesting, maj... I was looking at that first page a few hours ago (coincidence? I think not!). I am almost to the point to abandon my CheckBox idea, and just go with Option Buttons. I already have a good chunk of code for that method elsewhere. Upon request I can upload what I've done.

On a positive note, I'm glad to see that the people here are as helpful and friendly as I remember... It's been around 2 years since my last visit.

Was This Post Helpful? 1
  • +
  • -

#9 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Re: Using CheckBoxes to Return Variable Data

Posted 23 December 2011 - 12:26 AM

We're always friendly, especially when people put some effort in themselves. :)

Anyway, good luck on moving forward. ;)
Was This Post Helpful? 0
  • +
  • -

#10 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

Re: Using CheckBoxes to Return Variable Data

Posted 23 December 2011 - 01:40 AM

Thanks, maj! You've been a real help, and Bob's code examples were great!

Here is the completed, compiled, and ready to run program that I have been working on. I hope it works (crosses fingers).


My Program
Was This Post Helpful? 0
  • +
  • -

#11 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Using CheckBoxes to Return Variable Data

Posted 24 December 2011 - 09:44 AM

Sorry Jalenn,
cStr(Int(Rnd + 1) * 20)
should be
cStr(Int(Rnd * 20) + 1)
Dyslexia in full operation! I didn't have VB6 when I wrote that or I would have tested it. You will find that the correction does indeed give random numbers between 1 and 20, and no others. If you apply that change, you'll still save yourself quite a few lines of code.

p. s. Here's why the original comes up always with 20. Rnd is always a number between 0 and 1, e. g. .58398472. Add 1, you get 1.58398472. The Int function truncates the decimal value, leaving 1. Multiply that by 20 and of course you get 20. So, the corrected version runs this way: Rnd gives you its number, say .58398472 again. Multiply that by 20 and you get 11.6796944. The int function truncates that to 11; adding 1 leaves 12.

Now, the reason you add 1 is because you want a number between 1 and 20. If you didn't add 1, you'd get a number between 0 and 19. For example, what if rnd was .01. Multiplying it by 20 gives .2, which leaves 0 after the int function is applied. On the other end of the scale, if Rnd were .999999, multiplying by 20 would give = 19.99998, which Int would truncate to 19.

Since I led you down the garden path, the least I can do is explain how this works. :)

This post has been edited by BobRodes: 24 December 2011 - 12:06 PM

Was This Post Helpful? 0
  • +
  • -

#12 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

Re: Using CheckBoxes to Return Variable Data

Posted 28 December 2011 - 01:34 AM

And I actually understood that, Bob (as hard as that might be to believe). Thanks again for all the help!
Was This Post Helpful? 0
  • +
  • -

#13 Jaalenn  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 105
  • Joined: 17-November 08

Re: Using CheckBoxes to Return Variable Data

Posted 31 December 2011 - 02:17 AM

The above link to download the program will no longer work (yes, I did this on purpose).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1