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

Accessing Database Fields 1

Status
Not open for further replies.
Jun 28, 2002
34
GB
Does anyone know?

When I am Placing/Retrieving Data to/from a Database via ADO code I use the following..

RS.AddNew
RS!Forename = "John"
RS!Surname = "Smith"
Etc...
RS.Update

Is it possible to place a Variable instead of the actual hardcoded fieldname.

RS![Variable] = "John"

Any help would be most appreciated,

Thanking you all in advance

 
ADO recordsets have a Fields collection, which you can access either by index (0 - based) or by name.

rs.Fields(0)

or

rs.Fields(fldName)
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
You can also use string constants like so:

const FIRST_NAME as String = "Forename"

objRS(FIRST_NAME)="John"

Provides for somewaht more flexibility if the ordinal position of columns are likely to be changed due to database changes.

Anyway, the ! notation is obsolete, it is not a recommended notation anymore.
Greetings,
Rick
 
>Anyway, the ! notation is obsolete, it is not a recommended notation anymore.

True,

but so is using the default property!

i.e.
Code:
rs.fields(fldName)
vs
Code:
rs.fields.item(fldName)
Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
Question pertaining above about ! being obsolete. Trying to change up code in an application for based on this post and when I change my ! to log.Fields.Item(User)/log.Fields(User). I get error message saying that Item could not be found in collection corresponding to requested name of ordinal. Code works fine with the !, but since you stated obselete would like to get with times. Appreciate input since I am a new programmer in VB and would like to keep my job.

Dim connLog As ADODB.Connection
Dim log As ADODB.Recordset


Private Sub cmdCancel_Click()
End
End Sub

Private Sub cmdLogin_Click()

'If txtUser = log!User And txtPwd = log!Passwd Then
If txtUser = log.Fields.Item(User) And txtPwd = log.Fields.Item(Passwd) Then
Call Menu.Show
Me.Hide
Else
MsgBox ("Password not correct, try again")
txtPwd.SetFocus
SendKeys "{Home}+{End}"
End If

End Sub

Private Sub Form_Load()
Set connLog = New Connection
Set log = New Recordset

connLog.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Documents and Settings\default\Desktop\parking.mdb;Persist Security Info=False"
connLog.Open
Set log = connLog.Execute("Select * From Pwd")

'connLog.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Documents and Settings\default\Desktop\parking.mdb;Persist Security Info=False"
'log.Open "Select * From Pwd", connLog, adOpenDynamic, adLockOptimistic

'connLog.Close
End Sub

Richard
 
You might try putting User in quotes.
Code:
If txtUser = log.Fields.Item("User") And txtPwd = log.Fields.Item("Passwd") Then
Without the quotes, User is being interpreted as a variable, which opens up other doors

Dim FldName as String

FldName = "User"
If txtUser = log.Fields.Item(FldName)
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thanks CajunCenturion, it worked fine once I put into quotes.

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top