18 September 2011

Create Functions using Module




In MS Access, modules contain declarations, statements, and procedures or functions that are programmed in a structured and systematic manner as a single unified entity. There are two types of MS Access module, class module and standard module. Class module is on a form or report, and usually contains a specially programmed procedures for its form or report. Standard module contains general procedures that are not associated with or can be used on other objects - such as query, form, and report. Standard modules are listed under Modules in the Navigation Pane, while a class module not.

How to create a Module

To create a module, do the following steps:
  1. Click Create tab and choose Modules in Other group. Access will display Microsoft Visual Basic (VB) Editor like this


  2. You may copy VB scripts below and paste into VB editor. Save the module as GlobalProcedure
    '---------------------------------------------------------------'
    ' '
    'Procedures in this Module consists of general command '
    'that is really needed in making the application to be '
    'easily executed '
    ' '
    '---------------------------------------------------------------'
    '------------------------------------------------------------
    ' Global_Exit
    '
    '------------------------------------------------------------
    Function Global_Exit()
    On Error GoTo Global_Exit_Err
    
       DoCmd.RunCommand acCmdExit
    
    Global_Exit_Exit:
       Exit Function
    
    Global_Exit_Err:
       MsgBox Error$
       Resume Global_Exit_Exit
    
    End Function
    '------------------------------------------------------------
    ' Global_Next
    '
    '------------------------------------------------------------
    Function Global_Next()
    On Error GoTo Global_Next_Err
    
       DoCmd.RunCommand acCmdRecordsGoToNext
    
    Global_Next_Exit:
       Exit Function
    
    Global_Next_Err:
       MsgBox "You might be at the end of the recordset. Contact your admin if neccessary.", vbOKOnly
       Resume Global_Next_Exit
    
    End Function
    '------------------------------------------------------------
    ' Global_Previous
    '
    '------------------------------------------------------------
    Function Global_Previous()
    On Error GoTo Global_Previous_Err
    
       DoCmd.RunCommand acCmdRecordsGoToPrevious
    
    Global_Previous_Exit:
       Exit Function
    
    Global_Previous_Err:
       MsgBox "You might be at the first of the recordset. Contact your admin if neccessary.", vbOKOnly
       Resume Global_Previous_Exit
    
    End Function
    '------------------------------------------------------------
    ' Global_Last
    '
    '------------------------------------------------------------
    Function Global_Last()
    On Error GoTo Global_Last_Err
       DoCmd.RunCommand acCmdRefresh
       DoCmd.RunCommand acCmdRecordsGoToLast
    
    Global_Last_Exit:
       Exit Function
    
    Global_Last_Err:
       MsgBox "You might be at the end of the recordset. Contact your admin if neccessary.", vbOKOnly
       Resume Global_Last_Exit
    
    End Function
    '------------------------------------------------------------
    ' Global_First
    '
    '------------------------------------------------------------
    Function Global_First()
    On Error GoTo Global_First_Err
    
       DoCmd.RunCommand acCmdRecordsGoToFirst
    
    Global_First_Exit:
       Exit Function
    
    Global_First_Err:
       MsgBox "You might be at the first of the recordset. Contact your admin if neccessary.", vbOKOnly
       Resume Global_First_Exit
    
    End Function
    '------------------------------------------------------------
    ' Global_Close
    '
    '------------------------------------------------------------
    Function Global_Close()
    On Error GoTo Global_Close_Err
       DoCmd.RunCommand acCmdClose
    
    Global_Close_Exit:
       Exit Function
    
    Global_Close_Err:
       MsgBox Error$
       Resume Global_Close_Exit
    
    End Function
    '------------------------------------------------------------
    ' Global_Deletion
    '
    '------------------------------------------------------------
    Function Global_Deletion()
    On Error GoTo Global_Deletion_Err
    
       DoCmd.RunCommand acCmdDeleteRecord
    
    Global_Deletion_Exit:
    
       Exit Function
    
    Global_Deletion_Err:
       MsgBox Error$
       Resume Global_Deletion_Exit
    
    End Function
    '------------------------------------------------------------
    ' Global_Preview
    '
    '------------------------------------------------------------
    Function Global_Preview(stDocName As String)
    On Error GoTo Err_Global_Preview
       DoCmd.RunCommand acCmdRefresh
       DoCmd.OpenReport stDocName, acPreview
    
    Exit_Global_Preview:
       Exit Function
    
    Err_Global_Preview:
       MsgBox Err.Description
       Resume Exit_Global_Preview
    
    End Function
    '------------------------------------------------------------
    ' Global_OpenForm
    '
    '------------------------------------------------------------
    Function Global_OpenForm(stDocName As String)
    On Error GoTo Err_Global_OpenForm
       DoCmd.OpenForm stDocName
    
    Exit_Global_OpenForm:
       Exit Function
    
    Err_Global_OpenForm:
       MsgBox Err.Description
       Resume Exit_Global_OpenForm
    
    End Function
    '------------------------------------------------------------
    ' Global_OpenFormUnclose
    '
    '------------------------------------------------------------
    Function Global_OpenFormUnclose(stDocName As String)
    On Error GoTo Err_Global_OpenFormUnclose
    
       DoCmd.OpenForm stDocName
    
    Exit_Global_OpenFormUnclose:
       Exit Function
    
    Err_Global_OpenFormUnclose:
       MsgBox Err.Description
       Resume Exit_Global_OpenFormUnclose
    
    End Function
    '------------------------------------------------------------
    ' Global_Preview_Unrefreshed
    '
    '------------------------------------------------------------
    Function Global_PreviewUnRefresh(stDocName As String)
    On Error GoTo Err_Global_Preview
    
       DoCmd.OpenReport stDocName, acPreview
    
    Exit_Global_Preview:
       Exit Function
    
    Err_Global_Preview:
       MsgBox Err.Description
       Resume Exit_Global_Preview
    
    End Function
    '------------------------------------------------------------
    ' Global_Process_Deletion
    '
    '------------------------------------------------------------
    Function Global_Process_Deletion()
    On Error GoTo Global_Process_Deletion_Err
    
       DoCmd.Echo False, ""
       SendKeys "Y", False
       DoCmd.RunCommand acCmdDeleteRecord
       DoCmd.Echo True, ""
    
    Global_Process_Deletion_Exit:
       Exit Function
    
    Global_Process_Deletion_Err:
       MsgBox Error$
       Resume Global_Process_Deletion_Exit
    
    End Function
    '------------------------------------------------------------
    ' Global_Undo
    '
    '------------------------------------------------------------
    Function Global_Undo()
    On Error GoTo Err_Global_Undo
    
       DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
    
    Exit_Global_Undo:
       Exit Function
    
    Err_Global_Undo:
       MsgBox Err.Description
       Resume Exit_Global_Undo
    
    End Function
    '------------------------------------------------------------
    ' Global_Print
    '
    '------------------------------------------------------------
    Function Global_Print(stDocName As String)
    On Error GoTo Err_Global_Print
       DoCmd.RunCommand acCmdRefresh
    'DoCmd.RunCommand acCmdPrint
       DoCmd.OpenReport stDocName, acNormal
    
    Exit_Global_Print:
       Exit Function
    
    Err_Global_Print:
       MsgBox Err.Description
       Resume Exit_Global_Print
    
    End Function
    '------------------------------------------------------------
    ' Global_PrintUnRefreshed
    '
    '------------------------------------------------------------
    Function Global_PrintUnRefresh(stDocName As String)
    On Error GoTo Err_Global_Print
    
    'DoCmd.RunCommand acCmdPrint
       DoCmd.OpenReport stDocName, acNormal
    
    Exit_Global_Print:
       Exit Function
    
    Err_Global_Print:
       MsgBox Err.Description
       Resume Exit_Global_Print
    End Function
    '------------------------------------------------------------
    ' Global_AddNew
    '
    '------------------------------------------------------------
    Function Global_AddNew()
    On Error GoTo Global_AddNew_Err
       DoCmd.RunCommand acCmdRefresh
       DoCmd.RunCommand acCmdRecordsGoToNew
    
    Global_AddNew_Exit:
       Exit Function
    
    Global_AddNew_Err:
       MsgBox Error$
       Resume Global_AddNew_Exit
    
    End Function
    '------------------------------------------------------------
    ' Global_Find
    '
    '------------------------------------------------------------
    Function Global_Find()
    On Error GoTo Err_Global_Find
    
       Screen.PreviousControl.SetFocus
       DoCmd.DoMenuItem acFormBar, acEditMenu, 10, , acMenuVer70
    
    Exit_Global_Find:
       Exit Function
    
    Err_Global_Find:
       MsgBox Err.Description
       Resume Exit_Global_Find
    
    End Function
    
  3. Each function in the above module can be called using an event procedure or expression builder, which one is proper. Further explanation will be given later in time.

No comments :

Post a Comment