Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference database fields by number 1

Status
Not open for further replies.

tbuch

Programmer
Oct 17, 2000
80
US
Is there a way, in VB, to reference fields in a database table by number rather than by name?

tbuch
 

You have probably been using the banger until now, right?

rs!SomeField

Actually, or at least IMO, you should be doing this instead:

rs.Fields("SomeField").Value

To reference a field by the absolute position you would use:

rs.Fields(5).Value

But be aware: This is the current recordset field absolute position - not always the actual position in the table.
 
Thanx, CClint!

I knew it was like that with other some languages, but didn't know if it would work in VB.

tbuch
 
>Actually, or at least IMO, you should be doing this instead:

rs.Fields("SomeField").Value

Haven't MS said that using default properties should be avoided?, thus

rs.Fields.Item("SomeField").Value

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
I thought of typing that, and figured that someone would probably come back on it if I didn't.
But, I feel either way is sufficient.

My point was more the use of the banger.

What MS is referring to is the danger that a default property may change in the future.

I do not see this happening with ADO under VB6.

And, Fields is a collection, so "Item" should always be the default.

Using "Fields", and the property "Value", on the other hand, (instead of just x = rs("SomeField") ), I think this is good practice.
The Item property is about the only property I leave out anymore.

However, when you one day find that defaults are not used anymore (already the case), as opposed to the problem with changing defaults, then you will need to get into other habits, of coding with-out defaults.

Actually, you could just use the faster Collect method:

rs.Collect("SomeField")

No defaults used here.
 
different but related note! if necessary, I'll start a new thread...

but how do you extract a record object from a ADO recordset

ie
Code:
Private function foo(myRec as ADODB.Record) as string

How would I extract record to pass to foo from a full recordset?

I had a need for this recently, but couldn't work it out, so I had to pass about 5 strings in to the function!

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
The provider has to support this.

"rsADO" is an opened recordset.

Dim rec As ADODB.Record
Set rec = New ADODB.Record
rec.Open rsADO

If you receive the error: 3251, then the provider doesn't support is. (The Record object is used to access an item in a Web Storage System)

If the provider doesn't support the Record Object, just pass the Recordset object, or a clone of it (Set rsClone = rsAdo.Clone).



 
Thanks,

When I get aspare moment or five, I'll have a play! with that!

I was coming at the problem from the other end - trying to extract a record using a recordset method rather than a record one!

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top