Password to open certain report

A

ake

Please help me to add a password on opening a specific report.
I want to add a command button that once clicked would ask for a password,
when the password is correct it would open a certain report but if the
password is wrong a message box will open that advise the user that he cannot
open the report.

I am trying to do this with VBA but i am having a hard time. Please tell me
what to do.
 
C

Chris O'C via AccessMonster.com

When the form is in design view add a command button. In the proc for the
command button's click event put this:

On Error GoTo ProcErr

DoCmd.OpenReport "secretreport", acViewPreview

Exit Sub

ProcErr:
If Err.Number <> 2501 Then
MsgBox Err.Number & vbCrLf & Err.Description
End If
Err.Clear

Change secretreport to the name of your report, save and compile. In the
report's module put this:

Private Sub Report_Open(Cancel As Integer)
On Error GoTo ProcErr

Dim strPass As String
Const PASS As String = "mypassword"

strPass = InputBox("Please enter the password", "Password")

If Len(strPass) = 0 Then
Cancel = True
ElseIf strPass <> PASS Then
MsgBox "Sorry, not the right password"
Cancel = True
End If

Exit Sub

ProcErr:
MsgBox Err.Number & vbCrLf & Err.Description
Err.Clear
End Sub


Change mypassword to your password, save and compile. If you don't make this
db an mde or accde, anybody can read the code and find the password in the
vba editor.

Chris
 
A

ake

Hi Chris! Thanks for your help. I don't know why but there must be something
we've missed in there, it didn't work. The report opens but it doesn't ask
for a password.
 
C

Chris O'C via AccessMonster.com

IIRC Access 97 and 2000 doesn't automatically set the event procedure in the
report's properties when the code is pasted in and saved like it does in
newer versions.

Open the report in design view and look at the open event. Does it show "
[Event Procedure]" - without the quotes? Make sure it does and then save the
report.

Chris
 

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