Results 1 to 11 of 11

Thread: Text boxes with an if, driving me crazy

  1. #1

    Thread Starter
    Member
    Join Date
    May 2005
    Posts
    56

    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
    Last edited by Hizaed; Jul 15th, 2005 at 04:27 PM.

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Text boxes with an if, driving me crazy

    VB Code:
    1. Dim xlogin As String
    2. Dim xpassword As String
    3.  
    4. If trim(username.text) = "" Then
    5.      MsgBox "Please enter a username"
    6. ElseIf trim(password.text) = "" Then
    7.      MsgBox "Please enter a password"
    8. Else
    9.      xpassword = password.text
    10.      xlogin = login.text
    11.      docmd.close acform, "frm_login"
    12. End If

    HTH
    Last edited by kfcSmitty; Jul 14th, 2005 at 01:16 PM.

  3. #3
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    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

  4. #4
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Text boxes with an if, driving me crazy

    could also be
    VB Code:
    1. If textbox1.text NOT "" Then

  5. #5

    Thread Starter
    Member
    Join Date
    May 2005
    Posts
    56

    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

  6. #6
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Text boxes with an if, driving me crazy

    sorry about that...forgot you were using VBA

    VB Code:
    1. Dim xRMSLogin As String
    2. Dim xRMSpassword As String
    3.      RMSLogin.SetFocus
    4. If trim(RMSLogin.text) = "" Then
    5.      MsgBox "Please enter a username"
    6.      RMSpassword.SetFocus
    7. ElseIf trim(RMSpassword.text) = "" Then
    8.      MsgBox "Please enter a password"
    9. Else
    10.      RMSpassword.SetFocus
    11.      xRMSpassword = RMSPassword.text
    12.      RMSLogin.SetFocus
    13.      xRMSLogin = RMSLogin.text
    14.      docmd.close acform, "frm_login"
    15. End If

  7. #7

    Thread Starter
    Member
    Join Date
    May 2005
    Posts
    56

    Re: Resolved: Text boxes with an if, driving me crazy

    Awsome, thank you so much!
    Last edited by Hizaed; Jul 15th, 2005 at 04:27 PM.

  8. #8

    Thread Starter
    Member
    Join Date
    May 2005
    Posts
    56

    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!

  9. #9
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    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:
    1. Private Sub cmdDimLogin_Click()
    2.  
    3.  
    4.      RMSLogin.SetFocus
    5. If Trim(RMSLogin.Text) = "" Then
    6.      MsgBox "Please enter a username"
    7.  
    8.  
    9.      RMSPassword.SetFocus  'this statement should be on a separate line, but won't go there
    10. ElseIf Trim(RMSPassword.Text) = "" Then
    11.      MsgBox "Please enter a password"
    12. End If
    13.  
    14. 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:
    1. Private Sub cmdDimLogin_Click()
    2.      RMSLogin.SetFocus
    3.      If Trim(RMSLogin.Text) = "" Then
    4.         MsgBox "Please enter a username"
    5.         'alternative - let them keep trying
    6.         'If MsgBox("No username entered. Try again?", vbYesNo) = vbYes then
    7.              'Call cmdDimLogin_Click
    8.         'Else
    9.              'exit sub
    10.         'end if
    11.      End If
    12.  
    13.    
    14.      RMSPassword.SetFocus
    15.      If Trim(RMSPassword.Text) = "" Then
    16.         MsgBox "Please enter a password"
    17.         'can add retry like above
    18.         exit sub
    19.      End If
    20.      'do something when they login successfully
    21. 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.
    Last edited by salvelinus; Jul 15th, 2005 at 07:00 PM.
    Tengo mas preguntas que contestas

  10. #10
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    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

  11. #11
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Text boxes with an if, driving me crazy

    That's VBA. Annoying but true.
    Tengo mas preguntas que contestas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width