VBOffice

Copy Contacts from Access to Outlook

With a few lines of code you can copy recordsets from an Access database to Outlook.

Last modified: 2008/08/23 | Accessed: 27.359  | #67
◀ Previous sample Next sample ▶
SAM SAM
SAM automatically sets the sending account, or sender address, and folder for sent items based on several criteria.

This sample reads the fields "Fullname" and "EMailAddress" from the table "Customers" of your Access database, and it creates for every found recordset a new contact in Outlook.

In the procedure "ConnectDB" change the values for the variables "File" (file name of your Access database), "Fields" (list of field names to be read), and "Table" (name of the database table). Repeat the field names in the procedure "AddContactsFromAccess" as shown, and assign the values to the appropriate ContactItem properties.

You also need to add a reference to a "Microsoft ActiveX Data Objects 2.x Library" via Tools/References. If there's more than one library of that type, choose the most current one.


tip  How to add macros to Outlook
Public Sub AddContactsFromAccess()
  Dim Contact As Outlook.ContactItem
  Dim Name As String
  Dim Fields As ADODB.Fields

  ConnectDB
  Set Fields = m_Rs.Fields

  While Not m_Rs.EOF
    Set Contact = Application.CreateItem(olContactItem)

    Name = "Fullname"
    If IsNull(Fields(Name).Value) = False Then
      Contact.FullName = Fields(Name).Value
    End If

    Name = "EMailAddress"
    If IsNull(Fields(Name).Value) = False Then
      Contact.FullName = Fields(Name).Value
    End If

    Contact.Save
    m_Rs.MoveNext
  Wend

  m_Rs.Clone: Set m_Rs = Nothing
  m_Cn.Close: Set m_Cn = Nothing
End Sub

Private Sub ConnectDB()
  Dim File As String
  Dim Table As String
  Dim Fields As String

  File = "c:	est.mdb"

  Fields = "Fullname, EMailAddress"
  Table = "Customers"

  Set m_Cn = New ADODB.Connection
  With m_Cn
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = File
    .CursorLocation = adUseClient
    .Mode = adModeShareDenyNone
    .Open
  End With

  Set m_Rs = New ADODB.Recordset
  With m_Rs
    .CursorLocation = adUseClient
    .CursorType = adOpenStatic
    .LockType = adLockOptimistic
    Set .ActiveConnection = m_Cn
    .Open ("SELECT " & Fields & " FROM " & Table)
  End With
End Sub
OLKeeper OLKeeper
OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or e-mails.
email  Send a message