This VBA code demonstrates how to standardize a single address. The standardization results are displayed by this code.
[VBA]
If the Locator you are using supports intersection geocoding, then you must check each address to determine whether or not it is an intersection. Intersections are usually standardized differently than other addresses.
How to use
- Paste this code into VBA.
- If you are using ArcMap, add a geocoding service to the document, and make it the current geocoding service. You can do this by choosing the geocoding service on the Addresses tab of the Find dialog box.
- If you are using ArcCatalog, click a geocoding service in the ArcCatalog tree.
- Run the StandardizeAddress macro.
- When prompted, type the address that you want to find. Separate the address components with commas.
Private Const ERR_NUMBER_WRONGNUMBEROFINPUTS = 1001
Private Const ERR_DESCRIPTION_WRONGNUMBEROFINPUTS = "The wrong number of address fields were specified."
Private Const ERR_NUMBER_REQUIREDFIELDMISSING = 1002
Private Const ERR_DESCRIPTION_REQUIREDFIELDMISSING = "A required address field was missing."
Private Const MESSAGEBOX_TITLE = "Standardize Address Geocoding Developer Tip"
Public Sub StandardizeAddress()
Dim pApplication As esriFramework.IApplication '+++ reference to the parent application
Dim pGxApplication As esriCatalogUI.IGxApplication '+++ ArcCatalog application
Dim pMxApplication As esriArcMapUI.IMxApplication '+++ ArcMap application
Dim pGxObject As esriCatalog.IGxObject '+++ selected object in ArcCatalog
Dim pGxLocator As esriCatalog.IGxLocator '+++ selected locator in ArcCatalog
Dim pLocator As esriGeoDatabase.ILocator '+++ locator
Dim pAddressInputs As esriLocation.IAddressInputs '+++ IAddressInputs interface on the locator
Dim pAddressFields As esriGeoDatabase.IFields '+++ address input fields
Dim i As Long '+++ loop counter
Dim pField As esriGeoDatabase.IField '+++ field
Dim strAddressFields As String '+++ string containing address field names
Dim strAddress As String '+++ address string
Dim pAddressPropertySet As esriSystem.IPropertySet '+++ PropertySet containing input address components
Dim pAdvancedGeocoding As esriLocation.IAdvancedGeocoding '+++ IAdvancedGeocoding interface on the locator
Dim pStandardizedPropertySet As esriSystem.IPropertySet '+++ PropertySet containing the standardized address
Dim binIntersection As Boolean '+++ indicates if the address is an intersection address
Dim pAdvancedIntersectionGeocoding As esriLocation.IAdvancedIntersectionGeocoding '+++ IAdvancedIntersectionGeocoding on the locator
Dim pStandardizeFields As esriGeoDatabase.IFields '+++ Fields collection for a standardized address
Dim strStandardize As String '+++ string containing the standardize results
Dim pUID As esriSystem.UID '+++ UID for the LocatorExtension
Dim pLocatorExtension As esriLocationui.ILocatorExtension '+++ LocatorExtension object
Dim lngCurrentLocator As Long '+++ index to the current locator in ArcMap
On Error GoTo ErrorHandler
'+++ get a reference to the parent application
Set pApplication = ThisDocument.Parent
If (TypeOf pApplication Is esriCatalogUI.IGxApplication) Then
Set pGxApplication = pApplication
ElseIf (TypeOf pApplication Is esriArcMapUI.IMxApplication) Then
Set pMxApplication = pApplication
End If
'+++ get a locator from the application
If (TypeOf pApplication Is esriCatalogUI.IGxApplication) Then
Set pGxObject = pGxApplication.SelectedObject
If Not (TypeOf pGxObject Is esriCatalog.IGxLocator) Then
MsgBox "The selected object is not a locator."
Exit Sub
End If
Set pGxLocator = pGxObject
Set pLocator = pGxLocator.Locator
ElseIf (TypeOf pApplication Is esriArcMapUI.IMxApplication) Then
Set pUID = New esriSystem.UID
pUID.Value = "esriLocationUI.LocatorExtension"
Set pLocatorExtension = pApplication.FindExtensionByCLSID(pUID)
If pLocatorExtension.LocatorCount("Address") = 0 Then
MsgBox "There are no locators in the document.", vbCritical, MESSAGEBOX_TITLE
Exit Sub
End If
lngCurrentLocator = pLocatorExtension.CurrentLocator("Address")
If lngCurrentLocator = -1 Then
MsgBox "The ArcMap document does not have a current locator.", vbCritical, MESSAGEBOX_TITLE
Exit Sub
End If
Set pLocator = pLocatorExtension.Locator("Address", lngCurrentLocator)
End If
'+++ get the address input fields for the locator
If Not (TypeOf pLocator Is esriLocation.IAddressInputs) Then
MsgBox "The selected locator is not an address locator."
Exit Sub
End If
Set pAddressInputs = pLocator
Set pAddressFields = pAddressInputs.AddressFields
For i = 0 To pAddressFields.FieldCount - 1
Set pField = pAddressFields.Field(i)
strAddressFields = strAddressFields & pField.Name
If Not i = pAddressFields.FieldCount - 1 Then
strAddressFields = strAddressFields & ","
End If
Next i
'+++ prompt the user for the address
Do
strAddress = InputBox("Enter an address, separating the address fields with commas. " & _
vbNewLine & vbNewLine & strAddressFields & ":", MESSAGEBOX_TITLE, strAddress)
If strAddress = "" Then Exit Sub
'+++ construct the address PropertySet
Set pAddressPropertySet = ConstructAddressPropertySet(pAddressFields, strAddress)
If Not pAddressPropertySet Is Nothing Then Exit Do
Loop
'+++ standardize the address
Set pAdvancedGeocoding = pLocator
Set pStandardizedPropertySet = pAdvancedGeocoding.StandardizeAddress(pAddressPropertySet, binIntersection)
'+++ if this is an intersection, then get the standardize fields for intersection
If binIntersection Then
Set pAdvancedIntersectionGeocoding = pLocator
Set pStandardizeFields = pAdvancedIntersectionGeocoding.StandardizeIntersectionFields
Else
Set pStandardizeFields = pAdvancedGeocoding.StandardizeFields
End If
'+++ get the standardized address components
For i = 0 To pStandardizeFields.FieldCount - 1
Set pField = pStandardizeFields.Field(i)
If Not (pField.Type = esriFieldTypeBlob Or pField.Type = esriFieldTypeGeometry Or pField.Type = esriFieldTypeOID) Then
strStandardize = strStandardize & pField.AliasName & ": " & pStandardizedPropertySet.GetProperty(pField.Name) & vbNewLine
End If
Next i
MsgBox strStandardize, vbInformation, MESSAGEBOX_TITLE
Exit Sub
ErrorHandler:
If (Err.Number = ERR_NUMBER_WRONGNUMBEROFINPUTS Or Err.Number = ERR_NUMBER_REQUIREDFIELDMISSING) Then
If MsgBox("An unexpected error occurred." & vbNewLine & Err.Number & ": " & Err.Description, vbRetryCancel, MESSAGEBOX_TITLE) = vbCancel Then Exit Sub
Resume Next
End If
End Sub
Private Function ConstructAddressPropertySet(pFields As esriGeoDatabase.IFields, strAddress As String) As esriSystem.IPropertySet
Dim i As Long '+++ loop counter
Dim strAddressComponents() As String '+++ array of address components
Dim pField As esriGeoDatabase.IField '+++ address field
Dim pPropertySet As esriSystem.IPropertySet '+++ PropertySet containing address components
'+++ parse the address string to get the address components
strAddressComponents = Split(strAddress, ",")
If Not UBound(strAddressComponents) = pFields.FieldCount - 1 Then
Err.Raise ERR_NUMBER_WRONGNUMBEROFINPUTS, MESSAGEBOX_TITLE, ERR_DESCRIPTION_WRONGNUMBEROFINPUTS
Exit Function
End If
'+++ construct the PropertySet containing the address components
Set pPropertySet = New esriSystem.PropertySet
For i = 0 To UBound(strAddressComponents)
Set pField = pFields.Field(i)
If pField.Required = True And strAddressComponents(i) = "" Then
Err.Raise ERR_NUMBER_REQUIREDFIELDMISSING, MESSAGEBOX_TITLE, ERR_DESCRIPTION_REQUIREDFIELDMISSING
Exit Function
End If
pPropertySet.SetProperty pField.Name, strAddressComponents(i)
Next i
Set ConstructAddressPropertySet = pPropertySet
End Function