Using variables as field names (4 Viewers)

Jackdaw

New member
Local time
Today, 13:16
Joined
Jul 3, 2025
Messages
7
ekoto asked this January 1, 2000 but the answers did not seem to fit. Here is my code:
i = 1
rdsR.MoveFirst
While Not rdsR.EOF
a = ""
For j = 1 To 24
b = "r" & Right(Str(100 + i), 2) 'b is r01, r02, r03 up to r24
k = rdsR! ' this fails - Item not found in this collection
c = Chr(k) ' table has numbers between 40 and 110
a = a & c ' this condenses 24 fields into 24 ascii characters
Next j
Print #1, a
i = i + 1
rdsR.MoveNext
Wend
When I wrote the code to populate the table (years ago) I ran into the problem and ended up with 24 lines, rdsR![r01] = myvar(i), rdsR![r02] = myvar(2), etc. There must be a better way.
 
Jackdaw,

Can you tell us in plain English what you are trying to achieve? Perhaps an example of some input and corresponding output/result.
What do you mean by
Code:
k = rdsR!
?
 
First, to have fields named R01, R02, ..., R23, R24 stinks to high heaven of a non-normalized table, which is already problematic. Whatever you are doing with a 24-field non-normalized list like that is probably going to be incredibly inefficient if not outright wrong.

Second, if you ABSOLUTELY had to do this, the syntax MIGHT look like

rdsR.Fields("R" & Format( "00", i ) )
which would resolve internally in two steps to
rdsR.Fields("R" & "01") (if i = 1) and then to
rdsR.Fields("R01") - but you wouldn't see the intermediate step because VBA's application of concatenation is transparent to you. At this point, you would be looking for a field named R01. This works because the Fields collection is part of every opened recordset. Note that if the recordset is not opened, this doesn't work because there is no Fields collection to reference.
 
ekoto asked this January 1, 2000 but the answers did not seem to fit. Here is my code:
i = 1
rdsR.MoveFirst
While Not rdsR.EOF
a = ""
For j = 1 To 24
b = "r" & Right(Str(100 + i), 2) 'b is r01, r02, r03 up to r24
k = rdsR! ' this fails - Item not found in this collection
c = Chr(k) ' table has numbers between 40 and 110
a = a & c ' this condenses 24 fields into 24 ascii characters
Next j
Print #1, a
i = i + 1
rdsR.MoveNext
Wend
When I wrote the code to populate the table (years ago) I ran into the problem and ended up with 24 lines, rdsR![r01] = myvar(i), rdsR![r02] = myvar(2), etc. There must be a better way.
 
ekoto asked this January 1, 2000 but the answers did not seem to fit. Here is my code:
i = 1
rdsR.MoveFirst
While Not rdsR.EOF
a = ""
For j = 1 To 24
b = "r" & Right(Str(100 + i), 2) 'b is r01, r02, r03 up to r24
k = rdsR! ' this fails - Item not found in this collection
c = Chr(k) ' table has numbers between 40 and 110
a = a & c ' this condenses 24 fields into 24 ascii characters
Next j
Print #1, a
i = i + 1
rdsR.MoveNext
Wend
When I wrote the code to populate the table (years ago) I ran into the problem and ended up with 24 lines, rdsR![r01] = myvar(i), rdsR![r02] = myvar(2), etc. There must be a better way.
oops it should have have been k = rdsR! - tried several different dot and parentheses and quotes, and copied at the wrong moment. My table has 30 times 24 data points. One column gets updated every 2 weeks. I don't see how normalization would apply.
 
Since you didn't tell us WHY you are doing what you attempted to do, I don't see how normalization WOULDN'T apply. Look at the question from the viewpoint that we want to make what you are doing work better - but we can't do that if we don't know WHY you are doing something.
 
My table has 30 times 24 data points. One column gets updated every 2 weeks. I don't see how normalization would apply.
All those columns should be rows with some identification such as date or whatever you need for the two week separation.
 
You are forced to write code because your table isn't normalized. If you have to analyze this data, you can't use simple queries either. Repeating groups should ALWAYS be child tables to avoid writing code or complex queries to analyze them.
 
ekoto asked this January 1, 2000 but the answers did not seem to fit. Here is my code:
i = 1
rdsR.MoveFirst
While Not rdsR.EOF
a = ""
For j = 1 To 24
b = "r" & Right(Str(100 + i), 2) 'b is r01, r02, r03 up to r24
k = rdsR! ' this fails - Item not found in this collection
c = Chr(k) ' table has numbers between 40 and 110
a = a & c ' this condenses 24 fields into 24 ascii characters
Next j
Print #1, a
i = i + 1
rdsR.MoveNext
Wend
When I wrote the code to populate the table (years ago) I ran into the problem and ended up with 24 lines, rdsR![r01] = myvar(i), rdsR![r02] = myvar(2), etc. There must be a better way.
 
k = rdsR!(b) is what I attempted. It fails item not found in this collection
It's in a loop which creates a new "b" each pass. b is r01 on pass 1. r02 on pass 2 .. r24 on pass 24
So, what is the syntax which will take an integer from the defined cell in my table and and put it into variable k?
rdsR!(b) doesn't work. rdsR![eval(b)] doesn't work. No combination of .!"( that I have tried works.

Once I have k I convert it to a character and concatenate the characters so that for person 1 I have a 24 character string representing the 24 numbers in the first row of 24 numbers with the headings r01, r02, etc
Think of the table as 30 door-to-door vacuum cleaner salesmen with sales for JanA , JanB, FebA, FebB, etc but I chose to use r01 for JanA, etc in the so far vain hope that I could then loop them easily.
Why I want 24 character strings and whether or not my table is normalized is not relevant. It's what I want.

I had hoped that this was a help forum, not a vehicle for a self-describer immoderate moderator to vent his spleen over an irrelevance without attempting to understand.
 
you do not need bang (!) there.
simply:

k = rdsR(b)
 
Why are you 13 posts in and still trying bang notation. What part of you can not use bang did you not understand?
 
I had hoped that this was a help forum, not a vehicle for a self-describer immoderate moderator to vent his spleen over an irrelevance without attempting to understand.

Actually, it IS a help forum. I was, in fact, offering help - if you looked far enough. My initial answer, given in post #4, contained two parts. One was a criticism of the approach. Get past that because the other part of post #4's answer was the immediate and direct solution to your problem.

You are attempting to use a type of notation called "bang" notation after a common nickname for the "!" character. You know that we use names that end in DOT COM (.COM) when talking about web sites. I.e. we pronounce the DOT. Well, when talking in shorthand like we usually do, you talk about DOT or BANG as a way to specify the punctuation in the string we are reading.

Access VBA has DOT notation, too. It is the syntax indicator for examining properties of objects... such objects as your recordset, which is a particular type of object. Using DOT notation, you can look at properties such as the .Bookmark or the .EOF or .BOF indicators.

One of the properties of an open recordset object is the .FIELDS property, which returns a COLLECTION of individual FIELD objects, one for each field in the recordset. If you have five fields in a record, you will have five FIELD objects as members of the recordset's FIELDS collection. Each of the FIELD objects has a text name that is unique within that collection.

Usind DOT notation, you can select one of the FIELD objects in the FIELDS collection by using its text name, as in rdsR.Fields("R01"). BANG notation looks at members of collections, but is picky about the syntax because it a RUN-TIME operator. The correct syntax with BANG notation would be rdsR!R01. There is a third notation that would also work... rdsR("fieldname").

There: THREE ways to do what you want.

1. rdsR.Fields( "fieldname" ) - which returns the value of the field, and the name CAN be built by concatenation.

2. rdsR!fieldname - which returns the value of the field, but you cannot build the name by concatenation via run-time evaluation.

3. rdsR( "fieldname" ) - which returns the value of the field and the name CAN be built by concatenation. Option 3 is actually option 1 but taking into account those properties that are defaults.

More precisely, using DOT notation, your actual reference is rdsR.Fields( "fieldname" ).Value - but by omitting the explicit references to the default properties for each object, you omit .Fields and .Value, leaving you with option 3. Note that this is a COMPILE-TIME reference, for which the only thing that matters at run-time is the fieldname value, which can be passed in via string constant or via a string variable containing the fieldname. BANG notation DOES NOT SUPPORT concatenating the name in that context.

Summary: GIVE UP on the BANG notation for this process. You MUST use a method that allows you to build the name via concatenation. BANG notation is not going to help you here.
 
k = rdsR!(b) is what I attempted. It fails item not found in this collection
It's in a loop which creates a new "b" each pass. b is r01 on pass 1. r02 on pass 2 .. r24 on pass 24
So, what is the syntax which will take an integer from the defined cell in my table and and put it into variable k?
rdsR!(b) doesn't work. rdsR![eval(b)] doesn't work. No combination of .!"( that I have tried works.

Once I have k I convert it to a character and concatenate the characters so that for person 1 I have a 24 character string representing the 24 numbers in the first row of 24 numbers with the headings r01, r02, etc
Think of the table as 30 door-to-door vacuum cleaner salesmen with sales for JanA , JanB, FebA, FebB, etc but I chose to use r01 for JanA, etc in the so far vain hope that I could then loop them easily.
Why I want 24 character strings and whether or not my table is normalized is not relevant. It's what I want.

I had hoped that this was a help forum, not a vehicle for a self-describer immoderate moderator to vent his spleen over an irrelevance without attempting to understand.
Now now, don't throw your toys out of the pram.
It is not our fault if you cannot understand that you do not use the bang notation. :(

Here is an idea, why not just try the syntax that has been offered?
 

Users who are viewing this thread

Back
Top Bottom