Password protected report problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have just password protected a form and report according to Microsoft
Article 209871. It works fine on the form but I keep getting Error# 13, type
mismatch when I try to open the report. I checked the references and
Microsoft DAO 3.6 Object Library is checked, and the declaration of the
recordset is: Dim rs As DAO.Recordset. Now I'm totally befuddled....

Any help/suggestions as to where to go from here would be greatly appreciated.

Thanks Kindly
 
Since that KB article doesn't actually have sample code for reports, what
did you end up using?
 
Hi Doug,

First off thank you so much for replying. I used the exact same code that I
used on the form. I thought maybe it was because my Report didn't actually
have a "PrimaryKey" (Total Report based on a Crosstab query) so I tested it
on other reports that do have a PK and it seemed to work fine. So, could
this be the reason ? If so, is there a work around? Anyway, here is the
code I used:

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


And on the OnOpen Event of my report the following:


Private Sub Report_Open(Cancel As Integer)

Dim Hold As Integer
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/report
' from opening.

If Not (rs![KeyCode] = KeyCode(CStr(Hold))) Then
MsgBox "Sorry you entered a 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

Thanks again.
 
You've changed the declaration of Hold from the original in your code. You
need

Dim Hold As Variant

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Gabby Girl said:
Hi Doug,

First off thank you so much for replying. I used the exact same code that
I
used on the form. I thought maybe it was because my Report didn't
actually
have a "PrimaryKey" (Total Report based on a Crosstab query) so I tested
it
on other reports that do have a PK and it seemed to work fine. So, could
this be the reason ? If so, is there a work around? Anyway, here is the
code I used:

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


And on the OnOpen Event of my report the following:


Private Sub Report_Open(Cancel As Integer)

Dim Hold As Integer
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/report
' from opening.

If Not (rs![KeyCode] = KeyCode(CStr(Hold))) Then
MsgBox "Sorry you entered a 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

Thanks again.


Douglas J. Steele said:
Since that KB article doesn't actually have sample code for reports, what
did you end up using?
 
Hi Doug,

Works like a charm now. Thank you so very very much.


Douglas J. Steele said:
You've changed the declaration of Hold from the original in your code. You
need

Dim Hold As Variant

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Gabby Girl said:
Hi Doug,

First off thank you so much for replying. I used the exact same code that
I
used on the form. I thought maybe it was because my Report didn't
actually
have a "PrimaryKey" (Total Report based on a Crosstab query) so I tested
it
on other reports that do have a PK and it seemed to work fine. So, could
this be the reason ? If so, is there a work around? Anyway, here is the
code I used:

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


And on the OnOpen Event of my report the following:


Private Sub Report_Open(Cancel As Integer)

Dim Hold As Integer
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/report
' from opening.

If Not (rs![KeyCode] = KeyCode(CStr(Hold))) Then
MsgBox "Sorry you entered a 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

Thanks again.


Douglas J. Steele said:
Since that KB article doesn't actually have sample code for reports, what
did you end up using?

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Hello,

I have just password protected a form and report according to Microsoft
Article 209871. It works fine on the form but I keep getting Error#
13,
type
mismatch when I try to open the report. I checked the references and
Microsoft DAO 3.6 Object Library is checked, and the declaration of the
recordset is: Dim rs As DAO.Recordset. Now I'm totally befuddled....

Any help/suggestions as to where to go from here would be greatly
appreciated.

Thanks Kindly
 
Back
Top