Microsoft Office Tutorials and References
In Depth Information
Testing and Running Your Code
To test the preceding procedure, simply type its name (without the
parentheses) in the Immediate window, as follows:
The procedure switches to the Access window and shows a message. Then
close the message box and switch back to Visual Basic Editor to continue
writing code.
If a sub procedure accepts arguments, follow the procedure name with a
blank space and the value to pass to the sub procedure. The following
example sub procedure accepts one argument:
Sub WarnUser (msg as String)
x = MsgBox(msg, vbCritical, “Warning”)
End Sub
Access assumes that the passed parameter is a string. To test the
procedure, you need to pass some text to it. Type the following in the Immediate
window and then press Enter to test this procedure:
WarnUser “Don’t move!”
When you press Enter, the procedure executes, displaying a message box in
the Access window. Close the message box to return to Visual Basic Editor.
If a procedure accepts more than one argument, separate the arguments
with commas. The following procedure accepts two string arguments:
Sub TakeTwo(msg as String, tBar As String)
x = msgbox(msg, vbOKOnly, tBar)
End Sub
To test the procedure, you need to pass two parameters to it from the
Immediate window, as in this example:
TakeTwo “Hello World”, “Sample”
A message box opens, containing the text “Hello World” and a single OK
button, and “Sample” appears in the title bar. The result is the same if you
execute this statement directly:
x = msgbox(“Hello World”, vbOKOnly, “Sample”)
Running sub procedures from Access
The real goal of a sub procedure, of course, is to run from within Access
when appropriate. Sub procedures in a class module usually are tied to a
control on the corresponding form or report. To actually run a procedure,
open the corresponding form or report, and trigger the event that causes the
Search JabSto ::

Custom Search