OK here goes,
Here's my code ,don't laugh.
This is done in Access to transfer to OUtlook.
My problem is that I thought once the for loop closed that the itm.FullName
that I matched would close also.Down where the With itm code is placed is
where I'm having Trouble. I originally had the with itms code inside the
If strDecision = "0" Then ' User wishes to to Overwrite an itm. loop.
I then got an if without else error message.
When I moved it out of the loop the message went away but I don't think the
code works the way I have it.
Thanks
Again Carol
Private Sub cmdOutlookExport_Click()
'Declare the pfld variable as Public so it can be set in one procedure
'and used in another
Dim fld As Outlook.MAPIFolder
Dim appOutlook As Outlook.Application
Dim nms As Outlook.NameSpace
Dim itms As Outlook.Items
Dim itm As Outlook.ContactItem
Set appOutlook = CreateObject("Outlook.Application")
Set nms = appOutlook.GetNamespace("MAPI")
Set fld = nms.GetDefaultFolder(olFolderContacts) 'Set what kind of folder
Set itms = fld.Items 'Outlook folder item
On Error GoTo ErrorHandler
Dim strCustomerID As String
Dim strFName1 As String
Dim … All the rest of the variables that I wish to transfer to.
strCustomerID = Parent!txtCustomerID
strFName1 = FirstName_Concatenate_For_Outlook()
… assign all the other variables here
'Check Outlook for duplicates
For Each itm In itms
If strLName1 = itm.LastName Then
strNameList = strNameList & ";" & itm.FullName
End If
Next
If strNameList = "" Then
MsgBox "Looped through list" & strNameList & " is strNameList"
End If
If strNameList <> "" Then
DoCmd.OpenForm "frmDuplicateValue", OpenArgs:=strNameList,
WindowMode:=acDialog
'Stop here and wait until form goes away
'MsgBox "This should only be triggered if duplicate value found"
If Not IsLoaded("frmDuplicateValue") Then
MsgBox "Update has been canceled."
Exit Sub
End If 'Not Is Loaded
strDecision = Forms("frmDuplicateValue").Tag
DoCmd.Close acForm, "frmDuplicateValue"
End If 'strNameList<>""
'Separate the option from the FullName
strNameList = Mid(strDecision, 2)
strDecision = Replace(strDecision, Mid(strDecision, 2), "")
If strDecision <> "N" And strDecision <> "0" Then
‘ This means that the user wishes to update the information rather than
make new or overwrite.
For Each itm In itms
'Debug.Print itm.FullName
If strNameList = itm.FullName Then
Debug.Print itm.FullName & " is found"
With itm
If Len(.CustomerID) = 0 Then .CustomerID = strCustomerID
If Len(.FirstName) = 0 Then .FirstName = strFName1
… All the other fields to update here.
.Save
End With
MsgBox "Contact Updated Successfully"
Exit Sub
End If 'strNameList = itm.FullName
Next
End If 'strDecision <> "N" And strDecision <> "0"
If strDecision = "0" Then ' User wishes to to Overwrite an itm.
For Each itm In itms
If strNameList = itm.FullName Then
End If 'strNameList = itm.FullName
Exit For
Next
With itm ' This is where I want to use either this with itm to
overwrite or the one following to add.
Else ' if decision is new
Set itm = itms.Add
With itm
End If 'strDecision = "0"
'Write values from variables to fields in the new Contact item
'Standard Contact fields
.CustomerID = strCustomerID
.FirstName = strFName1
… All the other fields to deal with…
.Close (olSave)
End With
MsgBox " Contact exported! from Form"
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End Sub