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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get data by name vice by field number.

Status
Not open for further replies.

tommytx

Programmer
Nov 27, 2002
28
US
I am using VB6 and the ADODB.Connection.
How do I say List2.AddItem db_url

I don't want to fill the List1 box by calling .Fields(2) names.
List1.AddItem rstRecordSet.Fields(2)

db_Url is the actual field name used in the database.

In other words how do I get the item from the database into the list box using its name instead of the field number as above?

Also how do I get the rec number ID without all the mumbo jumbo above.
ADODB.Connection
List1.AddItem rstRecordSet.Fields(2)
List2.AddItem rstRecordSet.AbsolutePosition
rstRecordSet.Fields(1).Name & "=" & rstRecordSet.Fields(1)
Each record number is called ID.

Field 1 = name db_Url

Thanks.

 
I do this all the time.

While Not RS.EOF
Call List1.AddItem("" & RS("FieldName"))
List1.ItemData(List1.ListCount - 1) = Val("" & RS("IdField"))

RS.MoveNext
Wend

The .ItemData property of a list box control allows you to store longs. This is very convenient for ID fields from a database. When you want the ID field from the item the user picked, you do...

Dim iSelectedId as Long

If List1.ListIndex >= 0 Then
iSelectedId = List1.ItemData(List1.ListIndex)
End If

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
RTFM or F1 comes to my mind....

from the manual
As with any ADO collection, the Item property is the default property of the collection. It returns the individual Field object specified by the name or index passed to it. Therefore, the following statements are equivalent for the sample Recordset:

objField = objRecordset.Fields.Item("ProductID")
objField = objRecordset.Fields("ProductID")
objField = objRecordset.Fields.Item(0)
objField = objRecordset.Fields(0)

Regards

Frederico Fonseca
SysSoft Integrated Ltd
 

Actually, to be picky, it is:

objField = objRecordset.Fields.Item("ProductID").Value
objField = objRecordset.Fields("ProductID")").Value
objField = objRecordset.Fields.Item(0)").Value
objField = objRecordset.Fields(0).Value

Myself, I like objRecordset!ProductID format,
but I do use objRecordset("ProductID" & intX) if I have to loop thru Fields in a recordset like:
ProductID1
ProductID2
ProductID3
ProductID4
ProductID5 (etc.)

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top