Log in

View Full Version : Userform problem [Message box for missing entry in opt and chk button]



ranthrave
05-17-2013, 11:08 AM
Good day sir/madam!
Im using windows 7 excel 2010 and save it as xlsm

Kindly help me on my code




If (optAAA.Value) = "" Then
ElseIf (optBBB.Value) = "" Then
ElseIf (optCCC.Value) = "" Then
Else
optAAA.SetFocus
MsgBox "Pls choose product"
Exit Sub
End If


I try using this code but I got stuck on the option button maybe I'm missing something.

I also had a problem on putting a checkbox code in 'testing incomplete entry.

ORIGINAL CODE



Private Sub cmdOK_Click()
ActiveWorkbook.Sheets("Sheet1").Activate
Range("B5").Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True

'Test for incomplete entry
If (txtDate.Value) = "" Then
txtDate.SetFocus
MsgBox "Please enter a date of exit"
Exit Sub

End If
If (cboBranch.Value) = "" Then
cboBranch.SetFocus
MsgBox "Please enter a branch"
Exit Sub
End If

If (optAAA.Value) = "" Then
ElseIf (optBBB.Value) = "" Then
ElseIf (optCCC.Value) = "" Then
Else
optAAA.SetFocus
MsgBox "Pls choose a product"
Exit Sub
End If


ActiveCell.Value = txtDate.Value
ActiveCell.Offset(0, 1) = cboBranch.Value
ActiveCell.Offset(0, 2) = txtSName.Value
ActiveCell.Offset(0, 3) = txtFName.Value
ActiveCell.Offset(0, 4) = txtMI.Value
ActiveCell.Offset(0, 5) = txtCID.Value


If optAAA = True Then
ActiveCell.Offset(0, 6).Value = "AAA"
ElseIf optBBB = True Then
ActiveCell.Offset(0, 6).Value = "BBB"
ElseIf optCCC = True Then
ActiveCell.Offset(0, 6).Value = "CCC"
Else
ActiveCell.Offset(0, 6).Value = ""
End If


If chkXXX = True Then
ActiveCell.Offset(0, 7).Value = "XXX"
Else
ActiveCell.Offset(0, 7).Value = ""
End If
If chkYYY = True Then
ActiveCell.Offset(0, 8).Value = "YYY"
Else
ActiveCell.Offset(0, 8).Value = ""
End If
If chkZZZ = True Then
ActiveCell.Offset(0, 9).Value = "ZZZ"
Else
ActiveCell.Offset(0, 9).Value = ""
End If


'clearing forms
Range("B5").Select
txtDate.Value = ""
cboBranch.Value = ""
txtSName.Value = ""
txtFName.Value = ""
txtMI.Value = ""
txtCID.Value = ""
optAAA = False
optBBB = False
optCCC = False
chkXXX = False
chkYYY = False
chkZZZ = False
cmdExtra.SetFocus

End Sub




Thank You Very Much

Excel Fox
05-17-2013, 01:22 PM
Are the controls activex controls or form controls? If you are not sure, just mention whether you are seeing
=EMBED("Forms.XXX.1","") in the formula bar when you select the control? where XXX can be OptionButton or ComboBox

ranthrave
05-20-2013, 10:43 AM
Are the controls activex controls or form controls?

BUMP

VBA user form control

Admin
05-20-2013, 12:32 PM
Hi

Option Button value give either TRUE or FALSE and not ""

so try


If Not (optAAA.Value) + (optBBB.Value) + (optCCC.Value) Then
optCCC.SetFocus
MsgBox "Pls choose product"
Exit Sub
End If

ranthrave
05-20-2013, 02:45 PM
Thank you very much sir your code works fine

Heres the code I use before and put 'Test for incomplete entry

But I will use your code


If optAAA = True Then
ActiveCell.Offset(0, 6).Value = "AAA"
ElseIf optBBB = True Then
ActiveCell.Offset(0, 6).Value = "BBB"
ElseIf optCCC = True Then
ActiveCell.Offset(0, 6).Value = "CCC"
Else
ActiveCell.Offset(0, 6).Value = ""
fraProduct.SetFocus
MsgBox "Pls Choose product"
Exit Sub
End If




How about in checkbox? Any ideas sir

ranthrave
05-20-2013, 02:49 PM
Hi

Option Button value give either TRUE or FALSE and not ""




Sir Admin Based on your code it seems to be the and Checkbox Button also works but only for 1 box.
Assuming 2 checkbox or 3 checkbox will be tick

Kindly help me

Admin
05-20-2013, 03:38 PM
Hi

Not sure about the requirement. Are you looking something like ?


Dim Ctrls, i As Long

Ctrls = Array(CheckBox1, CheckBox2, CheckBox3)

For i = LBound(Ctrls) To UBound(Ctrls)
If Ctrls(i).Value Then 'if TRUE
ActiveCell.Offset(, i + 1) = "Some Value"
Else
'unchecked
End If
Next

ranthrave
05-21-2013, 05:21 AM
Sir
I'm looking something similar to your previous code


If Not (optAAA.Value) + (optBBB.Value) + (optCCC.Value) Then
optCCC.SetFocus
MsgBox "Pls choose product"
Exit Sub
End If


Similar case, but instead of option button I will use checked button



If Not (chkXXX.Value) + (chkYYY.Value) + (chkZZZ.Value) Then
chkXXX.SetFocus
MsgBox "Pls choose 2 or 3 brand"
Exit Sub
End If

ranthrave
05-21-2013, 06:34 AM
I forgot the "Else". %D

Anyway just wanted to say Many Thanks sir Admin for your time and solutions