Text boxes with an if, driving me crazy
Ok, I have a form is a a user name text box and password text box. Here is the code that I am trying.
public xlogin as varient
public xpassword as varient
If login.value = null then
msgbox " Enter a user name"
end if
If password.value = null then
msgbox "Enter a password"
end if
if login.value and password.value = not null then
xlogin = login
xpassword = password
docmd.close acform, "frm_login"
end if
Ok, so none of it works heh, I get a data type mismatch on the 3rd if and if I remove the 3rd if I dont get any msgboxes if I leave the User and Passworld blank. If I change the .value to .text I get an error message that I can't reference a property or method for a control unless that control has the focus. I tried a could other things but they were just shots in the dark. Thank you for any help that you can offer!
Hizaed
Re: Text boxes with an if, driving me crazy
VB Code:
Dim xlogin As String
Dim xpassword As String
If trim(username.text) = "" Then
MsgBox "Please enter a username"
ElseIf trim(password.text) = "" Then
MsgBox "Please enter a password"
Else
xpassword = password.text
xlogin = login.text
docmd.close acform, "frm_login"
End If
HTH
Re: Text boxes with an if, driving me crazy
Hi,
And, for the record, it's:
If Not(...test in here...) Then
and Variant is spelt with an "a"
zaza
Re: Text boxes with an if, driving me crazy
could also be
VB Code:
If textbox1.text NOT "" Then
Re: Text boxes with an if, driving me crazy
Hmmm I still having the error "Run time error '2185': You can't reference a property or method for a control unless the control has the focus." Here is the exact code that I am using. This is being writting in Access 2000 VBA. Also the user name and password will be used by another form would I need to load them as "Public"? Thanks!
Option Compare Database
Option Explicit
Dim xRMSLogin As String
Dim xRMSPassword As String
Private Sub Command7_Click()
If Trim(RMSLogin.Text) = "" Then
MsgBox "Please enter a username"
ElseIf Trim(RMSPassword.Text) = "" Then
MsgBox "Please enter a password"
Else
xRMSPassword = RMSPassword.Text
xRMSLogin = RMSLogin.Text
DoCmd.Close acForm, "frm_login"
End If
End Sub
Re: Text boxes with an if, driving me crazy
sorry about that...forgot you were using VBA
VB Code:
Dim xRMSLogin As String
Dim xRMSpassword As String
RMSLogin.SetFocus
If trim(RMSLogin.text) = "" Then
MsgBox "Please enter a username"
RMSpassword.SetFocus
ElseIf trim(RMSpassword.text) = "" Then
MsgBox "Please enter a password"
Else
RMSpassword.SetFocus
xRMSpassword = RMSPassword.text
RMSLogin.SetFocus
xRMSLogin = RMSLogin.text
docmd.close acform, "frm_login"
End If
Re: Resolved: Text boxes with an if, driving me crazy
Awsome, thank you so much!
Re: Text boxes with an if, driving me crazy
Ok, Im sorry... for my first test it worked great. If I put a passwrd and no user name I get message that I need enter a user name just like I should. However if I enter a user name but no password I get the "Run time error '2185': You can't reference a property or method for a control unless the control has the focus." Here is the code.
Private Sub cmdDimLogin_Click()
RMSLogin.SetFocus
If Trim(RMSLogin.Text) = "" Then
MsgBox "Please enter a username"
RMSPassword.SetFocus
ElseIf Trim(RMSPassword.Text) = "" Then
MsgBox "Please enter a password"
End If
End Sub
So I messed around with it and if I remove the elseif and make it two separate if statements then it works to a degree. the problem that I am having there is that if my password text box has an input mask of password then I get the error "Microsoft Access cannot retreive the value of this property." But if I remove the input mask it works. Any Ideas? Thanks!
Re: Text boxes with an if, driving me crazy
Quote:
Originally Posted by Hizaed
Ok, Im sorry... for my first test it worked great. If I put a passwrd and no user name I get message that I need enter a user name just like I should. However if I enter a user name but no password I get the "Run time error '2185': You can't reference a property or method for a control unless the control has the focus." Here is the code.
VB Code:
Private Sub cmdDimLogin_Click()
RMSLogin.SetFocus
If Trim(RMSLogin.Text) = "" Then
MsgBox "Please enter a username"
RMSPassword.SetFocus 'this statement should be on a separate line, but won't go there
ElseIf Trim(RMSPassword.Text) = "" Then
MsgBox "Please enter a password"
End If
End Sub
So I messed around with it and if I remove the elseif and make it two separate if statements then it works to a degree. the problem that I am having there is that if my password text box has an input mask of password then I get the error "Microsoft Access cannot retreive the value of this property." But if I remove the input mask it works. Any Ideas? Thanks!
First, you should exit the sub if no username is entered.
VB Code:
Private Sub cmdDimLogin_Click()
RMSLogin.SetFocus
If Trim(RMSLogin.Text) = "" Then
MsgBox "Please enter a username"
'alternative - let them keep trying
'If MsgBox("No username entered. Try again?", vbYesNo) = vbYes then
'Call cmdDimLogin_Click
'Else
'exit sub
'end if
End If
RMSPassword.SetFocus
If Trim(RMSPassword.Text) = "" Then
MsgBox "Please enter a password"
'can add retry like above
exit sub
End If
'do something when they login successfully
End Sub
I'd point out, however, that you can log in successfully with any gobbedlygook for username a/o password except an empty string. There's nothing in your sub to check that it matches valid username/password combinations. You could also consider using the InputBox() function to store the username & password in variables.
Re: Text boxes with an if, driving me crazy
Hi,
Out of interest, does anybody know why it requires the control to have focus before properties can be set? I've never heard of such a thing - surely it is a ridiculous obstacle to put in the way of programming?
zaza
Re: Text boxes with an if, driving me crazy
That's VBA. Annoying but true.