By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,688 Members | 1,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,688 IT Pros & Developers. It's quick & easy.

Object variable or With block variable not set error

P: 1
Dear Sir,

i have used the following code to verify password. i am getting "Object variable or With block variable not set error" whie opening the next form after giving correct password. please help me.

Code:
Option Compare Database
Option Explicit

Private Sub Command4_Click()
On Error GoTo Err_Command4_Click

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim stDocName, stLinkCriteria As String
Set db = CurrentDb
Set rs = db.OpenRecordset("tabPassword", dbOpenTable)

Do While Not rs.EOF

If Me!UserName.Value = rs!UserName And Me!Password.Value = rs!Password Then
DoCmd.Close
stDocName = "frmMainMenu"
DoCmd.OpenForm stDocName, , , stLinkCriteria
rs.Close
Set rs = Nothing
End If
rs.MoveNext
Loop

UserName.Value = Empty
Password.Value = Empty

MsgBox "Invalid User Name or Password " & _
"Try again.", vbOKOnly, "Incorrect Password"

Exit_Command4_Click:
Exit Sub

Err_Command4_Click:
MsgBox Err.Description
Resume Exit_Command4_Click

End Sub
May 24 '07 #1
Share this Question
Share on Google+
1 Reply

puppydogbuddy
Expert 100+
P: 1,923
Dear Sir,

i have used the following code to verify password. i am getting "Object variable or With block variable not set error" whie opening the next form after giving correct password. please help me.

Code:
Option Compare Database
Option Explicit

Private Sub Command4_Click()
On Error GoTo Err_Command4_Click

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim stDocName, stLinkCriteria As String
Set db = CurrentDb
Set rs = db.OpenRecordset("tabPassword", dbOpenTable)

Do While Not rs.EOF

If Me!UserName.Value = rs!UserName And Me!Password.Value = rs!Password Then
DoCmd.Close
stDocName = "frmMainMenu"
DoCmd.OpenForm stDocName, , , stLinkCriteria
rs.Close
Set rs = Nothing
End If
rs.MoveNext
Loop

UserName.Value = Empty
Password.Value = Empty

MsgBox "Invalid User Name or Password " & _
"Try again.", vbOKOnly, "Incorrect Password"

Exit_Command4_Click:
Exit Sub

Err_Command4_Click:
MsgBox Err.Description
Resume Exit_Command4_Click

End Sub
Among other things, you are closing out your recordset in the middle of your loop code. Try revising the loop portion of your code as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Do While Not rs.EOF
  3.  
  4. If Me!UserName.Value = rs!UserName And Me!Password.Value = rs!Password Then
  5.  
  6. DoCmd.Close
  7. stDocName = "frmMainMenu"
  8. DoCmd.OpenForm stDocName, , , stLinkCriteria
  9.  
  10. Else
  11.  
  12. MsgBox "Invalid User Name or Password " & _
  13. "Try again.", vbOKOnly, "Incorrect Password"
  14. UserName.Value = Empty
  15. Password.Value = Empty
  16. rs.MoveNext
  17.  
  18. End If
  19. Loop
  20.  
  21. rs.Close
  22. Set rs = Nothing
  23.  
May 24 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.