Solved

Field Name with alias

Posted on 2001-06-06
6
944 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
Technology Partners: 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 25 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 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