Correct code for Looping?

  • Thread starter Thread starter DJW
  • Start date Start date
D

DJW

I have the following code. I would like to write code to loop through Step1
until the conditions have been met. Once those condition have been met I
would like to do the same for Step2 if the code passes on to that level
instead of going directly to Step3. How do I code this to make it happen?



Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.Dirty Then 'Checks if any changes were made.
GoTo Question
Else
'Complete original action.
End If

Question:
Message = "Changes made! Do you wish to save?" 'Gives option to save
changes.
If MsgBox(Message, vbQuestion + vbYesNo, "Save Record?") = vbYes Then
GoTo Step1
Else
DoCmd.RunCommand acCmdUndo 'Undoes any changes.
End If

Step1: 'Checks to make sure all required fields contain
information.
If IsNull(Me![Date Submitted]) Then
MsgBox "When was the Item Submitted?"
Me![Date Submitted].SetFocus
ElseIf IsNull(Me![Location Stored]) Then
MsgBox "Where is the Item being Stored?"
Me![Location Stored].SetFocus
ElseIf IsNull(Me![Item Description]) Then
MsgBox "Describe the Item!"
Me![Item Description].SetFocus
ElseIf Not IsNull(Me![Removal Date]) Then
GoTo Step2
Else
GoTo Step3

Step2:
If IsNull(Me![Requested By]) Then
MsgBox "Who requested this item?"
Me![Requested By].SetFocus
ElseIf IsNull(Me![Recieved By]) Then
MsgBox "Who Recieved the Item?"
Me![Recieved By].SetFocus
Me![Record Updated By] = LoginNameField 'Enters variable value
of LoginNameField.
Else
GoTo Step3

Step3:
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
'Saves record.

Exit Sub

End If
End If
 
What you really should be doing is making these checks during data entry in
the Before Update event of each control. The Form Before Update event is
best used for validating data where more than one control is involved. For
example, if one control's value is determined by the value in another
control. If you move your code to the individual controls, you will find it
easier to manage. For example:

Private Sub Date Submitted_BeforeUpdate(Cancel As Integer)

If IsNull(Me![Date Submitted]) Then
MsgBox "When was the Item Submitted?"
Cancel = True
End If

Note that in the above context, Cancel = True leaves Date Submitted the
active control and prevents the user from moving to the next control until
they either enter a value or cancel the record.
 

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

Back
Top