Help fix tool to which uses a txtbox to make selection. pFeatureSelection broken

01-28-2014 12:50 PM
New Contributor
Hello all,
I'm trying to migrate some VBA code written in 9.2 into a Win7, Office 2010, 10.2 format and I'm having a little trouble as I'm very novice at programing.  My tool is pretty basic that I'm trying to create.  It uses a form with a textbox to which someone would paste in text in to be used as a selection. Its basically just a select by attributes with a custom form since all the selections are on the same field in the same layer.  Layer = Forest Field = ID Format is always XX0000-0000.  My tool runs through the code successfully but stops at the pFeatureSelection.SelectFeatures at the bottom below the pActiveView and does not make a selection.  I can't figure out what is going on.  I've tried pasting in what seems like updated code format but it does not like this barks at me.  If anyone has suggestions it would be very appreciated. 

Public Sub CommandButton1_Click()
        Dim pStID As String
        Dim pMxDoc As IMxDocument
        Dim pMap As IMap
        Dim pActiveView As IActiveView
        Dim pFeatureLayer As IFeatureLayer
        Dim pFeatureSelection As IFeatureSelection
        Dim pQueryFilter As IQueryFilter
        Dim pEnumLayer As IEnumLayer
        Set pMxDoc = Application.Document
        Set pMap = pMxDoc.FocusMap
        Set pActiveView = pMap
        Set pEnumLayer = pMxDoc.FocusMap.Layers
        Set pFeatureLayer = pEnumLayer.Next
        Do Until pFeatureLayer Is Nothing
        If pFeatureLayer.Name = "Forest" Then
        Exit Do
        End If
        Set pFeatureLayer = pEnumLayer.Next
            If pFeatureLayer Is Nothing Then
                MsgBox "It Is Empty"
                Exit Sub
            End If
        Set pFeatureSelection = pFeatureLayer
            pID = "[ID] IN (" & txtID.Text & ")"
      Set pQueryFilter = New QueryFilter
      pQueryFilter.WhereClause = pID
      pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
      pFeatureSelection.SelectFeatures pQueryFilter, esriSelectionResultNew, False
     pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
        ''pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)
                ' Perform the selection using the user input.
        '' pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)
End Sub
0 Kudos
4 Replies
MVP Frequent Contributor

The "updated" code is not VBA it is VB .Net that's why it is rejected. It's to do with the brackets.

Your pFeatureSelection is almost certainly failing to select anything due to the where clause of your queryfilter. You declare a string pStID but you use a variable pID, are these meant to be the same thing?

A query will change its syntax based upon the source format. Fields in a personal geodatabase are enclosed in [], fields in a shapefile are enclosed in "", a file geodatabase has no delimiters (certainly in 10.2). So... your pID query string is most likely to be in the wrong format. I would go into Arcmap run the select by attribute tool from the menu and try to create a selection then study its format and apply that to your queryfilter.

I would put your pActiveView.PartialRefresh... line after your pFeatureSelection.SelectFeatures... line as this would then refresh the map display after the selection so you see it.

0 Kudos
New Contributor
Thanks for the time and attention Duncan.  The code I pasted in here is slightly modified in that I changed all the StID to "ID" for whatever reason, but they are correct in the code I'm running.  I put a message box under the pQueryFilter.WhereClause to see what the string looked like when I would paste in text.  Apparently it is NOT in the format I need as it doesn't have a single quote and comma similar to the 'Select By Attributes' standard method. I'm not sure if I can somehow write in code to format the input in such a way or not.  The previous tool used a textbox but had rows and a column embedded within it which was set using the 'Properties' tab of the texbox so it mimicked a spreadsheet. This is no longer an option and is why I'm just using a textbox.  The problem I've seen is when I paste in a string like this (below) 


The textbox is reading it as [StID] IN (StIDWC0000-0000WC0000-0001XX0022-0022) I think.  It needs to ignore the first pasted in StID (or put in its own 'StID', which will ignore it) and It needs to be [StID] IN ('WC0000-0000','WC0001-0001','XX0022-0022')    Is this a way I can systematically add in the single quotes and a comma to this string:  pStID = "[StID] IN (" & "'" & txtStID.Text & "'" & ")"    ???
0 Kudos
MVP Frequent Contributor

You will need to do some pre-processing of the text in the text box. Firstly is stID always going to be part of what is pasted into the text box? If so that needs to be stripped out. I notice that your codes are a constant 11 characters long is this always the case? You would use the following functions to manipulate the text.

dim s,r,c as String
dim n as Integer
Let s = "StIDWC0000-0000WC0000-0001XX0022-0022"

[TABLE="class: grid"]Len(string)Gives you the length of the stringlet n = len(s)
Right(string,length)Returns the string from the rightlet r = Right(s,n-4)Mid(String,start,length)Returns the string starting at startlet c= Mid(r, 1, 11)

So having stripped off "stid", determined the length of the string, divide that by 11 and this is the number of codes entered. Step through your string returning each code and you can build up your full query string into [StID] IN ('WC0000-0000','WC0001-0001','XX0022-0022')

If you are not that familiar with string manipulation in VBA then a quick search of the internet will help.

0 Kudos
New Contributor
Thanks Duncan once again.  You are correct that the important text I'm trying to use will always be 11 characters in this format:      XX0000-0000 . I've thought about trying to use the hyphen in the middle as an identifier w/ 6 characters to the left and 4 to the right and do some magical text parsing or splitting or whatever.  I've never done any type of text manipulation so this will be new stuff.  Do I have to make a new public sub for the text manipulation or can I put it under the Public Sub CommandButton1  stuff?  

There is really no way of me knowing what will be included with this text when a user pastes it in.  Sometimes we copy stuff from an email, sometimes a word document, but most times it comes from an Access or Excel spreadsheet.  That's why I the spreadsheet previously worked so well because each row essentially had a single quote and comma.  You could type in:

Supper Time

And it would put Howdy in like:  [StID] IN ('Howdy', 'Supper Time', 'XX0000-0000', etc)  and since it just wouldn't find the first to strings in the field, it wouldn't care and just move on. Furthermore, with the textbox I'm using now, I can't type in more than one line.  It is a multiline textbox, but only if I paste more than one line.  If I hit enter, it fires the button, and doesn't drop down to the next line/row  which I would like it to.  So there are several limitations I'm seeing with the textbox. And also there is no row count, so you can't see how many StID's have been pasted in there.  But that is pretty unimportant in the scheme of things. 

I do thank you for you help and I'll keep working on this.
0 Kudos