VBScript to report XY coordinates

7943
19
07-04-2010 05:09 PM
CarlBeyerhelm
Occasional Contributor
The VBScript listed below returns zeros for both the X and Y coordinates when fired by a point form's OnLoad event.  This should be pretty straight-forward stuff.  What am I missing?

Thanks...

Sub UpdateXY()
'get a reference to the form
Dim pForm
Set pForm = ThisEvent.Object

'get the new point feature's X and Y
Dim X
Dim Y
X = pForm.Fields.Shape.X
Y = pForm.Fields.Shape.Y
pForm.Pages("page1").Controls("X_Coord").Value = X
pForm.Pages("page1").Controls("Y_Coord").Value = Y
End Sub
Tags (3)
0 Kudos
19 Replies
RolfBroch
Occasional Contributor II
The Fields property refers to the current record in the recordset. This record is actually empty until you save (click OK) in the form (only for new objects).
0 Kudos
CarlBeyerhelm
Occasional Contributor
I can see that might be the case for a newly created ("pending") point feature, but it also returns zeros for X and Y coordinates when an existing point feature is selected, and its form opened.

So, how would you get a newly created point feature's X and Y coords?  They do appear in the form's Geography page, so the form must "have" them.  I just don't know how to access them.  Any ideas?

I'm using AXF layers and AP 8 SP3.

Thanks...
0 Kudos
RolfBroch
Occasional Contributor II
Is pForm.Pages("page1") the active page when you execute the script?

If not you need to activate the page first

pForm.Pages("page1").Activate
pForm.Pages("page1").Controls("X_Coord").Value = X
pForm.Pages("page1").Controls("Y_Coord").Value = Y
0 Kudos
CarlBeyerhelm
Occasional Contributor
Yes, pForm.Pages("Page1") is already active when the form opens.
0 Kudos
EricShyer
New Contributor
Carl,

I have two scripts in my \system\ArcPad.apx.
The first in the .vbs script is as follows:
Option Explicit


Sub AddSturgeonTags



  Dim dblX, dblY, objToolButton, blnLyrExists
  'Get a reference to the tool button object
  Set objToolButton = ThisEvent.Object
  'Initialize blnLyrExists flag to False
  blnLyrExists = False
  'If SturgeonTags layer exists,
  'set the blnLyrExists flag to true
  Dim objLyr
  For Each objLyr in Map.Layers
    If StrComp (objLyr.Name, "Water", 1) = 0 Then
      blnLyrExists = True
      Exit For
    End If
  Next
  'If ISturgeonTags layer does not exist:
  'Notify the user, return the tool button to its original state, and exit.
  If Not blnLyrExists Then
    MsgBox "Water layer is not present in the current map.", vbExclamation, "Layer not present"
    objToolButton.Click
    Exit Sub
  End If
  'If the ISturgeonTags layer does exist:
  'Get the coordinates of the map where the user clicked
  dblX = Map.PointerX
  dblY = Map.PointerY
  'Get a reference to the Invasive Plants Layer object
  Dim objLayer
  Set objLayer = Map.Layers("Water")
  'If the layer can be made editable, make it editable
  If objLayer.CanEdit Then
    objLayer.Editable = True
    'Add a new Invasive Plant (point feature) at the clicked location
    Call Map.AddFeatureXY(dblX,dblY)
    'Return the tool button to its original state
    objToolButton.Click
  End If
End Sub

The second is added onto the .apx config file SystemObjects
                                                                      maps   as an onfeatureadded script

Dim MyCoordSys
Dim objRS
Set MyCoordSys = Application.Map.CoordinateSystem
Set objRS = Map.Selectionlayer.Records
objRS.Bookmark = Map.SelectionBookmark
If Left(objRS.Fields.ShapeType, 1) = 1 Then
If MyCoordSys.GeographicName = "GCS_WGS_1984" Then
  For Each f in objRS.Fields
   If f.name = "X" Then
    f.Value = objRS.Fields.Shape.Y
   ElseIf f.name = "Y" Then
    f.Value = objRS.Fields.Shape.X
   ElseIf f.name = "Z" Then
    f.Value = GPS.Z
   ElseIf f.name = "LATITUDE" Then
    f.Value = objRS.Fields.Shape.Y
   ElseIf f.name = "LONGITUDE" Then
    f.Value = objRS.Fields.Shape.X
   ElseIf f.name = "ALTITUDE" Then
    f.Value = GPS.Altitude
   End If

  Next

   Else
  For Each f in objRS.Fields
   If f.name = "X" Then
    f.Value = objRS.Fields.Shape.X
   ElseIf f.name = "Y" Then
    f.Value = objRS.Fields.Shape.Y
   ElseIf f.name = "Z" Then
    f.Value = GPS.Z
   ElseIf f.name = "LATITUDE" Then
    f.Value = objRS.Fields.Shape.X
   ElseIf f.name = "LONGITUDE" Then
    f.Value = objRS.Fields.Shape.Y
   ElseIf f.name = "ALTITUDE" Then
    f.Value = GPS.Altitude
   End If

  Next
End If
 
End If
objRS.Update
Map.Refresh(True)

The second script will add the XY or Z of the point IF you have either an X , Y and or Z ( or Latitude, Longitude, Altitude) attribute field in your .dbf

Hope this helps,

Eric
0 Kudos
CarlBeyerhelm
Occasional Contributor
Thanks for the post, but that's not really what I'm after since it appears to be capturing X and Y from existing features.

What I'd like is to be able to capture a newly created point's X and Y coordinates prior to clicking the OK button on the attribute capture form so that I can use those coords to copy an attribute from a polygon that the point falls within.  The issue is that I don't know how to capture the newly created point feature's X and Y before it has actually become a feature...before OK has been clicked.

The form obviously "has" the pending X and Y coords to create the feature from.  I just don't know how to access those pending X and Y coords.

For instance, the attached image illustrates what happens when I select an existing feature and open its Feature Properties form.  The X and Y are captured from the existing point feature, and the Cover type of the polygon the point falls within is written to the point's Cover type field.  The trick is to be able to discover the X and Y of a pending point feature before the OK button is clicked.

I'm a rookie at this, so maybe I'm missing something simple...
0 Kudos
RolfBroch
Occasional Contributor II
I created a quick project and did some modifications to the editform by adding a comand button that when clicked would run

MessageBox EditForm.Fields.Shape.X

This reported back my x-coordinate.

shapefile and modified apl is attached.

Rolf

Could it be that your code does not work since it runs in the onload event? My example above happens after the form has been loaded.
0 Kudos
CarlBeyerhelm
Occasional Contributor
Thanks for sticking with me on this one Rolf.  I appreciate it.

Could you try posting your ZIP file again.  It doesn't seem to have any content when I download it.

Thanks...
0 Kudos
RolfBroch
Occasional Contributor II
I'll try again.

Rolf
0 Kudos