Select to view content in your preferred language

Select By Attributes with VBScript

5243
10
Jump to solution
05-22-2012 06:35 AM
SandraDema
Emerging Contributor
Is it possible to programmatically select features by attributes  using vbscript?
I have introduced a unique identifier (not OID) to the attribute table that I need to search for, but i can't seem to find a method to do so.

I know this seems like a very simple question, but I have failed to find a good source of documentation pertaining to ArcPad and vbscript.
If anyone knows of such a source, please share.
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
ThaiTruong
Deactivated User
Probably  RecordSet::Find Method doesn't work for AXF file.  I don't have an axf file to test with, but you can try to use this code with your file to see if it works:

Dim objLayer, objRS, objRect, myQuery, myRec, myValue Set objLayer=application.map.layers("My Layer Name") myValue = 357799 Set objRS=objLayer.records If (Not objRS Is Nothing) Then  objRS.MoveFirst  For i = 0 To objRS.RecordCount - 1   If objRS.Fields("FieldName").Value = myValue Then    myRec = i+1   End If   objRS.MoveNext  Next End If  If (myRec > 0) Then  'Zoom to feature  Set objRect=objRS.Fields.Shape.Extent  Call objRect.ScaleRectangle(0.5)  Application.Map.Extent = objRect  Application.Map.Refresh Else  msgbox "Feature Not Found!" End If Set objRect = Nothing Set objLyr = Nothing Set objRS = Nothing Set pDS = Nothing

View solution in original post

0 Kudos
10 Replies
HannahFerrier
Deactivated User
Hi Sandra,

If you have checked out your data from ArcMap using the ArcPad Data Manager, your project should already have a set of automatically generated Query Forms. These forms are the same as your Edit Forms, but are specifically used to search your data. On the Browse Toolbar (pointy hand on the globe) select the Find Tool (binoculars) to open your Query Forms. Here is an example from the Riverside Sample Dataset:

[ATTACH=CONFIG]14539[/ATTACH]

From here I can enter the ID of the Poles Record I am searching for and tap OK to see my Results Tab:

[ATTACH=CONFIG]14540[/ATTACH]

Now I can select my record and use the tools on the right-hand side to open the edit form, zoom to the record or navigate to the record.

All without writing a single line of code (my favorite!!)

For more info - check out this help article

HTH

Hannah Ferrier
0 Kudos
SandraDema
Emerging Contributor
Thanks so much for the reply.

I understand that I can use the built-in query feature. What I need is a way that I can query the data without user interaction.

I know that I can use SelectXY to search spatially when I have the XY coordinates. I'm sure there is a way to automate the query by attributes feature, I'm just coming up empty handed.
0 Kudos
ThaiTruong
Deactivated User
Since you have unique values in a field, you can try to use RecordSet::Find method to find a record that meets your search criteria.  The expression is a string in form: "[FIELD] = VALUE"

There will be one result is returned at each time you call the Find method.  And a return value of 0 tells you that no record was found.
0 Kudos
ThaiTruong
Deactivated User
This code snippet will help you get started.
In this example, it finds feature that [FieldName]="Lookup Value" from "My Layer Name", then zooms to that feature.

Sub SelectAndZoomTo
Dim objLayer, objRS, objRect, myQuery, myRec, myValue
Set objLayer=application.map.layers("My Layer Name")
myValue = "Lookup Value"
Set objRS=objLayer.records
myQuery = "[FieldName]= """ & myValue & """
myRec = objRS.Find(myQuery)

'If feature is found
If (myRec > 0) Then
 objRS.movefirst
 objRS.move(myRec-1)
 Set objRect=objRS.Fields.Shape.Extent
 Map.Extent=objRect
'If feture is Not found
Else
 msgbox "Feature NOT Found!"
End If
Map.Refresh
End Sub
0 Kudos
SandraDema
Emerging Contributor
When I run your code, I get error 800A01BD - "Object doesn't support this action" at line
myQuery = "[FieldName]= " & myValue      (the field is an integer, so I removed the quotes)

When I use the same query string in ArcMap, everything works as expected.

Ideas?
0 Kudos
ThaiTruong
Deactivated User
Hi ssdema,

I don't see anything wrong with the above code or your expression string!  It should center the feature on your map.
Just double check to see your layer's name & Field name spelled correctly.
0 Kudos
SandraDema
Emerging Contributor
I am using axf files. I have read that the Find method does not work with previous versions of ArcPad when utilizing axf files.  Could this still be the case (explaining why I am getting this error)?
0 Kudos
ThaiTruong
Deactivated User
Probably  RecordSet::Find Method doesn't work for AXF file.  I don't have an axf file to test with, but you can try to use this code with your file to see if it works:

Dim objLayer, objRS, objRect, myQuery, myRec, myValue Set objLayer=application.map.layers("My Layer Name") myValue = 357799 Set objRS=objLayer.records If (Not objRS Is Nothing) Then  objRS.MoveFirst  For i = 0 To objRS.RecordCount - 1   If objRS.Fields("FieldName").Value = myValue Then    myRec = i+1   End If   objRS.MoveNext  Next End If  If (myRec > 0) Then  'Zoom to feature  Set objRect=objRS.Fields.Shape.Extent  Call objRect.ScaleRectangle(0.5)  Application.Map.Extent = objRect  Application.Map.Refresh Else  msgbox "Feature Not Found!" End If Set objRect = Nothing Set objLyr = Nothing Set objRS = Nothing Set pDS = Nothing
0 Kudos
SandraDema
Emerging Contributor
I did a little playing around with the code you provided and was able to get the search function to work the way i needed it to. i have included this code below.

I was a bit hesitant at first. The field crew will check out approximately 1500 features at a time. I thought looping though all of those features would be very slow. 
As it turns out, the code runs relatively quickly.

Thanks so much to everyone who contributed.



Function SearchByAttributes(myValue)

Dim objLayer, objRS, objRect, myQuery, myRec
Set objLayer=application.map.layers("LayerName")
        Dim I
        myRec = 0
Set objRS=objLayer.records
If (Not objRS Is Nothing) Then
  objRS.MoveFirst
  For i = 0 To objRS.RecordCount - 1
   If objRS.Fields("FieldName").Value = myValue Then
    myRec = i+1
           Exit For
   End If
   objRS.MoveNext
  Next
End If

If (myRec > 0) Then
  'Zoom to feature
  Set objRect=objRS.Fields.Shape.Extent
         Call objRect.ScaleRectangle(1.5)
                'select feature (feature must be in edit mode for the selection to work)
  objRs.Bookmark = Map.SelectionBookmark
  Application.Map.Extent = objRect
  Application.Map.Refresh
Else
  msgbox "Feature Not Found!"
End If
Set objRect = Nothing
Set objLayer = Nothing
Set objRS = Nothing

End Function
0 Kudos