SilentRage47
SilentRage47

Reputation: 952

VB6 variable recordset name

Is it possibile to access a recordset using a variable for the name ?

For example, i've a table with 10 fields called Name01,Name02,Name03.....Name10. I need to cycle through them so it would be nice to just use one instruction insted of repeating the same one with 10 different name.

This is the code i'm using now

Sal01 = rsUtility!Order01

Sal02 = rsUtility!Order02

.... 

Sal10 = rsUtility!Order10

This is what i would like to accomplish :

for i = 1 to 10 
    VariableName = "Order" & i 
    Sal(i) = rsUtility!VariableName 
next i

Upvotes: 3

Views: 508

Answers (2)

George
George

Reputation: 2213

Since your variables have a 2 digit ending, you gotta use a proper format and not just "Order" & i, because it will result in Order1 and not Order01

For i = 1 to 10 
    Sal(i) = rsUtility("Order" & Format(i,"00") )
Next i

The loop above will assign a value to a corresponding array element from DB variables in the range from Order01 to Order10 inclusive

Upvotes: 1

HK1
HK1

Reputation: 12210

Here you go:

for i = 1 to 10 
    VariableName = "Order" & i 
    Sal(i) = rsUtility(VariableName)
next i

Upvotes: 3

Related Questions