Select to view content in your preferred language

Update fields via a form using VBA

909
0
05-10-2011 08:42 AM
DavidSloan
New Contributor
Hi

I'm stuck with some VBA code and would really appeciate some help.  I've got a form which shows a multi-select list where the user can select addresses.  If those addresses are selected, I want the relevant record to be changed to 'returned' in the field called 'status', but it doesn't work.
EDCODE is the district number for addresses and selectedED is the variable for the district that the user has previously selected.

Private Sub CommandButton1_Click()

'Set all selected addresses to 'returned'

Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

Dim pMaps As IMaps
Set pMaps = pMxDoc.Maps

Dim pMap As IMap
Set pMap = pMaps.Item(0)

Dim pLayer As ILayer

Dim pPointerLayerDef As IFeatureLayerDefinition

Dim g As Integer
g = 0

For g = 0 To pMap.LayerCount - 1
Set pLayer = pMap.Layer(g)
If pLayer.Name = "Pointer" Then
Set pHouseLayerDef = pMap.Layer(g)
End If
Next g

Dim pHouseFLayer As IFeatureLayer
Set pHouseFLayer = pPointerLayerDef

Dim pHouseFClass1 As IFeatureClass
Set pHouseFClass1 = pHouseFLayer.FeatureClass

Dim pHouseFields As IFields
Set pHouseFields = pHouseFClass1.Fields

Dim intStatus As Integer
intStatus = pHouseFields.FindField("STATUS")

Dim pField As IField
Set pField = pHouseFields.Field(intStatus)

Dim pFieldEdit As IFieldEdit
Set pFieldEdit = pField

pFieldEdit.Editable = True

Dim intEdCode As Integer
intEdCode = pHouseFields.FindField("EDCODE")

Dim pFilter1 As IQueryFilter
Set pFilter1 = New QueryFilter
pFilter1.WhereClause = "EDCODE = " & SelectedED

Dim pFCursor1 As IFeatureCursor
Set pFCursor1 = Nothing
Set pFCursor1 = pHouseFClass1.Update(pFilter1, False)

Dim pFeature1 As IFeature
Set pFeature1 = pFCursor1.NextFeature

Dim x As Integer
x = 0

Do Until pFeature1 Is Nothing

        If ListBox1.selected(x) Then
        pFeature1.Value(intStatus) = "returned"
        Else
        pFeature1.Value(intStatus) = " "
        End If
        pFCursor1.UpdateFeature pFeature1
        Set pFeature1 = pFCursor1.NextFeature
        x = x + 1

Loop

Set pFCursor1 = Nothing

frmAddressUpdate.Hide

End Sub

Any help appreciated - thanks!
0 Kudos
0 Replies