Online Book Reader

Home Category

Access Cookbook - Ken Getz [276]

By Root 1985 0
problems requires little more than creating an object in memory, setting some properties, and calling the correct methods. This sample provides a form that demonstrates code you can use.

WARNING

Because of the serious threat of email viruses, the Outlook team has "locked down" the programmability features of Microsoft Outlook. The level of the virus support may be different depending on which version of Outlook you have installed and what service release you've added. In testing this demonstration, you may see an alert warning you that someone is attempting to modify your address book. You can safely dismiss that dialog for this demonstration, but you should never take it lightly in real use.

Load and run frmContacts from 12-08.MDB. This form, shown in Figure 12-17, allows you to edit contact information within Access. You can click on Send Email to create a new email message to the address you've provided in the contact record. Click on Add Contact to copy the contact information to a new contact item within Outlook. Note that the Send Email button isn't available unless you've specified an email address, and the Add Contact button isn't available unless you've specified a LastName value.

Figure 12-17. frmContacts allows you to work with Outlook contacts and send email

Follow these steps to create a form like frmContacts:

Import the module basAutomateOutlook from 12-08.MDB.

Open basAutomateOutlook and use the Tools → References... menu item to add a reference to the Microsoft Outlook Type Library. (Select the most current version of the library, or the version you're intending to target.)

Import tblContacts from 12-08.MDB.

Either import frmContacts from 12-08.MDB, or create your own form. (If you import the existing form, you can skip to Step 8.) You can create a new form based on your tblContacts. You can add fields and modify field names as necessary in the underlying table (tblContacts), but you'll need to modify the code that follows to match, if you do.

Add the following procedure to the form's module to handle enabling and disabling the two command buttons:

Private Sub HandleEnabling(varEmail As Variant, _

varFirstName As Variant)

Me.cmdEmail.Enabled = Len(varEmail & "") > 0

Me.cmdContact.Enabled = Len(varFirstName & "") > 0

End Sub

Add event procedures to call HandleEnabling from the form's Current event and from the two important text boxes' Change events:

Private Sub Email_Change( )

Call HandleEnabling(Me.Email.Text, Me.FirstName)

End Sub

Private Sub FirstName_Change( )

Call HandleEnabling(Me.Email, Me.FirstName.Text)

End Sub

Private Sub Form_Current( )

Call HandleEnabling(Me.Email, Me.FirstName)

End Sub

TIP

Note that in the Email text box's Change event, you must use the Text property (not the default Value property) if you want to refer to the current value in the control. This is a confusing area in Access forms: while in the middle of editing a control on an Access form, the Text property contains the actual, current text. The Value property (which is the default property, so you needn't explicitly specify it) contains the original text in the control, before you began editing it. In this example, the Change event procedures must refer to the Text property of the current control (the one being changed) but the Value property of the other control.

In the Click event procedures for the two command buttons, add code to call the appropriate procedures in basAutomateOutlook:

Private Sub cmdContact_Click( )

Call AddContact(Me.FirstName, Me.LastName, Me.Address, _

Me.City, Me.State, Me.PostalCode, Me.Email)

End Sub

Private Sub cmdEmail_Click( )

Call SendEmail(Me.Email)

End Sub

Run your form, add some data, and try out the two buttons on the form. Clicking Send Email should bring up the Outlook email editor. Clicking Add Contact should copy data to the contact editor in Outlook and leave the editor available for you to continue editing.

Discussion


All the power of this example is buried in basAutomateOutlook's code. This section will

Return Main Page Previous Page Next Page

®Online Book Reader