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