PDA

View Full Version : Check Userform value using a variable name for Userform



Waubain
08-26-2012, 09:10 AM
I am using VBA to automated a host program in Reflections WRQ. I now have 2 subs that have common code, so I was trying to create a separate sub rather than duplicate the code. The problem is that each sub uses a different userform to check the value of checkboxes. I created a simple Userform with 1 checkbox(chkBox1) and a cmd button that hides the Userform
This simplified code works without creating a separate subroutine.

Sub Lab()

frmLab.Show

If frmLab.chkBox1.Value = True Then
MsgBox "CheckBox1 is True", vbOKOnly, "frmLab"
ElseIf frmLab.chkBox1.Value = False Then
MsgBox "CheckBox1 is False", vbOKOnly, "frmLab"
End If

Unload frmLab
End Sub

Sub Drug()

frmDrug.Show

If frmDrug.chkBox1.Value = True Then
MsgBox "CheckBox1 is True", vbOKOnly, "frmDrug"
ElseIf frmDrug.chkBox1.Value = False Then
MsgBox "CheckBox1 is False", vbOKOnly, "frmDrug"
End If

Unload frmDrug
End Sub

This does not work. It seems the VBA.Userforms.Add(userform) pulls in the name but not the control values. I am not receiving any error messages. I tried to declare UF also as a UserForm (same outcome).


Option Explicit
Dim UF as Object

Sub Lab()

frmLab.Show
Set UF = VBA.UserForms.Add("frmLab")

HospitalLocation
Unload frmLab
End Sub

Sub Drug()

frmDrug.Show
Set UF = VBA.UserForms.Add("frmDrug")

HospitalLocation

Unload frmDrug
End Sub


Sub HospitalLocation()

If UF.chkBox1.Value = True Then
MsgBox "CheckBox1 is True", vbOKOnly, UF
ElseIf UF.chkBox1.Value = False Then
MsgBox "CheckBox1 is False", vbOKOnly
End If

End Sub


The Msgbox always returns a False, no matter if chkBox1 is checked or not.

Thoughts? Thank you in advanced.