4 Replies - 846 Views - Last Post: 30 June 2013 - 08:54 PM Rate Topic: -----

#1 daymickcorr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-June 13

error handling

Posted 26 June 2013 - 02:18 PM

when you enter the bad username or password it tells you that youve made an error but when i try to enter back a new username that is good it still gives me the error but it works if i havent writen a bad username or password the first time what can i do so i can reenter the username and password without recieving my error message

Private Sub cmdLogin_Click()
On Error GoTo errorhandler
Do Until txtUsername.Text = Form3.Data1.Recordset.Fields("username") And txtPassword.Text = Form3.Data1.Recordset.Fields("password")
Form3.Data1.Recordset.MoveNext
Loop
Form1.Show
Unload Form2
Unload Form3
Exit Sub
errorhandler:
MsgBox ("Wrong username or password")
txtUsername.Text = ""
txtPassword.Text = ""
End Sub


This post has been edited by macosxnerd101: 26 June 2013 - 03:27 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: error handling

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: error handling

Posted 26 June 2013 - 03:33 PM

Your description contains no punctuation, or sentences. It is extremely difficult to read.

Anyway, you are using MoveNext but at no point do you reset the point to the first row - MoveFirst.

Note that you shouldn't be using error-handling for this, you should be checking the recordset's EOF property to discover that you have reached the end of the recordset.

This post has been edited by andrewsw: 26 June 2013 - 03:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: error handling

Posted 29 June 2013 - 09:53 PM

The error handler approach is clumsy, but no clumsier than the rest of the approach. To fix it, you have to reset the cursor back to the top of the recordset as Andy mentions, and you also have to leave the error handler:
Private Sub cmdLogin_Click()
On Error GoTo errorhandler
:startOver
Do Until txtUsername.Text = Form3.Data1.Recordset.Fields("username") And txtPassword.Text = Form3.Data1.Recordset.Fields("password")
Form3.Data1.Recordset.MoveNext
Loop
Form1.Show
Unload Form2
Unload Form3
Exit Sub
errorhandler:
MsgBox ("Wrong username or password")
txtUsername.Text = ""
txtPassword.Text = ""
Form3.Data1.Recordset.MoveFirst
Resume startOver
End Sub


You will notice that I have added 3 lines to your code. Now, the not clumsy way to do this is:
Private Sub cmdLogin_Click()
With Form3.Data1.Recordset
     .Filter = "username = '" & txtUsername.Text & "' and password = '" & txtPassword.Text
     If .BOF And .EOF Then
        MsgBox ("Wrong username or password")
        txtUsername.Text = ""
        txtPassword.Text = ""
        Exit Sub
    End If
End With
Form1.Show
Unload Form2
Unload Form3
Exit Sub


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,008
  • Joined: 12-December 12

Re: error handling

Posted 30 June 2013 - 03:17 AM

@BobRodes
That should be startOver: on line 3 and the call Resume startOver causes an endless loop.

This post has been edited by andrewsw: 30 June 2013 - 03:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: error handling

Posted 30 June 2013 - 08:54 PM

View Postandrewsw, on 30 June 2013 - 05:17 AM, said:

@BobRodes
That should be startOver: on line 3 and the call Resume startOver causes an endless loop.

Dyslexia in full operation; thanks Andy. And you're quite right; my solution causes an endless loop. :blush: You just need to put the MoveFirst in there as you say. Midnight Saturday...must have been dreaming.

However, if you handle the error when you attempt to do a movenext when eof is true, you use less code overhead than checking eof each time you go through the loop. So it's more efficient to use the error handler. Of course, using the filter is still more efficient.

This post has been edited by BobRodes: 30 June 2013 - 08:55 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1