Using strings to refer to form fields

  • Thread starter Thread starter Steve Hughes
  • Start date Start date
S

Steve Hughes

Hi, I'm trying to refer to fields within a form using a
variable string. I'm trying to create a procedure that
will create an SQL string depending on what option has
been selected in 8 lookup boxs on a form. I don't really
want to use 8 if statements and therefore I am trying to
use For count loop.

I just can't get it to view different fields in the form
at different stages of the loop.

A very simplied version of the code is shown below. It
should just display the contents of each of the 8 boxes.

can anyone please tell me where I'm going wrong?

Dim strAndField as String
Dim inos as Integer

For count = 1 To 8 Step 1
strAndField = "Category" & inos
MsgBox Me![ & StrAndField & ]
Next count

Many Thanks
 
Steve,
If you are trying to concatenate the contents of a number of
form controls, in this case 8, and the names of those
controls start at Category1 and run through to Category8
then try the following in the form code:

Dim intI as integer, strConCat as String

For intI = 1 to 8
strConCat = strConCat & Me("Category" & intI)
Next intI

************
If you want to add spaces between then:

For intI = 1 to 8
If intI < 8 then
strConCat = strConCat & Me("Category" & intI) & " "
Else
strConCat = strConCat & Me("Category" & intI)
Endif
Next intI

Or you could just concatenate spaces and Trim when loop
finished.
 
Thanks very much Nick, worked a treat
-----Original Message-----
Steve,
If you are trying to concatenate the contents of a number of
form controls, in this case 8, and the names of those
controls start at Category1 and run through to Category8
then try the following in the form code:

Dim intI as integer, strConCat as String

For intI = 1 to 8
strConCat = strConCat & Me("Category" & intI)
Next intI

************
If you want to add spaces between then:

For intI = 1 to 8
If intI < 8 then
strConCat = strConCat & Me("Category" & intI) & " "
Else
strConCat = strConCat & Me("Category" & intI)
Endif
Next intI

Or you could just concatenate spaces and Trim when loop
finished.

--
Nick Coe (UK)
www.alphacos.co.uk

---

Steve Hughes said:
Hi, I'm trying to refer to fields within a form using a
variable string. I'm trying to create a procedure that
will create an SQL string depending on what option has
been selected in 8 lookup boxs on a form. I don't really
want to use 8 if statements and therefore I am trying to
use For count loop.

I just can't get it to view different fields in the form
at different stages of the loop.

A very simplied version of the code is shown below. It
should just display the contents of each of the 8 boxes.

can anyone please tell me where I'm going wrong?

Dim strAndField as String
Dim inos as Integer

For count = 1 To 8 Step 1
strAndField = "Category" & inos
MsgBox Me![ & StrAndField & ]
Next count

Many Thanks


.
 
Back
Top