Solved

Field Name with alias

Posted on 2001-06-06
6
881 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
  • 3
  • 2
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 25 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now