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!

Loading a access record into a vb variable

Status
Not open for further replies.

benza

IS-IT--Management
May 15, 2002
1
NZ
I am creating a vb applicaion that links to an access database. I need to load a value from a particular field in a table into variable in vb. Is there an easy way to do this?

Currently I am loading the database value that I need into a text box on a form in order to use it in my vb code which is quite tedious...
 
Best way is to use Open database method in code - there are many examples of how to do this - and then cycle through the data table, filling program variables.

eg

do until rs.eof
for x = i to 100
thisvalue(i)= rs(i)
next x
rs.movenext

...............where rs(i) is the column of the table, ie rs(0) is the first one


Regards,

Alex Kudrasev
 
I don't believe you you need the for next loop. All that is necessary is:

private sLastname as string
do until rs.eof
rs.fields("Lastname") = sLastname
rs.movenext
loop

To prove the efficacy put this before the loop
Msgbox slastname

Make sure that you have only a few records in the db or put breakpoint so that you can stop the looping.
 
Do you need just one at a time or a few of the same type?

How long do you need the data?

Will it change?

One value should be read into a simple local variable
Dim sName as string
sName = rs!FirstName & " " & rs!LastName

Multiple Values should be in an array
rs.MoveLast
lNumValues = rs.RecordCount
rs.MoveFirst
Dim aValue(lNumValues+1) as string
For i = 1 to numValues
aValue(i) = rs!FieldName
rs.MoveNext
Next

If you need the data for a while make the array a global public and just ReDim with the correct number of items.

I use number of items plus 1 so that i don't have to use the array's 0 index - everything starts from one

Hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top