Can you help with the following code – it errors on the select statement:
Dim sQuery As String
Dim pQuery As String
Dim nCol As Integer
Dim nNumCols As Integer
Dim arrColNames() As String
Dim arrColValues() As String
Dim LeftCamera As Alias
Dim RightCamera As Alias
Dim myTrackName As Alias
Dim myInterval As Alias
'**Getting the name of the query (instead of Selection)
sQuery = SelectionInfo(SEL_INFO_SELNAME)
Print "my sQuery = " + sQuery
LeftCamera = sQuery & ".Link"
RightCamera = sQuery & ".GoProLink"
myTrackName = sQuery & ".Trackname"
Open Table "G:\GPS_Tracks\All_VideoTrackPts.TAB" Interactive
Add Map Auto Layer All_VideoTrackPts
Select * from All_VideoTrackPts where myTrackName = "myTrackname" into myQuery
Browse * From myQuery
'pQuery = SelectionInfo(SEL_INFO_SELNAME)
'myInterval = pQuery & ".Seg_Interval_s"
'Fetch First From pQuery
Print "my LeftCamera = " + LeftCamera
Print "my RightCamera = " + RightCamera
Print "my TrackName = " + myTrackName
'Print "my pQuery = " + pQuery
'Print "my Segment = " + myInterval
Why does sQuery need to be defined as an alias? It's just the name of the selection table as a string and is later used to build aliases. It doesn't need to be an alias itself as far as I know.
I think there's a lot of confusion and overuse of aliases. They're only needed when you don't know table and field names at compile-time. An alias essential defers this until run-time.
Doing a bit of 360degree photography are we?
I think the key problem area in your code is one of precedence. In the following statement:
If you're expecting this query to run on a field called myTrackName in the table All_VideoTrackPts, I'm afraid it doesn't. That alias you defined earlier, with the same name, will take precedence. If you want to guarantee that it's a field from the database, prefix it with the table name. e.g. All_VideoTrackPts.myTrackName
Secondly, your query is searching for a literal string "myTrackname" every time, which might not be what you're expecting. If you're trying to look-up from All_VideoTrackPts based on the current selection, you need to evaluate the alias and then search based on the result.
Dim myTrackName as string
TrackNameAlias = sQuery & ".Trackname" ' This will evaluate to something like "Query1.Trackname"
myTrackName = TrackNameAlias ' Evaluate this at run-time. myTrackName now contains the contents of Query1.Trackname
Select * from All_VideoTrackPts where All_VideoTrackPts.myTrackName = myTrackname into myQuery ' Look-up that track name
It's quote common to prefix variable with something to make them working variable in MapBasic. In you example, you've used "my"... I often use "work", but it's odd to have myTrackName in the database? I would have assume TrackName would be more likely, so check that this isn't a bug in your code.
There are a few other things that you could look at to tidy your code. For example, check that there is something selected before proceeding.
Hope that helps.
Thanks. I think the original question demonstrates how people can get themselves into trouble with aliases.
One of the most common uses, where it's not necessary, is in table names. For example:
Select * from tableName into WORK noselect
tableName could be a string variable containing the actual table name to be queried. It doesn't need to be an alias. Otherwise, tableName is taken literally as the table name.
Although I'm an old school software engineer, I've never been a huge fan of Hungarian notation for variable names. This is when one would prefix the variable with its type. For example: dim iCount as integer There are, however, some exceptions to my rule. In languages like C/C++, I've always prefixes pointers with a "p" and in MapBasic, some indication that a variable is an alias is quite useful. This is because a=b is not a straight value copy when b is an alias... it's a run-time evaluation of b. So, poor variable names aside, an "alias" prefix/suffix makes codes much easier to understand:
a = bAlias
Select * From All_VideoTrackPts Where Trackname = "myTrackName" Into myQueryFetch First From myQuery
Print "my LeftCamera = " + myQuery.Link
sQuery = SelectionInfo(SEL_INFO_SELNAME)aTrackName = sQuery + ".Trackname"aLeftCamera = sQuery + ".Link"aRightCamera = sQuery + ".GoProLink"'Copy the column values from the first row into string variablesFetch First From sQuerysTrackName = aTrackNamesLeftCamera = aLeftCamerasRightCamera = aRightCameraOpen Table "G:\GPS_Tracks\All_VideoTrackPts.TAB" Interactive'assume the track column is called 'TrackName'...?Select * from All_VideoTrackPts where TrackName = sTrackname into myQueryBrowse * From myQueryFetch First From myQuery'no need to use aliases here since we have named the query result and know the column namessInterval = myQuery.Seg_Interval_s
Select ...Into SomeName
Global aLeftCamera As AliasGlobal maRightCamera As Alias 'PHM: Renamed these to include m for modular and a for Alias typeGlobal maTrackName As Alias 'PHM: Renamed these to include m for modular and a for Alias typeGlobal mnInterval As Integer 'PHM: Renamed these to include m for modular and i for Integer typeGlobal moPoly as Object 'PHM: Renamed these to include m for modular and o for Object type
Sub GetmyVidFilesDim sQuery As String 'PHM: should be a stringDim nCol As IntegerDim nNumCols As IntegerDim arrColNames() As StringDim arrColValues() As String
'**Getting the name of the query (instead of Selection)sQuery = SelectionInfo(SEL_INFO_SELNAME)
'ERRORS HERE "EXPRESSION DOES NOT EVALUATE TO A COLUMN OR TABLE NAME" This was working but now is broken -- See further down'Should be fixed now that sQuery is a String variable and not an Alias variableFetch First From sQuery
aLeftCamera = sQuery & ".Link"maRightCamera = sQuery & ".GoProLink"maTrackName = sQuery & ".Trackname"
Print "my LeftCamera = " + aLeftCameraPrint "my RightCamera = " + maRightCameraPrint "my TrackName = " + maTrackName'Print "my pQuery = " + pQuery'Print "my Segment = " + mnInterval
Sub GetmyTrackFilesDim pQuery As StringDim nCol As IntegerDim nNumCols As IntegerDim arrColNames() As StringDim sTrack As String 'You need a string to hold the current track name
'**This might be a bit unsafe as you don't know which record you are pointing at'**nor do you know if the query exists or even which query your alias refers to
Print "my LeftCamera = " + aLeftCameraPrint "my RightCamera = " + maRightCameraPrint "my TrackName = " + maTrackName
sTrack = maTrackName
Open Table "G:\GPS_Tracks\All_VideoTrackPts.TAB" InteractiveAdd Map Auto Layer All_VideoTrackPts
Select * from All_VideoTrackPts where TrackName = sTrack '**now we refer to the current value stored in the string variable into myQuery'Errors here :Tbale not open'I assume that has been fixed too as you now should select with a value that existsBrowse * From myQuery
'**Getting the name of the query (instead of Selection)'pQuery = SelectionInfo(SEL_INFO_SELNAME)
Fetch First From myQuerymnInterval = myQuery.Seg_Interval_s
'PHM: These Alias variables still point back at the earlier query'PHM I have given an alternative way to get to the values using the named query.named columnPrint "my LeftCamera = " + aLeftCamera 'You can refer to myQuery.LinkPrint "my RightCamera = " + maRightCamera 'You can refer to myQuery.GoProLinkPrint "my TrackName = " + maTrackName 'You can refer to myQuery.TracknamePrint "my pQuery = " + pQueryPrint "my Segment = " + mnInterval
True for fields but not tables names. For example, the following is fine:
dim tableName as string
tableName = "MyTable"
select * from tableName into FOO noselect ' this will select from a table called MyTable
Someone earlier said use "run command". The only time that's necessary with a select statement is when there's a variable number of fields (unknown until run-time) you want to select.