1

I need to run a string of code multiple times in a mbx while just changing 2 parameters. I tried a sub and function but both failed. How can I do this.

The code is

Print "Starting DCDB_FeatureName"

strFieldName="Feat_Name"
strOutTable="DCDB_FeatureName"

Create Table "scratch_table" (Search Char(60),X Float,Y Float) file strWorkingFolder+"scratch_table.TAB" TYPE NATIVEX Charset "UTF-16" Locale "en-AU"
Create Map For scratch_table CoordSys Earth Projection 8, 116, "m", 147, 0, 0.9996, 500000, 10000000 Bounds (-7745844.29605, 2035.05684549) (8745844.29605, 19997964.9432)
Map From scratch_table
Set Map Layer 1 Editable On
'Change Group By
Create Object As Union From DCDB_Full_Copy Into Table scratch_table Group by strFieldName Data Search=FEAT_NAME,X=X,Y=Y
Commit Table scratch_table Interactive
'Check which rows to delete
select * from scratch_table where Search = "" into Selection
Delete from Selection
'Clean up values and add search type
Update scratch_table Set Search = SearchReplace(Search, ",", " ")+" [FeatureName]" DropIndex Auto
Commit Table scratch_table
Pack Table scratch_table Graphic Data Interactive
'make sure x and y are updated
Update scratch_table
    Set X = CentroidX(obj), Y = CentroidY(obj)
'set order
Select * From  scratch_table Order By Search Into Sorted
Commit Table scratch_table
'set name of table to save data to
Commit Table Sorted As strWorkingFolder+strOutTable+".TAB" TYPE NATIVEX Charset "UTF-16" Locale "en-AU" Interactive
Close Table scratch_table Interactive
Close Table Sorted Interactive

It is just

dim strFieldName as String
dim strOutTable as String

That needs to change on each call of this code.

2

Just dump your code into a sub procedure but remove the assignment of the two string variables strFieldName and strOutTable. These should be passed to the procedure as parameters. Also, any other variables you use within that sub procedure, e.g. strWorkingFolder, will either need to be declared within the sub procedure itself or outside of any sub or function so as to give it modular scope (which just means to allow any of the code from the mapbasic module (file) to access it).

e.g.

Declare Sub Main
Declare Sub MyProcedureName( ByVal strFieldName as String, ByVal strOutTable as String )

Dim strWorkingFolder as String

Sub Main
    '// some stuff here...
    strWorkingFolder = "C:\...\SomeFolder"
    Call MyProcedureName("LotPlan", "DCDB_Lotplan")
    Call MyProcedureName("Address", "DCDB_Address")

End Sub

Sub MyProcedureName( ByVal strFieldName as String, ByVal strOutTable as String)

    Print "Starting DCDB_FeatureName"

    Create Table "scratch_table" (Search Char(60),X Float,Y Float) file strWorkingFolder+"scratch_table.TAB" TYPE NATIVEX Charset "UTF-16" Locale "en-AU"

    '// etc...

End Sub
  • It looks like I have to copy the code (creat table...) multiple times then for each run. What I'd like to do is call it as >> MyProcedure (LotPlan, DCDB_Lotplan) >> MyProcedure (Address, DCDB_Address) etc where just these values are being passed in. Sort of like using the SearchReplace function you helped me with yesterday. – GeorgeC May 12 '17 at 8:00
  • 1
    I've amended the code to show how you might do that. Hopefully I've understood what you want correctly? – T_Bacon May 12 '17 at 8:04
  • A typical trap, I have fallen into it myself, is that you forget to declare the parameters using ByVal. If you don't use ByVal you can only pass variables to the procedure and not fixed values like here – Peter Horsbøll Møller May 15 '17 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.