Results 1 to 3 of 3

Thread: Database Login

  1. #1

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Database Login

    I am creating a login routine that will check the username and password in table. I have managed to get it to work. The problem is i have used the .Find method to find the Username, if the username exists in the database and the password is the same in the record it will login, however, if the username is incorrect then and error message is generated (See below). Is there a more efficient way to create a login routine for this database via VB? Otherwise, how would i check to see if the username is actually in the database?

    Error

    Run-time error '3021':

    Either BOF or EOF is true, or the current record has been deleted. Requested operation requires a current record.
    Code:
    Private Sub Command1_Click()
        With adoRecordSet
            If .EOF Or .BOF Then
                .MoveFirst
                .MoveNext
            Else
                ' What if the Username is incorrect?
                .Find "Username = '" & Text1.Text & "'", , adSearchForward
                If Text2.Text = .Fields("Password") Then
                    strUsername = Text1.Text
                    Form2.Show
                    Unload Me
                Else
                    MsgBox "The Username and / or Password is incorrect. Please" & vbCrLf _
                        & "try again.", vbExclamation, "Login Error"
                End If
            End If
        End With
    End Sub
    Regards,



    Jenova

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Database Login

    If Not .EOF or .BOF is one way.

    Error trapping the 3021 error is another.

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Database Login

    Code:
    'Assume ADO connection object already setup
    Dim rs As ADODB.Recordset
    
    Set rs = cn.Execute ("SELECT COUNT(*) AS ret_cnt FROM tblUsers WHERE UserName = '" & Text1.Text & "' AND Password = '" & Text2.Text & "' ")
    If rs.Fields("ret_cnt").Value = 1 Then
       Form2.Show
    Else
     MsgBox "The Username and / or Password is incorrect. Please" & vbCrLf _
                        & "try again.", vbExclamation, "Login Error"
    End If
    rs.Close
    Set rs = Nothing

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