Loop with form

M

Martin

I have a form containing a label and an unbound control. I want to put the
form in a loop that will allow the label box to change values and allow the
user to enter a response in the control. I have the following code created:

DoCmd.OpenForm "Data Entry 2", , acNormal
For N = 0 To UBound(DeptArray)
Forms![Data Entry 2].[LabelText].Caption = DeptArray(N, 1)
Forms![Data Entry 2].Refresh
Forms![Data Entry 2].[RespAnswer].SetFocus
Next N

This code works except for the fact that the process does not stop to allow
the user to provide a response for each pass thru the loop. Only the last
pass thru the loop is displayed to the user.

Can someone tell me what I am missing?

Thanks.
 
M

Marshall Barton

Martin said:
I have a form containing a label and an unbound control. I want to put the
form in a loop that will allow the label box to change values and allow the
user to enter a response in the control. I have the following code created:

DoCmd.OpenForm "Data Entry 2", , acNormal
For N = 0 To UBound(DeptArray)
Forms![Data Entry 2].[LabelText].Caption = DeptArray(N, 1)
Forms![Data Entry 2].Refresh
Forms![Data Entry 2].[RespAnswer].SetFocus
Next N

This code works except for the fact that the process does not stop to allow
the user to provide a response for each pass thru the loop. Only the last
pass thru the loop is displayed to the user.


The only place that can tell if the user has entered
something is either the response text box's AfterUpdate
event or a command button's click event (that users click to
indicate they are done). You need to move that code to the
other form in either of of those events.

You will need a little more code to save the data and to
move to a new record. Depending on the details of whatever
you are trying to do, the code might liik something like:

Dim N As Integer

Private Form_Load(...
N = 0
Me.LabelText.Caption = DeptArray(N, 1)
RespAnswer.SetFocus
End Sub

Private Sub RespAnswer_AfterUpdate()
Me.Dirty = False 'save record
DoCmd.GoToRecord acDataForm, "Data Entry 2", acNewRec
N = N + 1
Me.LabelText.Caption = DeptArray(N, 1)
End Sub

I suspect that you should be using a text box instead of a
label to display the arrary value. Otherwise, the value
will not be saved back to the form's table along with the
response.
 
M

Martin

Thank you for the help. I thought I was missing something, but couldn't put
my fingers on it.

Marshall Barton said:
Martin said:
I have a form containing a label and an unbound control. I want to put the
form in a loop that will allow the label box to change values and allow the
user to enter a response in the control. I have the following code created:

DoCmd.OpenForm "Data Entry 2", , acNormal
For N = 0 To UBound(DeptArray)
Forms![Data Entry 2].[LabelText].Caption = DeptArray(N, 1)
Forms![Data Entry 2].Refresh
Forms![Data Entry 2].[RespAnswer].SetFocus
Next N

This code works except for the fact that the process does not stop to allow
the user to provide a response for each pass thru the loop. Only the last
pass thru the loop is displayed to the user.


The only place that can tell if the user has entered
something is either the response text box's AfterUpdate
event or a command button's click event (that users click to
indicate they are done). You need to move that code to the
other form in either of of those events.

You will need a little more code to save the data and to
move to a new record. Depending on the details of whatever
you are trying to do, the code might liik something like:

Dim N As Integer

Private Form_Load(...
N = 0
Me.LabelText.Caption = DeptArray(N, 1)
RespAnswer.SetFocus
End Sub

Private Sub RespAnswer_AfterUpdate()
Me.Dirty = False 'save record
DoCmd.GoToRecord acDataForm, "Data Entry 2", acNewRec
N = N + 1
Me.LabelText.Caption = DeptArray(N, 1)
End Sub

I suspect that you should be using a text box instead of a
label to display the arrary value. Otherwise, the value
will not be saved back to the form's table along with the
response.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top