Microsoft Access How-To:
Conditionally Opening an Object



Imagine you have a database object such as a table, a query, a form or a report but that should be opened only if a certain condition is met. For example, you may have created an object that holds the employees or contractors paychecks. You may want such an object to be opened only on a certain day of the week. To Accomplish this, you can specify a condition a condition that must be met for the object to be opened. To implement an example, when a user decides to open a paycheck object, you can first find out what day of the week it is and then take an appropriate action. Here is an example:

Private Sub cmdPaycheck_Click()
On Error GoTo Err_cmdPaycheck_Click

    Dim stDocName As String
    Dim intFriday As Integer

    stDocName = "rptPaycheck"
    intFriday = 6
    If Weekday(Date) = intFriday Then
        DoCmd.OpenReport stDocName, acPreview
        MsgBox "Paychecks are available on Friday only!"
    End If

    Exit Sub

    MsgBox Err.Description
    Resume Exit_cmdPaycheck_Click
End Sub

Based on this code, if a user tries opening a report named rptPaycheck on a day of the week other than Friday, he or she would receive an warning and the report would not be opened.


Home Copyright © 2005-2015, FunctionX