I need to run a table update command on every column in a table. The code works fine when I manually define the column to work in but not when it is part of a for loop that gets all the column names I get
If I take the update table command out of the loop and into the code after the for loop, it works fine.
What's different with defining a declare for use in a loop?
'64 bit regex based on https://community.pitneybowes.com/communities/communityhome/digestviewer/viewthread?MessageKey=a92a0fcd-5348-4eae-aa93-a47d5a1e6bbe&CommunityKey=3c2aca7a-c3ae-4602-a142-9ee956769d55&tab=digestviewer#bma92a0fcd-5348-4eae-aa93-a47d5a1e6bbe'
Include "MapBasic.def"
'64 bit regex'
Declare Method RegExReplace Class "WindowHelper.Controller" Lib "WindowHelper.dll" (ByVal sInput As String, ByVal sPattern As String, ByVal sReplace As String) As String
Declare Method RegExIsMatch Class "WindowHelper.Controller" Lib "WindowHelper.dll" (ByVal sInput As String, ByVal sPattern As String) As Logical
Declare Method RegExFirstMatch Class "WindowHelper.Controller" Lib "WindowHelper.dll" (ByVal sInput As String, ByVal sPattern As String) As String
Declare Sub Main
Sub Main
Dim inFile, regexString as String
Dim inColumn as Alias
inFile= "Z:\My Drive\Mangoesmapping\Spatial Projects\2018\DSC\007_DSC_Asset_Identification_and_Geocoding\Working\Phase3_DataEnrichment\Working\DataUpdates\Scratch\Water_Hydrants.TAB"
'Perform Regex and text Replace'
Open Table inFile as tempTable
'Alter Table tempTable (add ScratchField Char(254) ) Interactive
Browse * From tempTable
regexString ="^\s\|\s(.*)"
' Perform on all fields -ref: https://gis.stackexchange.com/questions/75494/how-to-loop-through-all-columns-in-a-table-layer-getting-all-data-for-a-row-feat
Dim sCol As String
Dim aCol As Alias
Dim nCol As Integer
For nCol = 1 To TableInfo(tempTable, TAB_INFO_NCOLS)
sCol = ColumnInfo(tempTable, "COL" & nCol, COL_INFO_NAME)
aCol = tempTable & "." & sCol
'Update every column as it's found --- GIVES ERROR
'Update tempTable Set ScratchField = RegExReplace(aCol,regexString,"")
Next
'Update a known column
inColumn = tempTable & ".As_con_3_hyperlink"
Update tempTable Set ScratchField = RegExReplace(inColumn,regexString,"")
End Sub
UPDATED CODE based on T_Bacon's input.
Had to change scratchField to aCol as it is now checking every column.
Sub Main
Dim inFile, inField, regexString, strValue as String
inFile = "Z:\My Drive\Mangoesmapping\Spatial Projects\2018\DSC\007_DSC_Asset_Identification_and_Geocoding\Working\Phase3_DataEnrichment\Working\DataUpdates\Scratch\Water_Hydrants.TAB"
inField = ".As_con_3_hyperlink"
'Perform Regex and text Replace'
Open Table inFile as tempTable
'Alter Table tempTable (add ScratchField Char(254) ) Interactive
Browse * From tempTable
'Pattern for starting | without anything before it
regexString ="(.*)[\s]+\|\s(.*)"
' Perform on all fields -ref: https://gis.stackexchange.com/questions/75494/how-to-loop-through-all-columns-in-a-table-layer-getting-all-data-for-a-row-feat
Dim sCol As String
Dim aCol As Alias
Dim nCol As Integer
For nCol = 1 To TableInfo(tempTable, TAB_INFO_NCOLS)
sCol = ColumnInfo(tempTable, "COL" & nCol, COL_INFO_NAME)
aCol = tempTable & "." & sCol
'Update every column as it's found --- GIVES ERROR ref: https://gis.stackexchange.com/questions/316048/get-code-to-run-on-every-column-in-a-table-error-could-not-find-a-public-stati
strValue = aCol
Update tempTable Set aCol = RegExReplace(strValue, regexString, "")
Next
End Sub