Full Version: Prevent a form from opening
UtterAccess Forums > Microsoft® Access > Access Forms
Powelldog
Hi all
I have a form that is opened from the switchboard, so i cannot use code from the button to prevent it from opening.

I have a form for Reconciling pay, some people are paid on salary, these people do not need to reconcile.

code
If Me.Check49 = -1 Then
MsgBox "You have checked off Paid on salary, This form is invalid", vbOKOnly, "Salary?"
Exit Sub
End If
code

Waht I want to have hapen, is when the form is opened it just gives the message then closes, when I click Ok
zocker
SubBlah
If Me.Check49 = -1 Then
MsgBox "You have checked off Paid on salary, This form is invalid", vbOKOnly, "Salary?"
docmd.close

End If
End sub
I am not familiar with the switchboard but you should be able to do it from there, opening and closing the form like that does not seem good practice.
All the best
Z
HiTechCoach
I see to options:
) Filter out he record in the form's record source so that the never make it to the form at all.
2) in the Form's On Current event use code:
CODE
Private Sub Form_Current()
If Me.Check49 = True Then
     MsgBox "You have checked off Paid on salary, This form is invalid", vbOKOnly, "Salary?"
     DoCmd.Close acForm, Me.Name
End If
End Sub
Powelldog
I did try that, it just gives me an error message syaing "Variable not defined"
vtd
If you are using the Access in-built Switchboard, I reckon that youneed to use Boyd's Option 1, i.e. setting the Form's RecordSource to filter out those paid on salary so that these people don't even appear on the Form.
Powelldog
Ok so I filtered the results and now just have a blank form that will open, along with the message that opens prior to the form, that is good enough!
thanks David
vtd
You're welcome... Glad we could help...
mishej
Here is an alternative solution I often use....
If I want to prevent someone from opening a form I may use the form's OpenArgs argument to look for a specific value and if not found close the form and optionally display a message to the user.
So, to open the form I might use this command:
DoCmd.OpenForm "frmSelectOutput", , , , , acDialog, "Open"
Note that the "Open" appears in the OpenArgs argument position. The form's Open event can then check the OpenArgs value for a value. The sample code below just checks that some value was passed, not specifically "Open". It sets the Cancel argument and if no value is passed in OpenArgs then the form silently closes (and raises error 2501 which your calling code should handle silently).
CODE
Private Sub Form_Open(Cancel As Integer)
    Cancel = (Len(Me.OpenArgs & vbNullString) = 0)
End Sub

Just another option but might not meet your needs.
Not sure the Switchboard can use the .OpenArgs property though.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.