Article ID: 191808
Article Last Modified on 3/2/2005
TABLE1 TABLE2 ------- ------- col colIf the statement "select * from table1, table2" is issued, there will be two fields in the resulting recordset with the name of "col". ActiveX Data Objects (ADO) does not rename any columns. To workaround this, alias the column names as indicated in the following example:
select Table1.col as A table2.col as B from a, bIf you do not alias the column names you can use the field property BASETABLENAME to determine the parent tablename.
rs.open "...",cn,adOpenKeyset
debug.print rs(0).properties("BASETABLENAME")
Getting the information about the BASETABLENAME is an expensive
proposition and many backends do not readily provide this information.
Dim cn as new connection
Dim rs as new recordset
Dim i as integer
cn.Open "Provider=SQLOLEDB;" & _
"Data Source=<Server Name>;" & _
"Initial Catalog=Northwind;" & _
"User ID=<username>;PASSWORD=<password>"
rs.ActiveConnection = cn
rs.CursorType = adOpenStatic
rs.Open "select customers.companyname, shippers.companyname " & _
"from customers, orders, shippers " & _
"where customers.customerid=orders.customerid and " & _
"orders.shipvia=shippers.shipperid"
For i = 0 To rs.Fields.Count - 1
Debug.Print rs(i).Name
Next
The preceding code results in two fields with the name companyname.
Modify the select to use aliases. For example:
rs.open "select a.companyname As CustomersCompany, " & _
"b.companyname As ShippersCompany " & _
"from customers a, orders c, shippers b " & _
"where a.customerid=c.customerid and c.shipvia=b.shipperid"
Debug.print rs(i).properties("BASETABLENAME") & ""
However, obtaining BaseTableName is an expensive operation, is only supported for serverside recordsets, and is not supported by all providers.
Keywords: kbhowto kbdatabase KB191808