New Contact from Access

  • Thread starter Thread starter Don Cardoza
  • Start date Start date
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
 
Hi Don,
Try this .BusinessAddressStreet = NZ(Forms!frmExpert.Address,"")
If the field is null the NZ function returns "" which is an empty string.
Regards
 
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




.
 
Back
Top