Here is a bit of code I use to solve this problem:
No warranties, of course !
' **************************************************************************
' A couple of utility functions to make MapBasic Life easier.
' dcOpenTable( Path, Name ) -- opens a table if it's not already open
' dcCheckOrCreateColumn ( Table, ColName, ColType ) -- create a column
if it doesn't exist
'
' Designed to be linked to your program.
' Include "dctabl.def" in your main routine to define the functions.
'
include "mapbasic.def"
Include "c:\projects\bes pfp\pfptools\dctabl.def"
' ======================================================================
' dcIsTableOpen
'
'
'
' ======================================================================
Function dcIsTableOpen(ByVal sTableName as String) as Logical
Dim l as Logical
OnError goto NotOpen
l = TableInfo(sTableName, TAB_INFO_READONLY)
OnError Goto 0
dcIsTableOpen = TRUE
Exit Function
NotOpen:
OnError goto 0
dcIsTableOpen = FALSE
Exit Function
End Function
' ======================================================================
' dcOpenTable
' Path The file path, not including table
name
' of the physical location of the
table in case
' it needs to be opened. If the table
is already
' open there is no promise that it
came from this
' location.
' Name The name of the *.TAB file.
' Alias The Alias used to open the file.
'
' ======================================================================
Function dcOpenTable(sPathName As String, ByVal sTabName as String, ByVal
sAlias as String) As Logical
Dim Username as String
OnError Goto NotOpen
Dim l As Logical
' print "Request is: " + sPathName + sTabname + " as " + sAlias
l = TableInfo(sAlias, TAB_INFO_READONLY)
dcOpenTable = TRUE
Exit Function
OpenTable:
OnError goto NoTable
If Right$(sPathName, 1) <> "\" then
sPathName = sPathName + "\"
End If
UserName = sPathName + sTabName
DlgName:
' print "Trying to open " + Username + " as " + sAlias
Open Table Username as sAlias
OnError goto 0
sPathName = PathToDirectory$(UserName)
dcOpenTable = TRUE
Exit Function
NotOpen:
' print "Not open..."
dcOpenTable = FALSE
Resume OpenTable
NoTable:
' print "Unable to open..."
UserName = FileOpenDLG(sPathName, sTabName, "TAB", "Please locate table: "
+ sAlias + " (" + sPathName + sTabname + ")" )
If Username = "" Then
dcOpenTable = FALSE
Exit Function
Else
Resume DlgName
End If
End Function
' ======================================================================
'
'
' ======================================================================
Function dcIsColumnThere(ByVal sTabName as String, sColName as String) as
Logical
Dim NumCols as Integer
Dim LoopCount as Integer
Dim ColQuery as String
Dim ColName as String
dcIsColumnThere = FALSE
' See if the column name exists...
NumCols = TableInfo (sTabName, TAB_INFO_NCOLS)
For LoopCount = 1 to NumCols
ColQuery = "Col" + LTrim$(str$(LoopCount))
ColName = ColumnInfo(sTabname, ColQuery, COL_INFO_NAME)
If ColName = sColName then
dcIsColumnThere = TRUE
Exit Function
End IF
Next
End Function
' ======================================================================
' dcCheckOrCreateColumn
' TableName The alias of the table containing
the column
' ColName The name of the column to check for
' ColType The data type if the column needs to be
created.
'
' Assumes the table is open...
' Further, does not check to see that the data type matches if the
column
' already exists.
'
' ======================================================================
Function dcCheckOrCreateColumn( ByVal sTabName as string, ByVal sColname as
string, ByVal SColType as String ) as Logical
Dim AlterTableText as String
If NOT dcIsColumnThere (sTabName, sColname) Then
' The column name does not exist.
Commit Table sTabName
AlterTableText = "Alter Table " + sTabname + " (Add " +
sColname + " " + sColType + ")"
Run Command AlterTableText
End If
dcCheckorCreateColumn = TRUE
End Function
================================================================
============ here's the content of dctabl.def ==================
Declare Function dcOpenTable(sPathName as String, ByVal sTabName As String,
ByVal sAlias as String) As Logical
Declare Function dcCheckOrCreateColumn( ByVal sTabname as String, ByVal
sColname as string, ByVal sColType as string) as Logical
Declare Function dcIsTableOpen(ByVal sTable as String) as Logical
Declare Function dcIsColumnThere(ByVal sTable as String, sColName as String)
as Logical
=================================================================
-----Original Message-----
From: joe baragar [mailto:[EMAIL PROTECTED]]
Sent: Monday, September 10, 2001 11:32 AM
To: [EMAIL PROTECTED]
Subject: MI-L Table Info
Hello all,
Can anyone assist me in finding a way in Mapbasic to determine if a specific
column exists by name within a table? What I want to do is determine if a
column (Can_ID)is present in the table, if it is not I want to create it.
Then I will populate it.
I'm working with multiple tables from multiple sources. Some of which have
the Can_ID column, others do not. But it is required.
Thanks,
Joe
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
_______________________________________________________________________
List hosting provided by Directions Magazine | www.directionsmag.com | To
unsubscribe, send e-mail to [EMAIL PROTECTED] and put
"unsubscribe MapInfo-L" in the message body.
_______________________________________________________________________
List hosting provided by Directions Magazine | www.directionsmag.com |
To unsubscribe, send e-mail to [EMAIL PROTECTED] and
put "unsubscribe MapInfo-L" in the message body.