Access.2007.VBA.Bibl..

(John Hannent) #1
Create a new company phone record in the target table.

rstTarget.AddNew
rstTarget![CompanyID] = lngCompanyID
rstTarget![Description] = “Company Phone”
Else
rstTarget.Edit
End If

rstTarget![IDOrPhone] = _
Nz(rstSource!CompanyMainTelephoneNumber)
rstTarget.Update
rstTarget.Close

The Contact IDs and Phones in tblContactIDsPhones are updated in a similar manner: First the
code searches for a value in one of these fields, and if it is found, the strDescriptionvariable
is set with the phone or ID description, and a record is sought using ContactID and strDescription.
If a record is found, it is updated; otherwise a new record is created in tblContactIDsAndPhones
and the phone number or ID is written to it:

UpdateContactIDs:

Search for a matching Contact ID record in the target table, and update it if found; otherwise, cre-
ate a new record, and write Contact ID data to it.

strTarget = “tblContactIDsPhones”
Set rstTarget = dbs.OpenRecordset(strTarget, _
dbOpenDynaset)

If Nz(rstSource![AssistantTelephoneNumber]) <> “” Then
strDescription = “Assistant Phone”
strSearch = “[ContactID] = “ & lngContactID _
& “ And [Description] = “ & Chr$(39) & _
strDescription & Chr$(39)
Debug.Print “Search string: “ & strSearch
rstTarget.FindFirst strSearch
If rstTarget.NoMatch = True Then

Create a new contact ID record in the target table.

rstTarget.AddNew
rstTarget![ContactID] = lngContactID
rstTarget![Description] = strDescription
Else
rstTarget.Edit
End If
rstTarget![IDOrPhone] = _
Nz(rstSource![AssistantTelephoneNumber])
rstTarget.Update
End If

Part II Writing VBA Code to Exchange Data between Office Components

Free download pdf