Results 1 to 15 of 15

Thread: Error #91 Object Variable or with block variable not set

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    8

    Error #91 Object Variable or with block variable not set

    I'm running an access 2003 project and I'm trying to password protect one of my forms but I keep getting that error when I open the form. Once I click OK the form opens that I want to protect. I followed this to the "T". Here is the script I'm using.

    1. Start Access and then open the sample database Northwind.mdb.
    2. Press ALT+F11 to start the Microsoft Visual Basic editor.
    3. On the Insert menu, click Module.
    4. In the module sheet, type the following procedure:Public MyPassword
    VB Code:
    1. Public Function KeyCode(Password As String) As Long
    2.    ' This function will produce a unique key for the
    3.    ' string that is passed in as the Password.
    4.    Dim I As Integer
    5.    Dim Hold As Long
    6.  
    7.    For I = 1 To Len(Password)
    8.       Select Case (Asc(Left(Password, 1)) * I) Mod 4
    9.       Case Is = 0
    10.          Hold = Hold + (Asc(Mid(Password, I, 1)) * I)
    11.       Case Is = 1
    12.          Hold = Hold - (Asc(Mid(Password, I, 1)) * I)
    13.       Case Is = 2
    14.          Hold = Hold + (Asc(Mid(Password, I, 1)) * _
    15.             (I - Asc(Mid(Password, I, 1))))
    16.       Case Is = 3
    17.          Hold = Hold - (Asc(Mid(Password, I, 1)) * _
    18.             (I + Len(Password)))
    19.    End Select
    20.    Next I
    21.    KeyCode = Hold
    22. End Function

    5. Press ALT+F11 to return to Access.
    6. In the Database window, under Objects, click Tables, and then click New.
    7. In the New Table dialog box, double-click Design View.
    8. Create a new table as follows: Table: tblPassword
    ---------------------------
    Field Name: ObjectName
    Data Type: Text
    Field Size: 50
    Field Name: KeyCode
    Data Type: Text
    Field Size: 25
    Input Mask: Password

    Table Properties: tblPassword
    -----------------------------
    PrimaryKey: ObjectName


    9. Open the tblPassword table and then enter the following data: ObjectName: Orders
    KeyCode: 2818


    10. Create a new form in design view and save the form as frmPassword.
    11. Add a single textbox to frmPassword called Text0, and a command button called CheckPassword.
    12. Set the Input Mask property of Text0 to "PASSWORD" (minus the quotation marks).
    13. Add the following code to the OnClick Event of the CheckPassword button and then save the form:
    VB Code:
    1. If IsNull(Forms!frmPassword!Text0.Value) Then
    2.                  MsgBox "You cannot enter a blank Password. Try again."
    3.                  Me!Text0.SetFocus
    4.             Else
    5.                  MyPassword = Me!Text0.Value
    6.                  DoCmd.Close acForm, "frmPassword"
    7.             End If

    14. Open the Orders form in Design view.
    15. If the property sheet is not visible, click Properties on the View menu.
    16. Type the following event procedure in the module for the OnOpen property of the form:Private Sub Form_Open(Cancel as Integer)
    VB Code:
    1. Dim Hold As Variant
    2.    Dim tmpKey As Long
    3.    Dim I As Integer
    4.    Dim rs As DAO.Recordset
    5.    Dim db As DAO.Database
    6.  
    7.    On Error GoTo Error_Handler
    8.    ' Prompt the user for the Password.
    9.    DoCmd.OpenForm "frmPassword", acNormal, , , , acDialog
    10.             Hold = MyPassword
    11. ' Open the table that contains the password.
    12.    Set db = CurrentDb
    13.    Set rs = db.OpenRecordset("tblPassword", dbOpenTable)
    14.    rs.Index = "PrimaryKey"
    15.    rs.Seek "=", Me.Name
    16.    If rs.NoMatch Then
    17.       MsgBox "Sorry cannot find password information. Try Again"
    18.       Cancel = -1
    19.    Else
    20.       ' Test to see if the key generated matches the key in
    21.       ' the table; if there is not a match, stop the form
    22.       ' from opening.
    23.       If Not (rs![keycode] = KeyCode(Cstr(Hold))) Then
    24.          MsgBox "Sorry you entered the wrong password." & _
    25.             "Try again.", vbOKOnly, "Incorrect Password"
    26.          Cancel = -1
    27.       End If
    28.    End If
    29.    rs.Close
    30.    db.Close
    31.    Exit Sub
    32.  
    33. Error_Handler:
    34.    MsgBox Err.Description, vbOKOnly, "Error #" & Err.Number
    35.    Exit Sub
    End Sub
    Last edited by MartinLiss; Mar 10th, 2006 at 07:10 PM.

  2. #2

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

    Re: Error #91 Object Variable or with block variable not set

    Welcome to the Forums

    Can you tell us on which line you are getting the error? When the error message appears, push Ctrl-Break to go to the debugger and look at which line you are at. It will be on this line that the error is occurring.
    Another trick is to put a line like:

    VB Code:
    1. Msgbox("Cobblers")

    as the first line in your Open proc. Again, you can then push Ctrl-Break to ge the debugger up and step through line by line (using F8) to see where the error occurs.

    Offhand, I can't see where you've defined DoCmd, for example. It will be a problem like this which is causing the error. Try it and see what happens.


    zaza

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    8

    Re: Error #91 Object Variable or with block variable not set

    Here is the script that I'm using: Private Sub Form_Open(Cancel As Integer)
    Dim Hold As Variant
    Dim tmpKey As Long
    Dim I As Integer
    Dim rs As DAO.Recordset
    Dim db As DAO.Database

    On Error GoTo Error_Handler
    ' Prompt the user for the Password.
    DoCmd.OpenForm "frmPassword", acNormal, , , , acDialog
    Hold = MyPassword
    ' Open the table that contains the password.
    Set db = CurrentDb
    Set rs = db.OpenRecordset("tblPassword", dbOpenTable)
    rs.Index = "PrimaryKey"
    rs.Seek "=", Me.Name
    If rs.NoMatch Then
    MsgBox "Sorry cannot find password information. Try Again"
    Cancel = -1
    Else
    ' Test to see if the key generated matches the key in
    ' the table; if there is not a match, stop the form
    ' from opening.
    If Not (rs![KeyCode] = KeyCode(CStr(Hold))) Then
    MsgBox "Sorry you entered the wrong password." & _
    "Try again.", vbOKOnly, "Incorrect Password"
    Cancel = -1
    End If
    End If
    rs.Close
    db.Close
    Exit Sub

    Error_Handler:
    MsgBox Err.Description, vbOKOnly, "Error #" & Err.Number
    Exit Sub (The error is here. If I delete it then the error is the next Line)
    End Sub

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

    Re: Error #91 Object Variable or with block variable not set

    Ah, I neglected to spot your error handler. Comment out the error handling line:

    On Error GoTo Error_Handler

    and then the actual error line will become apparent...

    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    8

    Re: Error #91 Object Variable or with block variable not set

    Thanks. It took care of that error but now I have another. The error is in bold.

    Private Sub Form_Open(Cancel As Integer)
    Dim Hold As Variant
    Dim tmpKey As Long
    Dim I As Integer
    Dim rs As DAO.Recordset
    Dim db As DAO.Database

    ' On Error GoTo Error_Handler
    ' Prompt the user for the Password.
    DoCmd.OpenForm "frmPassword", acNormal, , , , acDialog
    Hold = MyPassword
    ' Open the table that contains the password.
    Set db = CurrentDb
    Set rs = db.OpenRecordset("tblPassword", dbOpenTable)
    rs.Index = "PrimaryKey"
    rs.Seek "=", Me.Name
    If rs.NoMatch Then
    MsgBox "You entered the wrong password. Please try Again"
    Cancel = -1
    Else
    ' Test to see if the key generated matches the key in
    ' the table; if there is not a match, stop the form
    ' from opening.
    If Not (rs![KeyCode] = KeyCode(CStr(Hold))) Then
    MsgBox "Sorry you entered the wrong password." & _
    "Try again.", vbOKOnly, "Incorrect Password"
    Cancel = -1
    End If
    End If
    rs.Close
    db.Close
    Exit Sub

    Error_Handler:
    MsgBox Err.Description, vbOKOnly, "Error #" & Err.Number
    Exit Sub
    End Sub

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

    Re: Error #91 Object Variable or with block variable not set

    Hover your mouse over db when the error has occurred. I bet it says "db = Nothing". Where have you set CurrentDb?
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    8

    Re: Error #91 Object Variable or with block variable not set

    Nope. It says: dbOpenTable =1

    I don't understand what you mean as to where I set currentdb.

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

    Re: Error #91 Object Variable or with block variable not set

    No, not dbopentable. db. On the previous line, you Set db = CurrentDb. What is current db, and where are you getting it from? Since this is the form open event, you'd better have defined it somewhere...
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    8

    Re: Error #91 Object Variable or with block variable not set

    This is what I used to setup the password protection on my form:

    1. Start Access and then open the sample database Northwind.mdb.
    2. Press ALT+F11 to start the Microsoft Visual Basic editor.
    3. On the Insert menu, click Module.
    4. In the module sheet, type the following procedure:Public MyPassword
    Public Function KeyCode(Password As String) As Long
    ' This function will produce a unique key for the
    ' string that is passed in as the Password.
    Dim I As Integer
    Dim Hold As Long

    For I = 1 To Len(Password)
    Select Case (Asc(Left(Password, 1)) * I) Mod 4
    Case Is = 0
    Hold = Hold + (Asc(Mid(Password, I, 1)) * I)
    Case Is = 1
    Hold = Hold - (Asc(Mid(Password, I, 1)) * I)
    Case Is = 2
    Hold = Hold + (Asc(Mid(Password, I, 1)) * _
    (I - Asc(Mid(Password, I, 1))))
    Case Is = 3
    Hold = Hold - (Asc(Mid(Password, I, 1)) * _
    (I + Len(Password)))
    End Select
    Next I
    KeyCode = Hold
    End Function


    5. Press ALT+F11 to return to Access.
    6. In the Database window, under Objects, click Tables, and then click New.
    7. In the New Table dialog box, double-click Design View.
    8. Create a new table as follows: Table: tblPassword
    ---------------------------
    Field Name: ObjectName
    Data Type: Text
    Field Size: 50
    Field Name: KeyCode
    Data Type: Text
    Field Size: 25
    Input Mask: Password

    Table Properties: tblPassword
    -----------------------------
    PrimaryKey: ObjectName


    9. Open the tblPassword table and then enter the following data: ObjectName: Orders
    KeyCode: 2818


    10. Create a new form in design view and save the form as frmPassword.
    11. Add a single textbox to frmPassword called Text0, and a command button called CheckPassword.
    12. Set the Input Mask property of Text0 to "PASSWORD" (minus the quotation marks).
    13. Add the following code to the OnClick Event of the CheckPassword button and then save the form:If IsNull(Forms!frmPassword!Text0.Value) Then
    MsgBox "You cannot enter a blank Password. Try again."
    Me!Text0.SetFocus
    Else
    MyPassword = Me!Text0.Value
    DoCmd.Close acForm, "frmPassword"
    End If


    14. Open the Orders form in Design view.
    15. If the property sheet is not visible, click Properties on the View menu.
    16. Type the following event procedure in the module for the OnOpen property of the form:Private Sub Form_Open(Cancel as Integer)
    Dim Hold As Variant
    Dim tmpKey As Long
    Dim I As Integer
    Dim rs As DAO.Recordset
    Dim db As DAO.Database

    On Error GoTo Error_Handler
    ' Prompt the user for the Password.
    DoCmd.OpenForm "frmPassword", acNormal, , , , acDialog
    Hold = MyPassword
    ' Open the table that contains the password.
    Set db = CurrentDb
    Set rs = db.OpenRecordset("tblPassword", dbOpenTable)
    rs.Index = "PrimaryKey"
    rs.Seek "=", Me.Name
    If rs.NoMatch Then
    MsgBox "Sorry cannot find password information. Try Again"
    Cancel = -1
    Else
    ' Test to see if the key generated matches the key in
    ' the table; if there is not a match, stop the form
    ' from opening.
    If Not (rs![keycode] = KeyCode(Cstr(Hold))) Then
    MsgBox "Sorry you entered the wrong password." & _
    "Try again.", vbOKOnly, "Incorrect Password"
    Cancel = -1
    End If
    End If
    rs.Close
    db.Close
    Exit Sub

    Error_Handler:
    MsgBox Err.Description, vbOKOnly, "Error #" & Err.Number
    Exit Sub
    End Sub

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

    Re: Error #91 Object Variable or with block variable not set

    The object variable Not set error on that line almost certainly refers to the db. If, when you get the error, you hover the mouse over "CurrentDb" on the previous line, does it say "Nothing"?
    If you right-click "CurrentDb" and choose Definition, does it go anywhere? If you do a search for "CurrentDb" in the current project (Edit, Find menu), does it come up anywhere else? I can only think that this CurrentDb variable is not being allocated somewhere.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    8

    Re: Error #91 Object Variable or with block variable not set

    When I hover over "Set db = CurrentDb" it does say db=nothing. When I right click on Set db = CurrentDb it goes to object browser saying currentDB on the Members of "Application" but that's about it. I hope that is what you are asking for.

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

    Re: Error #91 Object Variable or with block variable not set

    Can you do a search for CurrentDb in the entire project? Where, apart from there, does it come up? You are attempting to set the database in this piece of code to whatever database is being referenced by CurrentDb. If you have not set CurrentDb to be anything, then it will not work.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  14. #14

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    8

    Re: Error #91 Object Variable or with block variable not set

    I checked the entire project and CurrentDb is only referenced once. How would I setup the CurrentDb to reference to the name of the database? The form does comes up after the error.

  15. #15

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    8

    Re: Error #91 Object Variable or with block variable not set

    Here is some more info. I wrote in DOA infront of the recordset and database to keep it from erroring out on:

    If rs.NoMatch Then
    MsgBox "Invalid Password. Try Again"

    When I hover over recordset and database it shows rs = nothing and db = nothing and when I hover over (If rs.NoMatch Then) it shows:

    rs.noMatch = <objectvariable or with block variable not set>

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