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

Using variables within field names

Status
Not open for further replies.

paulharvey

Technical User
May 21, 2003
6
GB
Hi,

I am currently working on a database that has 100 fields of the name meterx, where x is a number from 1 to 100. Is there any way i can increment the field name using a loop in order to access the fields in turn ?

Thanks in advance.
Paul
 
Hi Paul!

Try this:

Dim intIndex As Integer
Dim strField As String
Dim rst As DAO.Recordset

Set rst = CurrentDb.OpenRecordset("YourTable", dbOpenDynaset)

For intIndex = 1 To 100
strField = "meter" & Format(intIndex)
Debug.Print rst.Fields(strField)
Next intIndex

Set rst = Nothing

You can do what ever you need to with the field. This also works in forms using control names: Me.Controls(strField).

hth


Jeff Bridgham
bridgham@purdue.edu
 
Many thanks for the prompt response Jeff.

However, using the above code, I receive the error message "Compile error: User-defined type not defined" It appears to be highlighting the "rst As DAO.Recordset" portion of the code. Any ideas ?

Paul
 
Apologies, I jumped the gun with that post before checking the help file.

For anyone who has the same problem with the code, in the code viewer, click Tools, References and add the Microsoft DAO 3.51 Object Library. Works a treat.

Thanks again Jeff for your code sample.

Paul
 
from a different perspective, Meter(N} for (N = 1 to 100 is an indication of a not well mormalized database, A more NORMAL[ized?] approach would probably include seperate records for each of hte items. In many designs using similar nomenclature, many of the fields so identified are not used for every record and thus take up 'realestate' un-necessaruly.





MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top