Spatial Plus Knowledge Base

By Spatial Plus

MapBasic
Q: How do I load a MapBasic application automatically when I start MapInfo?
A. The easiest way of loading a MapBasic application automatically when MapInfo is started is achieved by adding the application via MapInfo's Tool Manager.The Tool Manager is located under the Tools Menu in MapInfo Professional.Use the Autoload option to ensure the MapBasic application is loaded every time MapInfo is started.

Another way of starting a MapBasic application automatically in MapInfo (generally less well known as the previous method) is to edit the StartUp.WOR file in the directory where MapInfo is located.This file is a workspace that loads up automatically on starting MapInfo.To run a MapBasic application add the "Run Application" command to the file using a text editor such as notepad.

!Workspace
!Version 400
!Charset WindowsLatin1
Run Application "{Application File}"

It is also possible to add other things to this workspace such as setting default styles.Below is a command that will set the default symbol to a red arrow:

Set Style MakeSymbol (48,16711680,36)

Q: How do I get the username in MapBasic?
A. To get the username under a Windows 32 bit environment you need to call the advapi.dll.Below is a snippet that creates a function called GetUserName that returns a string with the username.

Declare Function GetUserNameA Lib "advapi32.dll" (User As String, i As Integer) As Logical
Declare Function GetUserName() As String
Function GetUserName() As String
Dim User As String
Dim i As Integer
i = 30
User = String$(i," ")
If GetUserNameA(User,i) Then
GetUserName=RTrim$(User)
Else
GetUserName = ""
End If
End Function

Q: How can I find out if the user pressed the ESCAPE key during a selection?
A. The CommandInfo(CMD_INFO_INTERRUPT) will return TRUE if the user pressed the key on the last selection.

Q: How do I get MapInfo to read a MBX that was compiled by a later version of MapBasic?
A. If a MapBasic application is compiled in a later version of MapBasic than that of your MapInfo version, it will come up with an error saying it is not compatible.If there are no new commands or functions used in this later version of the MBX, it is possible to make it compatible with earlier versions.To do this you need to use a binary editor (not notepad) and edit the top part of the file where it says its version number.For example, version 5.5 will read "Version 550" so replace the "550" with an earlier number such as "450" (i.e.version 4.50).

If the mapbasic application does contain commands or functions that don't exist in the earlier version, it will most likely produce an error.However about 95% of the commands or functions do exist in earlier versions so there is a good chance it will work.

Q: How do I add a number of working days to a date in MapBasic?
A. There is no raw function in MapBasic that calculates the number of working days.However the code below show how it can be done using other functions.This assumes the workings are from Monday to Friday.

Function AddWorkingDays( ByVal StartDate As Date, ByVal WorkingDays As Integer) As Date
Dim Remainder,RealDays As Integer
RealDays = Int(WorkingDays / 5) * 7
Remainder = ((WorkingDays / 5) - Int(WorkingDays / 5)) * 5
If Remainder <> 0 And Remainder < (Weekday(StartDate) - 1) Then
RealDays = RealDays + 2
End If
RealDays = RealDays + Remainder
AddWorkingDays = StartDate + RealDays
End Function

MapInfo SQL
Q: How do I select just certain types of objects such as text in MapInfo?
A. The SQL statement to do this is as follows:

Select * From {table} Where Str$(Obj) = "TEXT"

For other objects use the following key words in place of "TEXT":

  • POINTS
  • LINE
  • POLYLINE
  • ARC
  • REGION
  • ELLIPSE
  • RECTANGLE
  • ROUNDED RECTANGLE
Q: How do I select complex objects in MapInfo?
A. Complex objects contain more than one object within themselves.A good example is a region shaped like a donut.To select complex objects use the following statement:

Select * From {table} Where Val(Str$(ObjectInfo(Obj,21)) > 1

Q: How do I find the longest entry in a MapInfo character column?
A. To select the biggest entry in a particular column use the following select statement:

Select * from {table} where Len({column}) = (Select Max(Len({column})) From {table})

To find the smallest entry use Min instead of Max.

Q: How do I select every second row in a MapInfo table?
A. To select every second row use the following select statement:

Select * From {Table} where Rowid Mod 2 = 0

To get every third row replace the 2 with 3.To get the inverse selection use "<>" instead of "=".

Q: How do I select rows that contain the same items in another table similar to using a lookup table?
A. To select items in a table that are in a lookup table use the following statement:

Select * From {Table} Where {Column} = Any (Select {Column} From {Lookup Table})

If you want the opposite where you want to select everything not in the lookup table replace the "=" with "<>".

Q: How do I select region objects that are adjoining my selection in MapInfo?
A. To select the regions objects that are adjoining my selection use :

Select * From {region table} where Obj Intersects Any (Select Obj From Selection)

Q: How do I put text object strings into a column in MapInfo?
A. To assign the text object string to column use the following SQL statement:

Update {Text Table} Set {Column}=ObjectInfo(Obj,3)

If the {text table} contains other objects that are not text, an error will occur.To overcome this problem, select out all the text from the table first and using the selection.To extract out all the text object use the following SQL statement:

Select * From {Text Table} Where Str$(Obj)="TEXT"

Q: How do I select all the points outside a region in MapInfo?
A. It is possible to select all the points outside a region by using the following SQL statement:

Select * From {Points Table} Where Not Obj Within Any (Select Obj From {Region Table})

Q: How do I create a MapInfo table that is a selection of another table without duplicating the data?
A. It is possible to create a table that is really is just a selection of another table.To do this you need to create a tab file in a text editor such as notepad.To demonstrate this pretend that a table of the world exists (world.tab) and you want to create another table with just America in it.Rather than copy the data create a tab file called america.tab in notepad with the following lines:

!table
!version 300
!charset WindowsLatin1
Open Table "World" Hide
Select * From World where Country="America" Into America NoSelect

Now it will look like a normal MapInfo table however the data is really coming from the world table.The advantages of doing this way include not having to maintain two datasets and less disk space is used.

Tables
Q: How do I change the data in a MapInfo table to upper case?
A. To change text data to upper case use the update command with the Ucase$ function:

Update {table} set {Column} = UCase$({Column})

For lower case use Lcase$ and for proper case use Proper$.

Q: How do I get rid of spaces on either side of my data in a MapInfo column?
A. To get rid of the spaces use the update command with the Ltrim$ and Rtrim$ (stands for left & right trim) functions:

Update {table} set {Column} = Ltrim$(Rtrim$({Column}))

Q: How do I remove the custom labels from a single layer in a MapInfo mapper window?
A. The standard MapInfo command "Clear custom labels" removes all the custom labels in the mapper.Sometimes you may only want to clear a particular layer's custom labels.To do this type the following in the MapBasic window:

Set Map Layer {x} Label Default

where {x} = Layer Number (the topmost layer is equal to 1).

Q: How do I revert back to the default color palette?
A. MapInfo stores the custom defined color palette in a file called "MapInfow.clr" in your Windows directory.(usually c:windows or c:winnt).To revert back to the default colors just delete this file out of the Windows directory.

Workspaces
Q: How do I read a workspace created in MapInfo 5.5 into an earlier version?
A. Workspaces are sometimes not downward compatible with different versions of MapInfo.This usually happens when commands that are only available in later versions are contained in the workspace.One of the most common commands that is new to version 5.5 is the "Set Window FrontWindow() Printer ...".This command occurs when there is a layout in the workspace that doesn't use the default printer settings.To make this workspace readable in early versions, you need to edit the workspace file through a text editor such as notepad.Two lines within the workspace need to be changed:

  1. The second line that reads "!Version 550" needs to be changed to "!Version 400"
  2. Near the bottom of the file should be a line that reads something like "Set Window FrontWindow() Printer ...".Just take this line out entirely.

Then save the file.This should now be accessible to earlier MapInfo versions (down to version 4).


Published Tuesday, January 11th, 2000

Written by Spatial Plus



If you liked this article subscribe to our newsletter...stay informed on the latest geospatial technology

© 2016 Directions Media. All Rights Reserved.