I (partially) solved my problems: the script now works because I created an apx which calls the GPS add-point function from the associated vbs. When the form loads the shape's vbs auto-fills correctly all the fields.
Now I have a last trouble because after the form closes clicking on "ok" arcpad returns a "run time error", it tells me that there's an undefined variable, source text unavailable. The problems is at the Dim rsGrid line of the trees.vbs file.
Thanks!
This is the finally working codes:
arcpad.vbs
Option Explicit
Sub AddTree
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 Trees layer exists,
'set the blnLyrExists flag to true.
Dim objLyr
For Each objLyr in Map.Layers
If StrComp (objLyr.Name, "Trees", 1) = 0 Then
blnLyrExists = True
Exit For
End If
Next
'If Trees layer does not exist,
'notify the user, return the tool button to its original state, and exit.
If Not blnLyrExists Then
MsgBox "The Trees layer is not present in the current map.", vbExclamation, "Layer not present"
objToolButton.Click
Exit Sub
End If
'If the Trees layer does exist,
'get the coordinates of the map where the user clicked.
dblX = Map.PointerX
dblY = Map.PointerY
'Get a reference to the Trees layer object.
Dim objLayer
Set objLayer = Map.Layers("Trees")
'If the layer can be made editable, make it editable.
If objLayer.CanEdit Then
objLayer.Editable = True
'Add a new tree (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
Sub NewPoint
Dim newX, newY
If Application.GPS.IsValidFix = False Then
MsgBox("No GPS fix! Assicurati che il GPS sia acceso e il segnale sia valido.")
Exit Sub
End If
newX = Application.GPS.X
newY = Application.GPS.Y
If Not Application.Map.Layers("Trees").CanEdit Then
Msgbox "Trees cannot be edited.",vbExclamation,"Error"
Exit Sub
End If
'If the layer is not already editable, make it editable
If Not Application.Map.Layers("Trees").Editable Then
Application.Map.Layers("Trees").Editable = True
End If
'Attempt to add a new point to the currently editable point layer at the current GPS location
'Return an error message if the attempt fails
If Not Application.Map.AddFeatureXY(newX, newY) Then
MsgBox "Error adding GPS Point.",vbExclamation,"Error"
Exit Sub
End If
End Sub
trees.vbs
Option Explicit
' compila il campo dell'operatore in automatico
Public Sub MyFormOnLoad
Dim objFrmCtls
Set objFrmCtls = EDITFORM.Pages("Page1").Controls
'Set objFrmCtls = ThisEvent.Object
'Set objEFPageOneControls = objFrmCtls.Pages("Page1").Controls
'objFrmCtls("edt_UserName").Enabled = False
objFrmCtls("edt_UserName").Value = Application.UserProperties("LoggedInEmployee")
'compila il nome zona sorveglianza in automatico
Dim rsCurrentXY
Set rsCurrentXY = Map.Layers("trees").records 'indica il il nome dello script vbs e il layer target
rsCurrentXY.movelast
Dim objXYShape
Set objXYShape = rsCurrentXY.Fields.Shape
Dim pControls
Set pControls = EDITFORM.Pages("Page1").Controls 'indica la pagina del form dove si trova il campo da compilare
Dim rsZona
Set rsZona = Map.Layers("ZoneSorveglianzaPNGP").records 'indica il layer poligono da cui prendere il valore
rsZona.movefirst
Do While Not rsZona.eof
If rsZona.fields.shape.Ispointin(objXYShape) Then
'pControls("tipo").Enabled = False 'disabilita l'edit nella textbox ma solo se il campo è compilato in automatico
pControls("tipo").value = rsZona.Fields("NOME").Value ' indica il controllo nella pagina del form da campilare e da quale campo del layer origine prendere il valore
Exit Do
End If
rsZona.Movenext
Loop
' Clean Up
Set rsCurrentXY = Nothing
Set objXYShape = Nothing
Set rsZona = Nothing
Set pControls = Nothing
' nomi valli in automatico
Dim rsCurrentXY1
Set rsCurrentXY1 = Map.Layers("trees").records 'indica il nome dello script vbs e il layer target
rsCurrentXY1.movelast
Dim objXYShape1
Set objXYShape1 = rsCurrentXY1.Fields.Shape
Dim pControls1
Set pControls1 = EDITFORM.Pages("Page1").Controls 'indica la pagina del form dove si trova il campo da compilare
Dim rsValle
Set rsValle = Map.Layers("Valli_PNGP_wgs84").records 'indica il layer poligono da cui prendere il valore
rsValle.movefirst
Do While Not rsValle.eof
If rsValle.fields.shape.Ispointin(objXYShape1) Then
'pControls1("edt_valle").Enabled = False ''disabilita l'edit nella textbox ma solo se il campo è compilato in automatico
pControls1("edt_valle").value = rsValle.Fields("VALLE").Value ' indica il controllo nella pagina del form da campilare e da quale campo del layer origine prendere il valore
Exit Do
End If
rsValle.Movenext
Loop
' Clean Up
Set rsCurrentXY1 = Nothing
Set objXYShape1 = Nothing
Set rsValle = Nothing
Set pControls1 = Nothing
'nomi griglia in automatico
Dim rsCurrentXY2
Set rsCurrentXY2 = Map.Layers("trees").records 'indica il nome dello script vbs e il layer target
rsCurrentXY2.movelast
Dim objXYShape2
Set objXYShape2 = rsCurrentXY2.Fields.Shape
Dim pControls2
Set pControls2 = EDITFORM.Pages("Page1").Controls 'indica la pagina del form dove si trova il campo da compilare
Dim rsGrid
Set rsGrid = Map.Layers("Grigliato_v2").records 'indica il layer poligono da cui prendere il valore
rsGrid.movefirst
Do While Not rsGrid.eof
If rsGrid.fields.shape.Ispointin(objXYShape2) Then
'pControls1("edt_valle").Enabled = False ''disabilita l'edit nella textbox ma solo se il campo è compilato in automatico
pControls2("edt_cella").value = rsGrid.Fields("N__CELLA").Value ' indica il controllo nella pagina del form da campilare e da quale campo del layer origine prendere il valore
Exit Do
End If
rsGrid.Movenext
Loop
' Clean Up
Set rsCurrentXY2 = Nothing
Set objXYShape2 = Nothing
Set rsGrid = Nothing
Set pControls2 = Nothing
End Sub