Link to home
Start Free TrialLog in
Avatar of mukunth
mukunth

asked on

Field Name with alias

I'm using a recordset in ADO to access data form multiple tables,say,with the following structure:

Table Name : One
Field Names : name

Table Name : One
Field Names : name

Now I say,
rs.Open "select one.name,two.name from one,two",cn
where rs=recordset name
I want to access this recordset with the fieldname(not with ordinal position) as rs("name") etc..It gives me only the first table's value.Is there any way to retrieve using the alias name,ie,for the second table?
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

rs.Open "select one.name AS Name_One,two.name AS name_Two from one,two",cn

Now you can access the fields with
rs("name_one") and rs("name_two")

You might however simply try to access the fields by their indexes:
rs(1)

Cheers
The following code, allows me to use the fully qualified field name:

    Dim rstADO As New ADODB.Recordset
    Dim cnnADO As New ADODB.Connection
    cnnADO.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\testbed\cremosa2k.mdb;Persist Security Info=False"
    cnnADO.Open
    rstADO.Open "select MyTable.Name,MyTable1.Name From MyTable,MyTable1", cnnADO, adOpenStatic, adLockReadOnly
    MsgBox rstADO.Fields("MyTable.Name").Value & " / " & rstADO.Fields("MyTable1.Name").Value
    rstADO.Close
    cnnADO.Close
    Set rstADO = Nothing
    Set cnnADO = Nothing

Avatar of mukunth
mukunth

ASKER

to angelIII : Yes,I can "alias" the field name itself but is there any property through which I can retrieve the table aliases?


to TimCottee : rs("tablename.fieldname") doesn't work.It says "Item cannot be found in the collection corresponding to the requested ordinal or position"
Yes, you can:

rs("name_one").Properties("BASECOLUMNTABLE")
rs("name_one").Properties("BASECOLUMNNAME")

Should give you the info you are looking for...

Cheers
Avatar of mukunth

ASKER

To angelIII :

I tried but it didn't return anything.ie.it returns NULL value.Is the following code correct?:

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

cn.Open "provider=sqloledb;data source=(local);initial catalog=pubs;uid=sa;pwd=;"
rs.Open "select t.i,t1.i from t,t1", cn

Dim i As Integer
For i = 0 To rs("i").Properties.Count - 1
List1.AddItem rs("i").Properties(i).Name & " = " & rs("i").Properties(i).Value
Next

The above adds ALL the properties in the listbox,but most of the properties have NULL as their value including BASETABLENAME and BASECOLUMNNAME.So,what goes?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial