Select to view content in your preferred language

Custom Map Book Table - Feature Cursor Problem - "Automation Error"

1169
6
05-17-2010 09:19 AM
JustinRiggs
Emerging Contributor
Environment: Visual Basic Editor
Language: VB6

I posted earlier on this, and got some great replies, but I didn't do a good job of explaining what I'm working on. This time, I have some code samples, and even a picture - I'm prepared.

At my company we produce map books - lots of them. Our boss decided he wanted a really pretty table for the layout, so he had someone design it in Excel. Then our GIS folks had to hand enter the data into the spreadsheet, and copy and paste it into ArcMap. Not so much fun (especially when the map book is 50+ pages). So my boss asks me, "Hey, is there any way we could automatically populate that table instead of having to pay people $40/hr to do data entry?" My first thought is, "No, boss. It's a pasted version of an Excel spreadsheet. I can't get to it." Wanting to keep my job, however, I respond, "Sure! I'll figure something out!" So I did.

If you open up the attachment, you'll see a picture of the table we use. The goal is to populate an entire row of the table when the user selects one of the features using the combo box on the right (you'll notice there are three combo boxes - one for each row). Right now, I've got the combo boxes and the header row populating on the OpenDocument event, and the first cell of each row populating on the SelectionChange event of the combo box. Now I've got to get the rest of the row to populate. My current code is below. My problem is that when I make a selection, the code snags at:

Set pFCursor = pFClass.Search(pQueryFilter, True)

The message is:

Run-time error '-2147220648 (80040358)': Automation Error. Google search was less than helpful.

Here's the code for the SelectionChange event (I've only included Case "A2" and "B2" because once I figure out "B2" the rest should be identical):

Private Sub cbTransect1_SelectionChange(ByVal newIndex As Long)
  Dim pMxDoc As IMxDocument
  Set pMxDoc = ThisDocument
 
  Dim pMaps As IMaps
  Set pMaps = pMxDoc.Maps
 
  Dim pIndexChipView As IMap
  Set pIndexChipView = pMaps.Item(0)
 
  Dim pMainMap As IMap
  Set pMainMap = pMaps.Item(1)

  Dim pGraphics As IGraphicsContainer
  Set pGraphics = pMxDoc.PageLayout
 
  pGraphics.Reset
 
  Dim pElementProp As IElementProperties2
  Set pElementProp = pGraphics.Next
 
  Dim pTextElement As ITextElement
 
  Do Until pElementProp Is Nothing
   Select Case pElementProp.Name
      Case "A2"
        Set pTextElement = pElementProp
        pTextElement.Text = cbTransect1.EditText
      Case "B2"
       
        Dim pMap As IMap
        Set pMap = pMxDoc.FocusMap
       
        Dim pFLayer As IFeatureLayer
        Set pFLayer = pMap.Layer(1)
       
        Dim pFClass As IFeatureClass
        Set pFClass = pFLayer.FeatureClass
       
        Dim pQueryFilter As IQueryFilter   
        Set pQueryFilter = New QueryFilter
       
        pQueryFilter.WhereClause = "DS_DIST_MI = '" & cbTransect1.EditText & "'"
       
        Dim pFCursor As IFeatureCursor
        Set pFCursor = pFClass.Search(pQueryFilter, True)
       
        Dim pFeature As IFeature
        Set pFeature = pFCursor.NextFeature
       
        Dim lngMaxWSE As Integer  '** placeholder for B2 value
        lngMaxWSE = 0
       
        '** Getting the index pos of the MaxFlow field
        Dim lngMaxWSEIndex As Long
        lngMaxWSEIndex = pFClass.Fields.FindField("MAXWSEL_FT")
       
        Do Until pFeature Is Nothing
           
            '** Getting the cell value and adding to the total (which is 0)
            lngMaxWSE = lngMaxWSE + pFeature.Value(lngMaxWSEIndex) 'this should be the value that will go in B2
           
            Set pFeature = pFCursor.NextFeature
        Loop
       
        'set the value of B2 to lngMaxWSE

        Set pTextElement = pElementProp
        pTextElement.Text = lngMaxWSE
       
  Dim pActiveView As IActiveView
  Set pActiveView = pGraphics
 
  pActiveView.PartialRefresh esriViewGraphics, Nothing, Nothing
End Sub

Any advice is appreciated.

Justin
0 Kudos
6 Replies
JamesCrandall
MVP Alum
Double check your WhereClause and make certain that chTransect1.EditText is correct and actually an attribute in the row:

pQueryFilter.WhereClause = "DS_DIST_MI = '" & cbTransect1.EditText & "'"


You might even setup a variable instead of directly adding the control's value into the query string.  This way you could check it before setting the whereclause.  (also: I see you are working in VB6, which I don't have and not working with, so this may not work for you.  I don't recall if there is a SelectedValue.ToString for a ComboBox control in VB6!).

Get the text of the SelectedValue or SelectedText in the Combobox:

Dim cbText as String
cbText = CStr(Me.cboRateClass.SelectedValue.ToString) 
 'or try 
cbText = CStr(Me.cboRateClass.SelectedText.ToString) 


'use this variable in your WhereClause
pQueryFilter.WhereClause = "DS_DIST_MI = '" & cbText & "'"
0 Kudos
JustinRiggs
Emerging Contributor
James,

Both good ideas. Before I sent this code, I had actually used a seperate variable, for the very reason you suggested. In debugging, I can see that the variable is properly getting passed as a string. Your suggestion on checking the WHERE clause led me to dig a little deeper, and I think I've opened up a can of worms.

I started querying against the table in ArcMap, and it appears there's something strange going on with the DS_DIST_MI Field. For example, if I write a WHERE query where DS_DIST_MI = 6.0, it finds it, but if I write a WHERE clause where DS_DIST_MI = 9.9 (a valid value), no record is found. I've had our GIS folks look at it, and they're all scratching their heads. Until then, I'm going to test the theory by querying against another filed. We'll see what happens.
0 Kudos
JustinRiggs
Emerging Contributor
More discoveries. The GIS team figured out that in some of their shapefiles, the data type had been switched from Double to Float, which apparently causes trouble when trying to query the attribute table. They're in the process of fixing that up.

In the meantime, I switched to querying a different field, and am thrilled and horrified by what I have found. First the good news. If I hard code the field's value, things work perfectly! For example:

pQueryFilter.WhereClause = "MAXWSEL_FT = 6322.9"

returns the correct value and places it right where I want it in the table. Bravo! Now the bad news...

As we all know, it does no good to have a hard coded value. I need a variable that can hold a value based on user input. First I tried to use the value directly from the combo box:

pQueryFilter.WhereClause = "MAXWSEL_FT = '" & cbTransect1.EditText & "'"

No luck. (FYI, cbTransect1.EditText's Type is String, which I didn't know until this afternoon)

So I tried creating a different variable and converting cbTransect1.EditText to String, which I now know was redundant:

Dim strRegion as String
strRegion = CStr(cbTransect1.EditText)
pQueryFilter.WhereClause = "MAXWSEL_FT = '" & strRegion & "'"

Still no luck.

Then I got the bright idea of storing the static value (the one that had worked earlier) in the strRegion variable. Would you believe that it didn't work either?

Dim strRegion as String
strRegion = "6322.9"
pQueryFilter.WhereClause = "MAXWSEL_FT = '" & strRegion & "'"

It seems like it just doesn't like anything when its passed in through a variable. I've seen loads of examples with this same syntax. Can anyone spot what's wrong with my SQL?
0 Kudos
JamesCrandall
MVP Alum
More discoveries. The GIS team figured out that in some of their shapefiles, the data type had been switched from Double to Float, which apparently causes trouble when trying to query the attribute table. They're in the process of fixing that up.

In the meantime, I switched to querying a different field, and am thrilled and horrified by what I have found. First the good news. If I hard code the field's value, things work perfectly! For example:

pQueryFilter.WhereClause = "MAXWSEL_FT = 6322.9"

returns the correct value and places it right where I want it in the table. Bravo! Now the bad news...

As we all know, it does no good to have a hard coded value. I need a variable that can hold a value based on user input. First I tried to use the value directly from the combo box:

pQueryFilter.WhereClause = "MAXWSEL_FT = '" & cbTransect1.EditText & "'"

No luck. (FYI, cbTransect1.EditText's Type is String, which I didn't know until this afternoon)

So I tried creating a different variable and converting cbTransect1.EditText to String, which I now know was redundant:

Dim strRegion as String
strRegion = CStr(cbTransect1.EditText)
pQueryFilter.WhereClause = "MAXWSEL_FT = '" & strRegion & "'"

Still no luck.

Then I got the bright idea of storing the static value (the one that had worked earlier) in the strRegion variable. Would you believe that it didn't work either?

Dim strRegion as String
strRegion = "6322.9"
pQueryFilter.WhereClause = "MAXWSEL_FT = '" & strRegion & "'"

It seems like it just doesn't like anything when its passed in through a variable. I've seen loads of examples with this same syntax. Can anyone spot what's wrong with my SQL?


I think it just might all boil down to a strings-as-numbers type problem.  For example, if this works:

pQueryFilter.WhereClause = "MAXWSEL_FT = 6322.9"

....then I think you should setup your QueryFilter as such (not tested, and going from memory, maybe someone can chime in until tomorrow when I get in front of some of my ArcObjects):

Dim var as Decimal
var = CDec(cbTransect1.EditText)
pQueryFilter.WhereClause = "MAXWSEL_FT = " & var
0 Kudos
JustinRiggs
Emerging Contributor
James,

Just wanted to take a second and say thank you for all the help you gave me on this issue. You were right - while this:

pQueryFilter.WhereClause = "MAXWSEL_FT = '" & var & "'" *didn't work

this:

pQueryFilter.WhereClause = "MAXWSEL_FT = " & var *did, as long as I declared var as the proper type.

My employer has had three programmers work on this specific tool over the course of several years. Your help is getting me big brownie points with those who pay my bills, and for that I can only say, "You da man!"

Justin
0 Kudos
JamesCrandall
MVP Alum
James,

Just wanted to take a second and say thank you for all the help you gave me on this issue. You were right - while this:

pQueryFilter.WhereClause = "MAXWSEL_FT = '" & var & "'" *didn't work

this:

pQueryFilter.WhereClause = "MAXWSEL_FT = " & var *did, as long as I declared var as the proper type.

My employer has had three programmers work on this specific tool over the course of several years. Your help is getting me big brownie points with those who pay my bills, and for that I can only say, "You da man!"

Justin


Nice.  Glad to see you got it working!
0 Kudos