PDA

View Full Version : form question



austenr
07-17-2015, 07:30 AM
I have a login form in the attached WB (UserForm1) that seems to work fine but when you debug it you get an error. Here is the code:


Private Sub cmdSubmit1_Click()


Dim Username As String
Username = txtUserNameIn.Text
Dim password As String
password = txtPasswordIn.Text
'Check to see if data is entered into field: txtUserNameIn
If IsNull(Me.txtUserNameIn) Or Me.txtUserNameIn = "" Then
MsgBox "You must enter your username.", vbOKOnly, "Required Data"
Me.txtUserNameIn.SetFocus
Exit Sub
End If

'Check to see if data is entered into field: txtPasswordIn
If IsNull(Me.txtPasswordIn) Or Me.txtPasswordIn = "" Then
MsgBox "You must enter your Password (case sensitive).", vbOKOnly, "Required Data"
Me.txtPasswordIn.SetFocus
Exit Sub
End If

'Check to see if the Username & Password entered is a valid username in the 'User Register'
'****************
If Username = WorksheetFunction.VLookup(Me.txtUserNameIn.Value, Range("UserRegister"), 1, 0) Then
'****************
If password = WorksheetFunction.VLookup(Me.txtUserNameIn.Value, Range("UserRegister"), 2, 0) Then
Sheets("FilterData").Visible = xlSheetVisible
MsgBox "Password & Username Accepted"
Unload Me
Sheets("FilterData").Select

Else
' Sheets("ADMIN").Visible = xlVeryHidden
MsgBox "Username & Password Combination Not Accepted!!"


End If
End If

End Sub








I get a variable undefined on this line:

Username = txtUserNameIn.Text

An additional problem is that if the form validates the user name and password as valid, I want it to show the FilterData sheet but somehow it flips to the ADMIN sheet. Don't see where that is happening or possible.

Kenneth Hobs
07-17-2015, 08:07 AM
txtUserNameIn does not exist for the Private Sub in your Module which is why it says it is not defined.

Move "Unload Me" to after your selected sheet code.

austenr
07-17-2015, 08:16 AM
Whats the best approach or way around the txtUserNameIn problem?

Kenneth Hobs
07-17-2015, 08:32 AM
Depends. Is the Userform loaded? If so and it is named say UserForm1, then I would do:
password = Userform1.txtPasswordIn.Text

If it is Userform code, I would just move it to the Userform object. If sheet code, then your object reference would depend on the control type, activex or form.