Aceman: I found your response to set a password to open a form in a previous post, but I am having trouble with it. What does Set prp = db.TableDefs("tblType").Properties("PassAns")refer to? Also, are you suggesting a table to hold Pass and PassAns or the db's custom properties?Does the password form's Ok button open the form? This is a little over my head. Any help would be appreciated.
Thanks Sophia
You start br creating two custom table properties, Pass & PassAns (it helps if the password can be saved somewhere not out in the open, even if its VBA). These properties hold the password and password answer from the popup. You can use any table to hold the properties. I used a table to hold the properties instead of the db because db properties call for redistribution of the front end if the db is split.
In the Click event of the button on your switchboard, copy/paste the following:
CODE
On Error GoTo GotErr
DoCmd.OpenForm "FormToOpenName"
GotErr:
On Error simply traps the following error due to canceling opening of the form thru DoCmd.OpenForm:
Run-Time Error '2501':
The Open Form action was canceled.
The form starts to open triggering its On Open event which has the following code for each form you wish to test password before opening:
CODE
Dim db As DAO.Database, prp As Property
Set db = CurrentDb
Set prp = db.TableDefs("tblType").Properties("PassAns")
prp = "~" 'Reset PassAns
If Not PasswordOK() Then Cancel = True 'Test PassWord!
prp = "~" 'Reset PassAns
Set prp = Nothing
Set db = Nothing
PassAns is reset then the popup is opened in the function PasswordOK to get user input. Code stops running here in the form (due to acDialog in opening the popup) and continues when the popup is closed (after the function updates PassAns with user input 1st . . . of course!). If Pass and PassAns match you know what happens . . .
Here's the code for the popup:
CODE
Private Sub Form_Unload(Cancel As Integer)
Dim Ans As String
If Trim(Me!TextboxName & "") = "" Then
Ans = "@"
Else
Ans = Me!TextboxName
End If
CurrentDb.TableDefs("tblType").Properties("PassAns") = Ans
End Sub
Private Sub PopupTextboxName_AfterUpdate()
DoCmd.Close acForm, "PopupFormName", acSaveNo
End Sub
Finally, in a module in the modules window, copy/paste the following:
CODE
Public Function PasswordOK() As Boolean
Dim db As DAO.Database, tdf As TableDef
Dim PW As Property, PWA As Property
Set db = CurrentDb
Set tdf = db.TableDefs("tblType")
Set PW = tdf.Properties("Pass")
Set PWA = tdf.Properties("PassAns")
DoCmd.OpenForm "purple]PopupFormName[/purple]", , , , , acDialog
If StrComp(PW, PWA, vbBinaryCompare) = 0 Then
PasswordOK = True
Else
MsgBox "Wrong PASSWORD!"
End If
Set PWA = Nothing
Set PW = Nothing
Set tdf = Nothing
Set db = Nothing
End Function
Thanks Sophia
You start br creating two custom table properties, Pass & PassAns (it helps if the password can be saved somewhere not out in the open, even if its VBA). These properties hold the password and password answer from the popup. You can use any table to hold the properties. I used a table to hold the properties instead of the db because db properties call for redistribution of the front end if the db is split.
In the Click event of the button on your switchboard, copy/paste the following:
CODE
On Error GoTo GotErr
DoCmd.OpenForm "FormToOpenName"
GotErr:
On Error simply traps the following error due to canceling opening of the form thru DoCmd.OpenForm:
Run-Time Error '2501':
The Open Form action was canceled.
The form starts to open triggering its On Open event which has the following code for each form you wish to test password before opening:
CODE
Dim db As DAO.Database, prp As Property
Set db = CurrentDb
Set prp = db.TableDefs("tblType").Properties("PassAns")
prp = "~" 'Reset PassAns
If Not PasswordOK() Then Cancel = True 'Test PassWord!
prp = "~" 'Reset PassAns
Set prp = Nothing
Set db = Nothing
PassAns is reset then the popup is opened in the function PasswordOK to get user input. Code stops running here in the form (due to acDialog in opening the popup) and continues when the popup is closed (after the function updates PassAns with user input 1st . . . of course!). If Pass and PassAns match you know what happens . . .
Here's the code for the popup:
CODE
Private Sub Form_Unload(Cancel As Integer)
Dim Ans As String
If Trim(Me!TextboxName & "") = "" Then
Ans = "@"
Else
Ans = Me!TextboxName
End If
CurrentDb.TableDefs("tblType").Properties("PassAns") = Ans
End Sub
Private Sub PopupTextboxName_AfterUpdate()
DoCmd.Close acForm, "PopupFormName", acSaveNo
End Sub
Finally, in a module in the modules window, copy/paste the following:
CODE
Public Function PasswordOK() As Boolean
Dim db As DAO.Database, tdf As TableDef
Dim PW As Property, PWA As Property
Set db = CurrentDb
Set tdf = db.TableDefs("tblType")
Set PW = tdf.Properties("Pass")
Set PWA = tdf.Properties("PassAns")
DoCmd.OpenForm "purple]PopupFormName[/purple]", , , , , acDialog
If StrComp(PW, PWA, vbBinaryCompare) = 0 Then
PasswordOK = True
Else
MsgBox "Wrong PASSWORD!"
End If
Set PWA = Nothing
Set PW = Nothing
Set tdf = Nothing
Set db = Nothing
End Function