TheSwamp

Code Red => VB(A) => Topic started by: CHulse on August 13, 2017, 10:39:55 AM

Title: VBScript for ArcPad
Post by: CHulse on August 13, 2017, 10:39:55 AM
Does anyone here use VBS to customize Arcpad for data collection?
I've been using it for collecting tree data.

Does anyone have an example of how to parse numbers from a text string, run a calculation and replace the result in the string?

Thanks
Title: Re: VBScript for ArcPad
Post by: roy_043 on August 13, 2017, 05:28:36 PM
I do not know Arcpad. But here is a VBS example.
To test the code: save with the .vbs extension and double-click the file.
Code - vb.net: [Select]
  1. Dim inp, matches, newVal, oldVal, reg
  2. inp = InputBox("Enter text with number")
  3. Set reg = New RegExp
  4. reg.Pattern = "\d+\.?\d+(?=\D*)"   ' Change "\." to "," if required.
  5. Set matches = reg.Execute(inp)
  6. If 0 <> matches.Count Then
  7.   oldVal = matches(0).Value
  8.   newVal = oldVal + 15             ' Add 15. No need to convert oldVal to a number or newVal to a string.
  9.   MsgBox(Replace(inp, oldVal, newVal, 1, 1))
  10. Else
  11.   MsgBox("No number found")
  12. End If
Title: Re: VBScript for ArcPad
Post by: CHulse on August 14, 2017, 10:00:35 AM
Thanks. I was able to figure out what I needed using an array.

Code - vb.net: [Select]
  1. '' increase DBH
  2. Sub DBHPLUS
  3.   Dim objTheLayer, objTheForm, objEditFormCtrls, strDBH, aa, ub
  4.   Set objTheLayer = Application.Map.Layers.item(1)
  5.   Set objTheForm = objTheLayer.Forms(1)
  6.   Set objEditFormCtrls = objTheForm.Pages("PAGE1").Controls
  7.     If (objEditFormCtrls("txtDBH").value = "") Then
  8.       objEditFormCtrls("txtDBH").value = 1
  9.     Else
  10.       'get DBH string form form field
  11.       strDBH = objEditFormCtrls("txtDBH").value
  12.       'remove all spaces
  13.       strDBH = Replace(strDBH," ","",1,-1)
  14.       'create array using comma delimeter
  15.       aa = split (strDBH, ",", -1, 1)
  16.       'get upper limit od array
  17.       ub = ubound(aa)
  18.       'convert last entry to integer and add one
  19.       aa(ub)= 1 + cint(aa(ub))
  20.       'rebuild string from array adding commas
  21.       strDBH = Join (aa, ",")
  22.       'pass new string to form field
  23.       objEditFormCtrls("txtDBH").value = strDBH
  24.     End If
  25.  
  26.   'Free objects
  27.   set aa = nothing
  28.   set ub = nothing
  29.   set strDBH = Nothing
  30.   Set objEditFormCtrls = Nothing
  31.   Set objTheForm = Nothing
  32.   Set objTheLayer = Nothing
  33. End Sub
  34.  
Title: Re: VBScript for ArcPad
Post by: roy_043 on August 14, 2017, 11:12:56 AM
As mentioned in one of the comments in my suggestion: you do not have to use the CInt function. But If you do, wouldn't it then make sense to also use CStr?
Title: Re: VBScript for ArcPad
Post by: CHulse on August 14, 2017, 06:23:25 PM
As mentioned in one of the comments in my suggestion: you do not have to use the CInt function. But If you do, wouldn't it then make sense to also use CStr?

Interesting point. I don't really know. I've used something similar for excel and the type conversion was needed, so I assumed I'd need it here. I believe I've read that JOIN returns a string regardless of input, so I don’t think the CStr is needed. I'll need to test it without the Cint to see.