?
Solved

Field Name with alias

Posted on 2001-06-06
6
Medium Priority
?
1,021 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:mukunth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6159220
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
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6159254
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

0
 
LVL 1

Author Comment

by:mukunth
ID: 6162355
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"
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6162583
Yes, you can:

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

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

Cheers
0
 
LVL 1

Author Comment

by:mukunth
ID: 6162993
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?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 75 total points
ID: 6163025
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset


set cn = new ADODB.Connection
set rs = new ADODB.Recordset

cn.Open "provider=sqloledb;server=(local);initial catalog=pubs;uid=sa;pwd=;"

set rs.ActiveConnection = cn
rs.CursorLocation = adUseClient
rs.LockType = adLockPessimistic
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

This worked for me...
It seems that the locking value enforces ADO to get the underlying tablenames and column names...

Cheers

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question