Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources]

Alison Balter

نسخه متنی -صفحه : 544/ 137
نمايش فراداده

Passing Objects to Subroutines and Functions

Just as you can pass a string or a number to a subroutine or function, you can also pass an object to a subroutine or function. The code, found in the basExamples module in the Chap8Ex database, looks like this:

Sub ChangeCaption(frmAny as Form) 'Change the caption property of the form received 'to what was already in the caption property, 'concatenated with a colon and the name of the current user frmAny.Caption = frmAny.Caption & ": " & CurrentUser End Sub

The ChangeCaption routine receives a reference to a form as a parameter. The caption of the form referenced by the procedure is modified to include the name of the current user. The ChangeCaption routine is called like this:

Private Sub cmdChangeCaption_Click() 'Call the ChangeCaption routine, passing a reference to the current form Call ChangeCaption(Me) End Sub

In this example, the click event of the cmdChangeCaption command button calls the ChangeCaption routine, sending a reference to the form that the command button is contained within. You will find this code in the form frmChangeCaption.