Why Checkbox count = 4

G

Guest

attempting to better understand the use of TypeOf I have created:
All message boxs return the proper number, except the CkBoxct,
which returns 4, when IN FACT there is only 1 Checkbox on my Userform1.
Any ideas why this problem?
TIA,
Jim May

Private Sub CommandButton1_Click()
For Each ctl In UserForm1.Controls
If TypeOf ctl Is MSForms.Label Then labelct = labelct + 1
If TypeOf ctl Is MSForms.Frame Then Framect = Framect + 1
If TypeOf ctl Is MSForms.CheckBox Then CkBoxct = CkBoxct + 1
If TypeOf ctl Is MSForms.TextBox Then Txtct = Txtct + 1
If TypeOf ctl Is MSForms.OptionButton Then OptBnct = OptBnct + 1
If TypeOf ctl Is MSForms.ComboBox Then ComBoxct = ComBoxct + 1
Next ctl
MsgBox "labels total " & labelct
MsgBox "Frame total " & Framect
MsgBox "Checkboxes total " & CkBoxct
MsgBox "Textboxs total " & Txtct
MsgBox "OptionButtons total " & OptBnct
MsgBox "ComboBoxs total " & ComBoxct
End Sub
 
M

merjet

Your code worked fine for me. Are you sure you don't have Checkbox's
lying on top of one another such that it appears there is only one?

Hth,
Merjet
 
J

JE McGimpsey

At the risk of offending, are you sure that there aren't checkboxes
hidden by other controls (perhaps frame(s)?)?

If you execute

?UserForm1.Controls.Count

in the immediate window, do you get the correct total?
 
G

Guest

JE,
When I (in the immediate window) enter your suggested

?UserForm1.Controls.Count

I get the right total count -- But still a Checkbox Overcount;
I'm still working on.. I'll report back if I CRACK..

No Checkboxes ARE HIDDEN

Tks,

Jim
 
D

Dave Peterson

How did you declare ckboxct?

Is it accumulating each time you run the procedure?
 
G

Guest

Add a debugging statement

Private Sub CommandButton1_Click()
For Each ctl In UserForm1.Controls
If TypeOf ctl Is MSForms.Label Then labelct = labelct + 1
If TypeOf ctl Is MSForms.Frame Then Framect = Framect + 1
If TypeOf ctl Is MSForms.CheckBox Then
CkBoxct = CkBoxct + 1
debug.print CkBoxct, ctl.name
end if
If TypeOf ctl Is MSForms.TextBox Then Txtct = Txtct + 1
If TypeOf ctl Is MSForms.OptionButton Then OptBnct = OptBnct + 1
If TypeOf ctl Is MSForms.ComboBox Then ComBoxct = ComBoxct + 1
Next ctl
MsgBox "labels total " & labelct
MsgBox "Frame total " & Framect
MsgBox "Checkboxes total " & CkBoxct
MsgBox "Textboxs total " & Txtct
MsgBox "OptionButtons total " & OptBnct
MsgBox "ComboBoxs total " & ComBoxct
End Sub


See the results in the immediate window in the VBE (view=>Immediate window
if it isn't already visible)
 
G

Guest

Thanks Tom:

Your code produces in my immediate window:

1 OptionButton1
2 OptionButton2
3 CheckBox1

And my Msgbox for Checkboxes indicates 3;

But there is ONLY 1 !!
 
G

Guest

My ONLY two OptionButtons ARE INSIDE my Frame Control
With my Userform active, when I click on the Properties Drop-down
I do not see my 2 optionbuttons,,, hummmmmmm -- these are
being counted as a checkbox (no doubt) .... hummmm
 
M

merjet

I affirm your experience. I put 2 OptionButtons in a Frame and they
were miscounted as CheckBoxes. However, using the following gave the
correct count.

For Each ctl In UserForm1.Controls
If TypeName(ctl) = "CheckBox" Then CkBoxct = CkBoxct + 1
Next ctl

Hth,
Merjet
 
G

Guest

Yeah, The following works fine -- WITHOUT A PROBLEM

Thanks for your input..

Private Sub CommandButton2_Click()
For Each ctl In UserForm1.Controls
If TypeName(ctl) = "Label" Then labelct = labelct + 1
If TypeName(ctl) = "Frame" Then Framect = Framect + 1
If TypeName(ctl) = "CheckBox" Then
CkBoxct = CkBoxct + 1
'Debug.Print CkBoxct, ctl.Name
End If
If TypeName(ctl) = "TextBox" Then Txtct = Txtct + 1
If TypeName(ctl) = "OptionButton" Then OptBnct = OptBnct + 1
If TypeName(ctl) = "ComboBox" Then ComBoxct = ComBoxct + 1
Next ctl
MsgBox "labels total " & labelct
MsgBox "Frame total " & Framect
MsgBox "Checkboxes total " & CkBoxct
MsgBox "Textboxs total " & Txtct
MsgBox "OptionButtons total " & OptBnct
MsgBox "ComboBoxs total " & ComBoxct
End Sub
 
G

Guest

I deleted 1 of the original 3 optionbuttons I had at the time
I typed in the Subject line
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top