From Access to Outlook-Add Your Custom Data to the Outlook Calendar

Probably the most used Microsoft Office Application is Outlook. Often Outlook is open all day to receive emails, schedule appointments, and manage Contact information. Unfortunately, it is not always easy to share data between Outlook and Access.

If you use an Access Database that stores almost any kind of dates such as shipping dates, appointments, service scheduling, and maintenance schedules you may wish you could save that information to the Outlook Calendar with just the click of a button.


In this article, we will show you how to do just that. We will show you how to add data from an Access Form to an Outlook Calendar Appointment with just one mouse click.


One of the best things about this method is its flexibility. You can create an Access Form that holds the precise data you want to save to Outlook. We have set up a Table and Form to hold the Appointment Information we want. Yours can be set up differently. You may use a maintenance schedule, client appointments, project progress dates, or almost any kind of information with a date.


We can add an Appointment to the Outlook Calendar with just one click on our Form’s “Link” button.


The Appointment Manager Form

The Appointment Manager Form


Let us look at the code behind that button. First we need to make sure the Current Record is saved. If a Form is “Dirty” that means there has been a change made to the data on the Form, but it has not yet been saved. In order for Access to make the Form “Dirty = False” it has to save the current data on the Form.

Private Sub btnAddApptToOutlook_Click()
    If Me.Dirty Then
        Me.Dirty = False
    End If


Next, we check to make sure the appointment has not already been added to Outlook. On our form we have a checkbox that indicates if that has been done. If the appointment already exists, we inform the user and Exit the Sub.

If Me.chkAddedToOutlook = True Then
    MsgBox "This appointment has already added to Microsoft Outlook", vbCritical
    ' Exit the procedure
    Exit Sub
Else
    ' Add a new appointment.


We are using “late binding” so our code works in almost all versions of Outlook. This is crucial if you distribute your Database or take it with you in your thumb drive to use on other computers. However, one drawback of using late binding is that it is slower than early binding. With late binding we use an Object variable:

' Use late binding to avoid the "Reference" issue
Dim olapp As Object ' Outlook.Application
Dim olappt As Object ' olAppointmentItem


This is how we would do it if we were using “early binding”:

Dim olApp As Outlook.Application
Dim olappt As Outlook.AppointmentItem
Set olapp = CreateObject("Outlook.Application")
Set olappt = olapp.CreateItem(olAppointmentItem)


We need to know if Outlook is already open because there are two methods of instantiating Outlook and other Office Applications. You use the CreateObject Function if Outlook is not open, and you use the GetObject Function if Outlook is already open.


To determine if Outlook is open we are using the isAppThere Custom Function which we have included at the end of this article. This Function returns True if the application is open, and False if it is not open.

If isAppThere("Outlook.Application") = False Then
    ' Outlook is not open, create a new instance
    Set olApp = CreateObject("Outlook.Application")
Else
    ' Outlook is already open--use this method
    Set olapp = GetObject(, "Outlook.Application")
End If


An Outlook Appointment is called an Item, and is referenced in Outlook as olAppointmentItem; its constant value is 1. We must use the value 1 in order to create the Appointment Item so we put the 1 in parentheses after “CreateItem”. With late binding, we must use the numbers. In early binding, we could have put olAppointmentItem in the parentheses.


To create a new Appointment Item we use the CreateItem Function instead of the CreateObject Function.

Set olAppt = olApp.CreateItem(1) ' olAppointmentItem


Our example adds data from the Form to Outlook Appointment Properties. Your Fields and Form Controls probably will not have the same names as the Outlook Appointment Properties. This is no problem because you can easily manage that difference in code. For example, if you have a TextBox named “txtProjectDueDate”. You can use that as your Outlook “Start” Property like this:

Start = Me.txtProjectDueDate


You may need to use more Appointment Properties than we have in our example. You can use the Object Browserto see the complete list of Appointment Properties. It is a long list with more than enough Properties to meet almost anyone’s needs.


Now let us examine how to add our data to the Appointment Properties:

With olAppt
    ' If There is no Start Date or Time on
    ' the Form use Nz to avoid an error

    ' Set the Start Property Value
    .Start = Nz(Me.txtStartDate, "") & " " & Nz(Me.txtStartTime, "")


“Start” is the first Appointment Property we are setting. Outlook uses both the Date and Time in the Start Property, but our Form has the date and time in seperate TextBoxes. To handle this, we just concatenate the date and time together like this:

Me.txtStartDate & ” ” & Me.txtStartTime

In order to avoid errors, we will use the Nz Function to handle nulls and zero length strings in our TextBoxes or ComboBoxes. The Nz Function gets the value you designate if the value is Null or a Zero Length String after the comma like this: Nz(Me.myTextBox, 0). This will not change the value in the TextBox, but it will change the value in the code.


Outlook does not require that the time be added to the Start Property. However, to avoid errors, if you leave the Start Time blank, then you need to leave the End Time blank also. You can do this if you just want to add the due date for a project without the Time.


The Outlook Appointment Property “End” is not always required but you may get an error if you do not supply any value.

    ' Set the End Property Value
    .End = Nz(Me.txtEndDate, "") & " " & Nz(Me.txtEndTime, "")  


Here we are using the Nz Functionto provide a 0 value in case the txtApptLength TextBox is Null.

    .Duration = Nz(Me.txtApptLength, 0)     ' vbNullString uses a little less memory than ""
    .Subject = Nz(Me.cboApptDescription, vbNullString)

    .Body = Nz(Me.txtApptNotes, vbNullString)


We have provided a Combo box to select Locations from a Table to add to our Form along with other information about the location in unbound TextBoxes. Here we add the Outlook Location Property.

    .Location = Nz(Me.txtLocation, vbNullString)


If we want Outlook to Remind us when our appointment is near we check the chkAppointReminder Checkbox on our Form and enter the number of minutes we want to be reminded before our Appointment. If the chkAppointReminder Checkbox is checked but we forget to enter our reminder minutes it will cause an error. To avoid that error we use code to change no minutes to 30 minutes.

    If Me.chkApptReminder = True Then
        If IsNull(Me.txtReminderMinutes) Then
            Me.txtReminderMinutes.Value = 30
        End If
        .ReminderOverrideDefault = True
        .ReminderMinutesBeforeStart = Me.txtReminderMinutes
        .ReminderSet = True
    End If


Now we can save the Appointment Properties and start cleaning up:

        .Save
    End WithEnd If ' Release the Outlook object variables.
    Set olAppt = Nothing
    Set olApp = Nothing    ' Set chkAddedToOutlook to checked
    Me.chkAddedToOutlook = True

    ' Save the Current Record because we checked chkAddedToOutlook
    If Me.Dirty Then
        Me.Dirty = False
    End If

    ' Inform the user
    MsgBox "Appointment Added!", vbInformation
End Sub


We have shown you how you can transfer Calendar data from Access to Outlook with just one mouse click using the Appointments Database. With just a little modification of the code, you can use your present Forms to send information with Dates to the Outlook Calendar.


To download the code used in this article visit our Gaining Access Website.


You can download the completed and free Report Date Dialog Form in the US or UK Version at our Gaining Access website.


More Free Downloads:
Pop-up Calendar
ScopeSight for Access Reports Demo: get the exact data you want on your Access Reports.
Free Church Management Software now with new Contributions management.
Code Samples


Get the Access and Outlook Appointment Manager to manage all of your Outlook Calendar Appointments and Access dated information.


Happy computing,
Patrick (Pat) Wood
Gaining Access

12 Comments

  1. Brian
    Posted September 17, 2008 at 11:47 AM | Permalink

    Excellent tutorial!
    My application is for consumable items which have a shelf life. The alarm is to remind me when an item is about to expire so I can order more and remove it from the floor. However the items could be consumed before the shelf life expires. In that case I would like to remove the entry from Outlook so I do not get an alarm for something that is used up. How would I go about creating a button in Access that would remove the entry from Outlook?

  2. Patrick Wood
    Posted September 17, 2008 at 12:14 PM | Permalink

    Hi Brian,

    That would be a great feature to have!

    It should not be too hard to do except we need to be careful to delete only the Appointments we want to delete.
    Possibly the best way to do this is to enable drill-down selection starting with dates, categories, and finally, selecting the exact Appointment(s) using a ListBox on a Dialog Form.

    I will check into it and see what I can come up with. It may be a few days before I can post it.

    My next Post here will be about completing and using the Date part of a Dialog Form and I should have it up today. You can also read that article and download some of the code from my other site listed below.

    Best Regards,
    Pat
    http//gainingaccess.com

  3. Larry H.
    Posted October 16, 2008 at 12:34 PM | Permalink

    Hi Patrick
    Great information and very timely regarding a problem I am trying to figure out.
    Attempting to schedule an Outlook appointment from MS Access. The users have multiple Outlook mailboxes. How do I setup an appointment in a calendar other than the default calendar ?

    Thanks ahead for the help and best regards;
    Larry

  4. Patrick Wood
    Posted October 16, 2008 at 3:08 PM | Permalink

    Hi Larry,

    That is a great question. All you need to do is modify the code a little.

    Dim olApp As Object ‘Outlook.Application
    Dim olAppt As Object ‘olAppointmentItem
    ‘ Declare a variable for the MAPI Namespace
    Dim olNS As Object ‘ Outlook Namespace

    If isAppThere(“Outlook.Application”) = False Then
    ‘ Outlook is not open, create a new instance
    Set olApp = CreateObject(“Outlook.Application”)
    Else
    ‘ Outlook is already open–use this method
    Set olApp = GetObject(, “Outlook.Application”)
    End If

    ‘ Set a Pointer to the NameSpace
    Set olNS = olApp.GetNamespace(“MAPI”)
    ‘ Now we “pick” the Appointment Item Folder
    ‘ This opens the Outlook PickFolder Dialog Form
    Set olAppt = olNS.PickFolder

    ‘ Create an Instance of the AppointmentItem
    ‘ Add the Form data to Outlook Calendar
    With olAppt.Items.Add(1) ‘ olAppointmentItem

    ‘Then continue as shown in the Post above
    ‘ Set the Start Property Value
    .Start = Nz(Me.txtStartDate, “”) & ” ” & Nz(Me.txtStartTime, “”)

    Let us know how that works for you

    Best Regards,
    Pat

  5. Ritzvy
    Posted March 20, 2009 at 9:55 AM | Permalink

    Pat,

    Very simple and neately explained for a begginers.

    You are GOOOOOOD.

    Thanks Ritzvy

  6. Posted March 20, 2009 at 10:45 AM | Permalink

    Thank you Ritzvy. You are very kind. Helping beginners was exactly what I wanted to do with this post.
    Pat

  7. Joel Madrid
    Posted August 7, 2009 at 6:39 AM | Permalink

    I really love the tutorial!

    Is there a way to pass these appointments from MS Access to a calendar that is stored in Access, Excel, or Word.

    I have thousands of appointments that clinical therapists have made with their patients (stored in Access table) and want to populate a calendar with that information, but don’t necessarily want the info to go into an Outlook calendar.

    thanks for any help!!!

    • Posted August 7, 2009 at 10:33 AM | Permalink

      Thank you, Joel. I am glad you find the articles useful.
      To answer your question, a custom calendar can be done. Access makes a very good platform for such a calendar. To talk about it more click the “Contact Me” link in the upper left of the web page.
      Best Regards.

  8. Paul P
    Posted August 29, 2009 at 10:58 PM | Permalink

    I used the code you provided above in another comment in order to add the appointment to a different calendar but regardless of the calendar I chose in the folder selection window, it still juar added it to the main calendar for some reason. How can that be fixed?

    Also, is there anyway to program it to just add it to a specific calendar (like a public calendar) all the time.

    • Posted August 30, 2009 at 1:41 AM | Permalink

      Hi Paul,

      Some time ago I tried to solve the same problem you are having without success. At the present, I am unable to take on this issue. You may be able to find a solution for this on an Outlook Forum like the one at the link below. (You may need to copy the link and paste it into your browser.)

      http://www.microsoft.com/office/community/en-us/default.mspx?dg=microsoft.public.outlook.calendaring&lang=en&cr=US

      If you find a solution, I would love to hear about it.

      My best regards to you.

      • Paul P
        Posted September 4, 2009 at 3:08 PM | Permalink

        Patrick, I got it fixed. I posted it on UtterAccess Forums and someone pointed out that it was setting olAppt twice and that the second instance was to the default calendar, so I deleted the lines:

        Set olAppt = olApp.CreateItem(1) ‘ olAppointmentItem
        With olAppt

        and I also had to delete an End With later down the code.

        Now when I add the appointment and choose a public calendar it goes straight to it.

        Is there any way to set a label (color label) for an appointment in the Access form? I have a yellow custom label in the calendar that I named “AV/Equipment Request” that I want to be able to set in the Access form.

        Thanks,
        Paul

  9. Posted September 4, 2009 at 5:19 PM | Permalink

    That is great Paul. Thank you for letting us know about it.

    You can find some help with changing the color of the labels at the link below. It may take some studying to understand how it works. You will need to set a Reference to the Microsoft CDO 1.21 Library. On my computer it is located at C:\Program Files\Common Files\System\MSMAPI\1033\CDO.DLL.

    http://www.outlookcode.com/codedetail.aspx?id=139

    Best Regards


Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*