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: 29.062  | #67
◀ Previous sample Next sample ▶
Category-Manager Category-Manager
Access the master category list in the blink of an eye, share your color categories in a network, get a reminder service, and more.

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
SAM SAM
Determine the "identity" of your emails. Set with SAM the sender, signature and folder for sent items with the help of rules.
email  Send a message