New Contact from Access

D

Don Cardoza

Microsoft's Knowledge base was great. I am almost there.
There is only one thing wrong with this code, and I am
hoping someone can help.

I have various fields in my form, FirstName, MiddleName
etc. I created a button that opens up a New Contact in
Outlook and Populates the New Contact from the various
fields in Access.

This works GREAT if every necessary field is populated.
However, if one of these fields is Null then I get an
error "94- Invalid use of Null." Not every record will
have a middle name.

I could I suppose write "if statements" to test each
field, but that seems tedious. There has to be a better
way. any help would be greatly apprecaited.

Thanks so much,
Don

Private Sub cmdAddContacts_Click()

On Error GoTo StartError

'Declare the Variables
Dim ObjOutlook As Outlook.Application
Dim ObjOutlookContact As Outlook.ContactItem

'Create the Outlook Session
Set ObjOutlook = CreateObject("Outlook.Application")

'Create and Open a New Contact Form for Input
Set ObjOutlookContact = ObjOutlook.CreateItem
(olContactItem)

'Add the Field Names
With ObjOutlookContact
.FirstName = Forms!frmExpert.FirstName
'.MiddleName = Forms!frmExpert.MiddleName
.LastName = Forms!frmExpert.LastName
.Suffix = Forms!frmExpert.Suffix
.BusinessAddressStreet = Forms!frmExpert.Address
.BusinessAddressCity = Forms!frmExpert.City
.BusinessAddressState = Forms!frmExpert.State
.BusinessAddressPostalCode = Forms!
frmExpert.ZipCode
.Email1Address = Forms!frmExpert.Email
.BusinessTelephoneNumber = Forms!
frmExpert.PhoneNumber
.BusinessFaxNumber = Forms!frmExpert.FaxNumber
End With

'***** I'D LIKE TO AVOID HAVING TO DO THIS ********
If Forms!frmExpert.MiddleName = Null Then
ObjOutlookContact.MiddleName = Forms!
frmExpert.MiddleName
End If

ObjOutlookContact.Display

'Quit Microsoft Outlook
Set ObjOutlook = Nothing

Exit Sub

StartError:
ErrorHandler.ERRHAND
Exit Sub

End Sub
 
T

Terry

Hi Don,
Try this .BusinessAddressStreet = NZ(Forms!frmExpert.Address,"")
If the field is null the NZ function returns "" which is an empty string.
Regards
 
D

Don Cardoza

Perfect!
Thank you!
-----Original Message-----
Hi Don,
Try this .BusinessAddressStreet = NZ(Forms! frmExpert.Address,"")
If the field is null the NZ function returns "" which is an empty string.
Regards




.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top