I got the VBA script that I wrote to work completely well, with the help of Merle and George (THANK YOU!).
Now I have to try to figure out how to get rid of the extra field ("Transferred") in my database and add some error handling in the code. Because once it goes through it puts a check mark in the box under the field "Transferred" in the Assecc database and then it skips the whole contact information, so if any new information (i.e. Notes, change of phone numbers, etc...,) is put into the database then it won't get updated. Does that make sense?
Here's the code I have now:
Function TransferContacts()
'Transfer contact records from Contacts to Outlook.
Dim rst As New ADODB.Recordset
Dim cnn As New ADODB.Connection
Dim appOutlook As New Outlook.Application
Dim ns As Outlook.Namespace
Dim fldContacts As Outlook.MAPIFolder
Dim itms As Outlook.Items
Dim objContactFolder As Object
'This is the only error handler I have in the code.
On Error GoTo ErrHandler
Set cnn = CurrentProject.Connection
Set appOutlook = CreateObject("Outlook.Application")
Set ns = appOutlook.GetNamespace("MAPI")
Set fldContacts = ns.GetDefaultFolder(olFolderContacts)
Set itms = fldContacts.Items
'Prevent duplicate contacts in Outlook.
'There's got to be an easier way to check for duplicates instead of putting the field "Transferred" in my database.
rst.Open "SELECT * FROM Lead_Generation_Contacts WHERE Transferred = 0", cnn, adOpenKeyset, adLockOptimistic
'Prevent error when recordset is empty, meaning there are no new contact records to transfer.
'And if there's not any new contacts, I need it to check for new information in the database, or updated information, etc.. New phone number, new notes to ADD ON not to OVERWRITE.
If rst.RecordCount = 0 Then
MsgBox "There are no new contact records to transfer", vbOKOnly, "Transfer stopped"
Exit Function
End If
rst.MoveFirst
'These are the only fields in the database that I need to pull out and either add to Outlook 2003 Contacts or update in Outlook 2003 Contacts
Do While Not rst.EOF
Set objContactFolder = itms.Add("IPM.Contact")
With objContactFolder
.CustomerID = Nz(rst!ContactNo)
.FullName = Trim(Nz(rst!ContactOneFirstName, "") & " " & Nz(rst!ContactOneLastName, ""))
.AssistantName = Trim(Nz(rst!ContactTwoFirstName, "") & " " & Nz(rst!ContactTwoLastname, ""))
.HomeTelephoneNumber = Nz(rst!HomePhone)
.MobileTelephoneNumber = Nz(rst!CellPhone)
.Body = Nz(rst!Notes)
.Email1Address = Nz(rst!Email)
.HomeAddress = Trim(Nz(rst!Address, "") & " " & Nz(rst!City, "") & ", " & Nz(rst!State, "") & " " & Nz(rst!Zip, ""))
.ManagerName = Nz(rst!LenderAssignedTo)
' .Created = Nz(rst!InitialDateProspected)
.Close olSave
End With
Set objContactFolder = Nothing
'This is the field in the database I need to be able to remove, but still have it figure out if there's been new information added to the database contact or changed information, the only field I don't want it to overwrite is the ( .Body = Nz(rst!Notes) ) which the field is called notes in the database.
rst.Update "transferred", -1
rst.MoveNext
Loop
Set cnn = Nothing
Set appOutlook = Nothing
Set ns = Nothing
Set fldContacts = Nothing
Set itms = Nothing
Exit Function
'Need more Error handlers then this, so nothing gets messed up in the database and it is all correct when I change information or add information to the database.
ErrHandler:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"
End Function
Thanks,
Mickey
Now I have to try to figure out how to get rid of the extra field ("Transferred") in my database and add some error handling in the code. Because once it goes through it puts a check mark in the box under the field "Transferred" in the Assecc database and then it skips the whole contact information, so if any new information (i.e. Notes, change of phone numbers, etc...,) is put into the database then it won't get updated. Does that make sense?
Here's the code I have now:
Function TransferContacts()
'Transfer contact records from Contacts to Outlook.
Dim rst As New ADODB.Recordset
Dim cnn As New ADODB.Connection
Dim appOutlook As New Outlook.Application
Dim ns As Outlook.Namespace
Dim fldContacts As Outlook.MAPIFolder
Dim itms As Outlook.Items
Dim objContactFolder As Object
'This is the only error handler I have in the code.
On Error GoTo ErrHandler
Set cnn = CurrentProject.Connection
Set appOutlook = CreateObject("Outlook.Application")
Set ns = appOutlook.GetNamespace("MAPI")
Set fldContacts = ns.GetDefaultFolder(olFolderContacts)
Set itms = fldContacts.Items
'Prevent duplicate contacts in Outlook.
'There's got to be an easier way to check for duplicates instead of putting the field "Transferred" in my database.
rst.Open "SELECT * FROM Lead_Generation_Contacts WHERE Transferred = 0", cnn, adOpenKeyset, adLockOptimistic
'Prevent error when recordset is empty, meaning there are no new contact records to transfer.
'And if there's not any new contacts, I need it to check for new information in the database, or updated information, etc.. New phone number, new notes to ADD ON not to OVERWRITE.
If rst.RecordCount = 0 Then
MsgBox "There are no new contact records to transfer", vbOKOnly, "Transfer stopped"
Exit Function
End If
rst.MoveFirst
'These are the only fields in the database that I need to pull out and either add to Outlook 2003 Contacts or update in Outlook 2003 Contacts
Do While Not rst.EOF
Set objContactFolder = itms.Add("IPM.Contact")
With objContactFolder
.CustomerID = Nz(rst!ContactNo)
.FullName = Trim(Nz(rst!ContactOneFirstName, "") & " " & Nz(rst!ContactOneLastName, ""))
.AssistantName = Trim(Nz(rst!ContactTwoFirstName, "") & " " & Nz(rst!ContactTwoLastname, ""))
.HomeTelephoneNumber = Nz(rst!HomePhone)
.MobileTelephoneNumber = Nz(rst!CellPhone)
.Body = Nz(rst!Notes)
.Email1Address = Nz(rst!Email)
.HomeAddress = Trim(Nz(rst!Address, "") & " " & Nz(rst!City, "") & ", " & Nz(rst!State, "") & " " & Nz(rst!Zip, ""))
.ManagerName = Nz(rst!LenderAssignedTo)
' .Created = Nz(rst!InitialDateProspected)
.Close olSave
End With
Set objContactFolder = Nothing
'This is the field in the database I need to be able to remove, but still have it figure out if there's been new information added to the database contact or changed information, the only field I don't want it to overwrite is the ( .Body = Nz(rst!Notes) ) which the field is called notes in the database.
rst.Update "transferred", -1
rst.MoveNext
Loop
Set cnn = Nothing
Set appOutlook = Nothing
Set ns = Nothing
Set fldContacts = Nothing
Set itms = Nothing
Exit Function
'Need more Error handlers then this, so nothing gets messed up in the database and it is all correct when I change information or add information to the database.
ErrHandler:
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Error"
End Function
Thanks,
Mickey