Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've published a ArcServer service that is going to be consumed by out of the box, standard ArcGIS on the desktop. My question is, is it possible to write custom tools in VB to query layers from that service in the ArcMap interface. I'm using ArcGIS 9.3. Thanks in advance.

share|improve this question
up vote 3 down vote accepted

Yes, you can. Basically, you have two options to query ArcGIS Server layers in ArcMap via ArcObjects.

The first way is to get a reference to an individual AGS sublayer (IMapServerSubLayer). Those layers (i.e. sublayers in this context) which have the Query capability enabled can be cast to IFind interface. You can use this interface to perform simple (and quite limited) querying.

The second way means getting access to the actual AGS server object and making use of its search capabilities. First you get a reference to the top-level AGS layer (IMapServerLayer) and use its GetConnectionInfo method to obtain information about the connection. Then you would get access to the server object (map server) and call one of the IMapServer methods, such as QueryFeatureData. This approach is much more involved (see my example code), but the search is not as limited as in the first case.

  Dim pAgsLayer As IMapServerLayer

    ' ... get reference to the top-level AGS layer somewhere, e.g. from the TOC ...

  Dim pSoName As IAGSServerObjectName
  Dim pName As IName
  Dim pMapServer As IMapServer
  Dim sDocLocation As String, sMapName As String
  Dim pRecordSet As IRecordSet
  Dim pQueryFilter As IQueryFilter

    ' get access to the layer's AGS map server object
    pAgsLayer.GetConnectionInfo pSoName, sDocLocation, sMapName
    Set pName = pSoName
    Set pMapServer = pName.Open

    ' specify some query conditions
    Set pQueryFilter = New QueryFilter
    pQueryFilter.SubFields = "*"
    pQueryFilter.WhereClause = "OBJECTID = 1"

    ' query the layer with Id 0 (the first layer in the map service)
    Set pRecordSet = pMapServer.QueryFeatureData(sMapName, 0, pQueryFilter)

  Dim pCursor As ICursor
  Dim pRow As IRow

    Set pCursor = pRecordSet.Cursor(True)
    Set pRow = pCursor.NextRow

    ' loop through the results and do something with them
    Do While Not pRow Is Nothing
      Debug.Print pRow.OID
      Set pRow = pCursor.NextRow
share|improve this answer
Thanks for the feedback. I've pasted your code into arcmap, but I'm getting an "Obejct variable or with block variable not set" error at this line: pAgsLayer.GetConnectionInfo pSoName, sDocLocation, sMapName. I'm using your code as I found it, is there something I need to add to it? – Zachary Dec 28 '10 at 16:23
@Zachary: the code is meant to be merely a guiding sample, definitely not a complete ready-to-run solution. You will first need to familiarise yourself with basics of VBA, ArcObjects and several parts of its object model. There are some posts on this site which could point you to good resources. – Petr Krebs Dec 28 '10 at 17:34
Anyway, the message means that the pAgsLayer variable is not initialized to an instance. As mentioned at the very beginning of the code sampe, it needs to be pointed to an existing AGS top-level layer first. How you do that depends on your scenario. Again, basic programming skills and knowledge of AO are essential, sorry. – Petr Krebs Dec 28 '10 at 17:35
Thanks for the help. – Zachary Dec 28 '10 at 20:52

I believe you will need arcobjects. It is the vb api for esri.

System Requirements

VB help

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.