Type MisMatch.... (arrrrrghhh..."HELP") (1 Viewer)

Randix

Registered User.
Local time
Today, 05:55
Joined
Mar 24, 2001
Messages
56
Ok, borrowing from the sample code put up by Microsoft to do this, eg password protect a form or report, I keep getting nowhere...now I'm getting "type mismatch" error...here's what I've done...

1. I've created a new module which I've called "KeyCode" containing the following:

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

The above is an exact cut and paste from Microsoft's suggested code.

2. I then went and created a new table called "tblPassword" containing two variables, ObjectName which is "Text", field size of 50, and KeyCode which is "Number", field size of "Long Integer", Input Mask of "Password". I made the objectName the primary key. I opened the tblPassword table and entered the following data: ObjectName: orders, KeyCode: 2818

All of the above was exactly per Microsoft.

3. I then created a form called "Orders", went into design mode, and in the OnOpen property i created an event procedure then goes as follows (cut and paste from the Microsoft suggestion):

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.
If IsNull(Me.OpenArgs) Then
Hold = InputBox("Please Enter Your Password", "Enter Password")
Else
Hold = Me.OpenArgs
End If
' 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

4. I then run the form Orders and whatever i put into the input box, i get the response "Error #13, Type mismatch"...can someone tell me what i'm doing wrong...i've tried to be careful to follow all the steps and used cut and paste to avoid typing errors

Thanks!
 

JKB

Registered User.
Local time
Today, 05:55
Joined
Feb 25, 2001
Messages
10
Funny, I was working on the exact same code this morning, having almost the same problem. MS says to create the passwork table field named KeyCode, using a number data type with an input mask. When you try this, you get an error stating that input masks can only be used with Text data type fields, not number. So I used a text field instead, and all seemed to work fine then. This may or may not be the source of your problem. HTH
 

Users who are viewing this thread

Top Bottom