Loop through fields on form, simply with values from array

  • Thread starter Thread starter BlueWolverine
  • Start date Start date
B

BlueWolverine

Hello,
MS ACCESS 2003 on XP PRO.

I asked earlier about how to loop through all the controls on a form.

I now have a slightly different question.


I have an array with values loaded into it. Col1 is full of fieldNames, and
Col2 is full of fieldValues. call the array a_Fields(1 to x,1 to 2) as
string

I want my loop to fill these fields to look something like this

lcv=1

while lcv<=RECORDCOUNT

Forms!MyForm!a_Fields(lcv,1).value =a_Fields(lcv,2)

lcv=lcv+1
wend

This doesn't work, but I think you see what I'm trying to do.

I want to avoid looping within loop, and I'd rather not write queries every
time to limit my selection to 1 record within a loop.

Is there anyway to do what I'm looking for?

Thanks
 
BlueWolverine said:
Hello,
MS ACCESS 2003 on XP PRO.

I asked earlier about how to loop through all the controls on a form.

I now have a slightly different question.


I have an array with values loaded into it. Col1 is full of fieldNames,
and
Col2 is full of fieldValues. call the array a_Fields(1 to x,1 to 2) as
string

I want my loop to fill these fields to look something like this

lcv=1

while lcv<=RECORDCOUNT

Forms!MyForm!a_Fields(lcv,1).value =a_Fields(lcv,2)

lcv=lcv+1
wend

This doesn't work, but I think you see what I'm trying to do.

I want to avoid looping within loop, and I'd rather not write queries
every
time to limit my selection to 1 record within a loop.


You say "RECORDCOUNT" and talk about "records", but there's nothing in what
you've posted that talks about more than one record. The code I posted for
you to load the array dealt only with the current record on the form. Do
you meant "field count" and "fields"? I'm going to assume that you do.

To do this, you're going to need to know the number of fields you loaded
into the array; that is, the highest used value of lcv. I'll assume that
you saved this value when you loaded it, in a variable called lcv_max. Then
you could use code like this:

'----- start of example code -----
With Forms!MyForm

For lcv = 1 To lcv_max
.Controls(a_Fields(lcv, 1)) = a_Fields(lcv, 2)
Next lcv

End With
'----- end of example code -----

May I ask, what are you trying to do with this code, in a larger sense?
There may be a more efficient way of doing it.
 
Now that you mention it, there probably is.

I have a filter panel, and I want it to default to the last values used, so
that if I tweak a filter, I don't have to re-enter the whole thing.
 
BlueWolverine said:
Now that you mention it, there probably is.

I have a filter panel, and I want it to default to the last values used,
so
that if I tweak a filter, I don't have to re-enter the whole thing.


I don't think I have the fill picture, but if it's a matter of default
values, you might set each control's DefaultValue property instead of
loading and unloading an array. For example:

'----- start of speculative example code -----
Dim ctl As Access.Control
Const Q As String = """"

For Each ctl In Forms!MyForm.Controls

If ctl.ControlType = acTextBox _
Or ctl.ControlType = acComboBox _
Then
ctl.DefaultValue = Q & ctl.Value & Q
End If

Next ctl
'----- end of code -----

You would have to run this code in the appropriate event, of course.
 
Back
Top