Aliasing with Access 2.0I have the following example that works with no error:
Buf="C:\data\mydata.mdb"
Set DB = DBEngine.Workspaces(0).OpenDatabase(Buf)
Set DSResult = DB.OpenRecordset("Select count(*) from Table1", dbOpenDynaset)
But the following lines produce an error:
Buf="C:\data\mydata.mdb"
Set DB = DBEngine.Workspaces(0).OpenDatabase(Buf)
Set DSResult = DB.OpenRecordset("Select count(*)
xcount from Table1", dbOpenDynaset)
As you see the only difference is trying to show count(*) as xcount. Is there a way to get around this? I have tried using SQLPassThrough method also but I still get an error.
I'd appreciate your help. Thanks!
From: Jim
Email: 72253.1465@compuserve.com
SELECT Count(*) AS xcount FROM table1
Unfortunately this is one of those little gems that is pretty well hidden in the documentation, so it's pretty easy to miss. The reason why the example, with no attempted alias, works is because the JET engine figures out that the value is derived and therefore cannot inherit a value directly from a field in the base table, so it generates a name for the column itself and aliases it quietly. Sometimes the resultant names make sense, sometimes they don't. However, this does at least still allow you to reference that field via its index. As it is the only field in this case you could simply use DSResult.Fields(0) which gives you the value of the first field in the recordset.
By the way, the reason why your SQL passthrough made no difference is that when you are hitting an Access database from VB you are only using one database engine. When you are accessing another database via ODBC through VB's database engine (JET) you can have either engine execute the query (if it's in the appropriate syntax) by specifying SQLPassthrough (to have the back end process it) or not - which will have JET process the SQL. When you are directly opening an Access MDB there is no back-end database so there is no second engine to Pass Through to. Make sense?
Hope this helps you out.