Password Code Error: Compile Error: Sub or Function not defined

N

Nikki

Hi,

I followed instructions on how to create a password on a form but when i
went to test it the following error came up:

Compile Error: Sub or Function not defined

Below is the code I entered as the event procedure for OnOpen for my form
called 'Add' . Can someone see how I can change it to make this work?

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

On Error GoTo Error_Handler
' Check to see if the user is passing in the Password.
Hold = InputBox("Please Enter Your Password", "Enter Password")
' 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 info. 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. " & _
"Please 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
 
N

Nikki

I just found the page that had that extra part on. Trying that now.

Jeff Conrad said:
Did you set up the module code as well per the KB article?

--
Jeff Conrad
Access Junkie - MVP
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:
Hi,

I followed instructions on how to create a password on a form but when i
went to test it the following error came up:

Compile Error: Sub or Function not defined

Below is the code I entered as the event procedure for OnOpen for my form
called 'Add' . Can someone see how I can change it to make this work?

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

On Error GoTo Error_Handler
' Check to see if the user is passing in the Password.
Hold = InputBox("Please Enter Your Password", "Enter Password")
' 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 info. 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. " & _
"Please 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
 
N

Nikki

Now when i try it the following error comes up:

Runtime error: '438'
Object does not support this property or method

When I click Debug it takes me to this:

Private Sub CheckPassword_Click()
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
End Sub

Any ideas?

Jeff Conrad said:
Did you set up the module code as well per the KB article?

--
Jeff Conrad
Access Junkie - MVP
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:
Hi,

I followed instructions on how to create a password on a form but when i
went to test it the following error came up:

Compile Error: Sub or Function not defined

Below is the code I entered as the event procedure for OnOpen for my form
called 'Add' . Can someone see how I can change it to make this work?

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

On Error GoTo Error_Handler
' Check to see if the user is passing in the Password.
Hold = InputBox("Please Enter Your Password", "Enter Password")
' 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 info. 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. " & _
"Please 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
 
J

Jeff Conrad

Have you been following the steps in the KB article **to the letter**?

If you follow them in perfect order and not make any modifications,
the code will work.

Back up a little bit, and rework your way through all the steps carefully.
If you're still having trouble, post back and we'll take it slowly.
--
Jeff Conrad
Access Junkie - MVP
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:
Now when i try it the following error comes up:

Runtime error: '438'
Object does not support this property or method

When I click Debug it takes me to this:

Private Sub CheckPassword_Click()
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
End Sub

Any ideas?

Jeff Conrad said:
Did you set up the module code as well per the KB article?

--
Jeff Conrad
Access Junkie - MVP
http://home.bendbroadband.com/conradsystems/accessjunkie.html
http://www.access.qbuilt.com/html/articles.html

in message:
Hi,

I followed instructions on how to create a password on a form but when i
went to test it the following error came up:

Compile Error: Sub or Function not defined

Below is the code I entered as the event procedure for OnOpen for my form
called 'Add' . Can someone see how I can change it to make this work?

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

On Error GoTo Error_Handler
' Check to see if the user is passing in the Password.
Hold = InputBox("Please Enter Your Password", "Enter Password")
' 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 info. 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. " & _
"Please 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
 
N

Nikki

Thanks will do.
Can you please give me the link to the KB article you are talking about so i
know i am following the right instructions
 
N

Nikki

OK so I found the error. I had named the text box label Text0 instead of the
Text box itself. All fixed now.
Just a few minor issues - I have to double click Check Password Button even
tho the command it OnClick not OnDblClick.
Also if I enter the wrong password it says 'Wrong Password try again' and
gives an OK button. When I click that it just closes down and doesn't take me
back to try another password.

Any ideas on either of these?


Nikki said:
Thanks will do.
Can you please give me the link to the KB article you are talking about so i
know i am following the right instructions

Nikki said:
Hi,

I followed instructions on how to create a password on a form but when i
went to test it the following error came up:

Compile Error: Sub or Function not defined

Below is the code I entered as the event procedure for OnOpen for my form
called 'Add' . Can someone see how I can change it to make this work?

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

On Error GoTo Error_Handler
' Check to see if the user is passing in the Password.
Hold = InputBox("Please Enter Your Password", "Enter Password")
' 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 info. 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. " & _
"Please 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
 
J

Jeff Conrad

in message:

Hi Nikki,

Comments in-line...
OK so I found the error. I had named the text box label Text0 instead of the
Text box itself. All fixed now.
Excellent.

Just a few minor issues - I have to double click Check Password Button even
tho the command it OnClick not OnDblClick.

That's weird.
I'm probably going to state the obvious: are you *absolutely* positive you
have the code in the Click event procedure?
Also if I enter the wrong password it says 'Wrong Password try again' and
gives an OK button. When I click that it just closes down and doesn't take me
back to try another password.

Any ideas on either of these?

Well the second one was a lot trickier than I first expected because the form
is being opened in Dialog mode from the other form's Open event. Hard to
explain, but it took some tweaking.

It would be WAY too much to explain everything I did so I uploaded a working
sample file to my website. The code is heavily commented so you can see what
is going on. There are four objects:
1. tblPassword - No changes there
2. frmPassword - Lots of changes there
3. frmProtectedForm - Sample form for opening. A lot of modifications there!
4. basPassFunction - Module code modified a bit

The file is in 2000 format so let me know if you need a 97 sample.
Unzip the file and then open frmProtectedForm. The password form
will open first. The correct password is actually just "password" (no quotes
though and all lower case). Try an incorrect password first and you'll see
that the password form pops back up for you to try again. The text box
is cleared and then you can try again. At any time you can hit Cancel to
stop. At some point use the correct password and you will see the form
open up.

Here is your own special download page:

http://home.bendbroadband.com/conradsystems/accessjunkie/temp.html

Let me know if this helps. If you have questions just ask.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top