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
    001'---------------------------------------------------------------'
    002' '
    003'Procedures in this Module consists of general command '
    004'that is really needed in making the application to be '
    005'easily executed '
    006' '
    007'---------------------------------------------------------------'
    008'------------------------------------------------------------
    009' Global_Exit
    010'
    011'------------------------------------------------------------
    012Function Global_Exit()
    013On Error GoTo Global_Exit_Err
    014 
    015   DoCmd.RunCommand acCmdExit
    016 
    017Global_Exit_Exit:
    018   Exit Function
    019 
    020Global_Exit_Err:
    021   MsgBox Error$
    022   Resume Global_Exit_Exit
    023 
    024End Function
    025'------------------------------------------------------------
    026' Global_Next
    027'
    028'------------------------------------------------------------
    029Function Global_Next()
    030On Error GoTo Global_Next_Err
    031 
    032   DoCmd.RunCommand acCmdRecordsGoToNext
    033 
    034Global_Next_Exit:
    035   Exit Function
    036 
    037Global_Next_Err:
    038   MsgBox "You might be at the end of the recordset. Contact your admin if neccessary.", vbOKOnly
    039   Resume Global_Next_Exit
    040 
    041End Function
    042'------------------------------------------------------------
    043' Global_Previous
    044'
    045'------------------------------------------------------------
    046Function Global_Previous()
    047On Error GoTo Global_Previous_Err
    048 
    049   DoCmd.RunCommand acCmdRecordsGoToPrevious
    050 
    051Global_Previous_Exit:
    052   Exit Function
    053 
    054Global_Previous_Err:
    055   MsgBox "You might be at the first of the recordset. Contact your admin if neccessary.", vbOKOnly
    056   Resume Global_Previous_Exit
    057 
    058End Function
    059'------------------------------------------------------------
    060' Global_Last
    061'
    062'------------------------------------------------------------
    063Function Global_Last()
    064On Error GoTo Global_Last_Err
    065   DoCmd.RunCommand acCmdRefresh
    066   DoCmd.RunCommand acCmdRecordsGoToLast
    067 
    068Global_Last_Exit:
    069   Exit Function
    070 
    071Global_Last_Err:
    072   MsgBox "You might be at the end of the recordset. Contact your admin if neccessary.", vbOKOnly
    073   Resume Global_Last_Exit
    074 
    075End Function
    076'------------------------------------------------------------
    077' Global_First
    078'
    079'------------------------------------------------------------
    080Function Global_First()
    081On Error GoTo Global_First_Err
    082 
    083   DoCmd.RunCommand acCmdRecordsGoToFirst
    084 
    085Global_First_Exit:
    086   Exit Function
    087 
    088Global_First_Err:
    089   MsgBox "You might be at the first of the recordset. Contact your admin if neccessary.", vbOKOnly
    090   Resume Global_First_Exit
    091 
    092End Function
    093'------------------------------------------------------------
    094' Global_Close
    095'
    096'------------------------------------------------------------
    097Function Global_Close()
    098On Error GoTo Global_Close_Err
    099   DoCmd.RunCommand acCmdClose
    100 
    101Global_Close_Exit:
    102   Exit Function
    103 
    104Global_Close_Err:
    105   MsgBox Error$
    106   Resume Global_Close_Exit
    107 
    108End Function
    109'------------------------------------------------------------
    110' Global_Deletion
    111'
    112'------------------------------------------------------------
    113Function Global_Deletion()
    114On Error GoTo Global_Deletion_Err
    115 
    116   DoCmd.RunCommand acCmdDeleteRecord
    117 
    118Global_Deletion_Exit:
    119 
    120   Exit Function
    121 
    122Global_Deletion_Err:
    123   MsgBox Error$
    124   Resume Global_Deletion_Exit
    125 
    126End Function
    127'------------------------------------------------------------
    128' Global_Preview
    129'
    130'------------------------------------------------------------
    131Function Global_Preview(stDocName As String)
    132On Error GoTo Err_Global_Preview
    133   DoCmd.RunCommand acCmdRefresh
    134   DoCmd.OpenReport stDocName, acPreview
    135 
    136Exit_Global_Preview:
    137   Exit Function
    138 
    139Err_Global_Preview:
    140   MsgBox Err.Description
    141   Resume Exit_Global_Preview
    142 
    143End Function
    144'------------------------------------------------------------
    145' Global_OpenForm
    146'
    147'------------------------------------------------------------
    148Function Global_OpenForm(stDocName As String)
    149On Error GoTo Err_Global_OpenForm
    150   DoCmd.OpenForm stDocName
    151 
    152Exit_Global_OpenForm:
    153   Exit Function
    154 
    155Err_Global_OpenForm:
    156   MsgBox Err.Description
    157   Resume Exit_Global_OpenForm
    158 
    159End Function
    160'------------------------------------------------------------
    161' Global_OpenFormUnclose
    162'
    163'------------------------------------------------------------
    164Function Global_OpenFormUnclose(stDocName As String)
    165On Error GoTo Err_Global_OpenFormUnclose
    166 
    167   DoCmd.OpenForm stDocName
    168 
    169Exit_Global_OpenFormUnclose:
    170   Exit Function
    171 
    172Err_Global_OpenFormUnclose:
    173   MsgBox Err.Description
    174   Resume Exit_Global_OpenFormUnclose
    175 
    176End Function
    177'------------------------------------------------------------
    178' Global_Preview_Unrefreshed
    179'
    180'------------------------------------------------------------
    181Function Global_PreviewUnRefresh(stDocName As String)
    182On Error GoTo Err_Global_Preview
    183 
    184   DoCmd.OpenReport stDocName, acPreview
    185 
    186Exit_Global_Preview:
    187   Exit Function
    188 
    189Err_Global_Preview:
    190   MsgBox Err.Description
    191   Resume Exit_Global_Preview
    192 
    193End Function
    194'------------------------------------------------------------
    195' Global_Process_Deletion
    196'
    197'------------------------------------------------------------
    198Function Global_Process_Deletion()
    199On Error GoTo Global_Process_Deletion_Err
    200 
    201   DoCmd.Echo False, ""
    202   SendKeys "Y", False
    203   DoCmd.RunCommand acCmdDeleteRecord
    204   DoCmd.Echo True, ""
    205 
    206Global_Process_Deletion_Exit:
    207   Exit Function
    208 
    209Global_Process_Deletion_Err:
    210   MsgBox Error$
    211   Resume Global_Process_Deletion_Exit
    212 
    213End Function
    214'------------------------------------------------------------
    215' Global_Undo
    216'
    217'------------------------------------------------------------
    218Function Global_Undo()
    219On Error GoTo Err_Global_Undo
    220 
    221   DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
    222 
    223Exit_Global_Undo:
    224   Exit Function
    225 
    226Err_Global_Undo:
    227   MsgBox Err.Description
    228   Resume Exit_Global_Undo
    229 
    230End Function
    231'------------------------------------------------------------
    232' Global_Print
    233'
    234'------------------------------------------------------------
    235Function Global_Print(stDocName As String)
    236On Error GoTo Err_Global_Print
    237   DoCmd.RunCommand acCmdRefresh
    238'DoCmd.RunCommand acCmdPrint
    239   DoCmd.OpenReport stDocName, acNormal
    240 
    241Exit_Global_Print:
    242   Exit Function
    243 
    244Err_Global_Print:
    245   MsgBox Err.Description
    246   Resume Exit_Global_Print
    247 
    248End Function
    249'------------------------------------------------------------
    250' Global_PrintUnRefreshed
    251'
    252'------------------------------------------------------------
    253Function Global_PrintUnRefresh(stDocName As String)
    254On Error GoTo Err_Global_Print
    255 
    256'DoCmd.RunCommand acCmdPrint
    257   DoCmd.OpenReport stDocName, acNormal
    258 
    259Exit_Global_Print:
    260   Exit Function
    261 
    262Err_Global_Print:
    263   MsgBox Err.Description
    264   Resume Exit_Global_Print
    265End Function
    266'------------------------------------------------------------
    267' Global_AddNew
    268'
    269'------------------------------------------------------------
    270Function Global_AddNew()
    271On Error GoTo Global_AddNew_Err
    272   DoCmd.RunCommand acCmdRefresh
    273   DoCmd.RunCommand acCmdRecordsGoToNew
    274 
    275Global_AddNew_Exit:
    276   Exit Function
    277 
    278Global_AddNew_Err:
    279   MsgBox Error$
    280   Resume Global_AddNew_Exit
    281 
    282End Function
    283'------------------------------------------------------------
    284' Global_Find
    285'
    286'------------------------------------------------------------
    287Function Global_Find()
    288On Error GoTo Err_Global_Find
    289 
    290   Screen.PreviousControl.SetFocus
    291   DoCmd.DoMenuItem acFormBar, acEditMenu, 10, , acMenuVer70
    292 
    293Exit_Global_Find:
    294   Exit Function
    295 
    296Err_Global_Find:
    297   MsgBox Err.Description
    298   Resume Exit_Global_Find
    299 
    300End 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