By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,479 Members | 1,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,479 IT Pros & Developers. It's quick & easy.

Conflict in use of OpenArgs and Validation Code

P: 22
am checking for completed fields within a form in the Before Update event. However within the Form Unload event I am also using open args code to close the form and open the previous form. The problem is that when my MsgBox comes up and I check the OK button, the previous form opens!

Can anyone please advise? Thanks in advance

BEFORE UPDATE CODE
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.  
  3. 'Place an asterisk (*) in the Tag Property of the text
  4. 'boxes you wish to validate.
  5. 'Then in the BeforeUpdate Event of the form, copy/paste the following:
  6.  
  7.  
  8.     Dim msg As String, Style As Integer, Title As String
  9.     Dim nl As String, ctl As Control
  10.  
  11.  
  12.     nl = vbNewLine & vbNewLine
  13.  
  14.     For Each ctl In Me.Controls
  15.       If ctl.ControlType = acTextBox Then
  16.         If ctl.Tag = "*" And Trim(ctl & "") = "" Then
  17.          msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
  18.                "You can't save this record until this data is provided!" & nl & _
  19.                "Enter the data and try again . . . "
  20.           Style = vbCritical + vbOKOnly
  21.           Title = "Required Data..."
  22.           MsgBox msg, Style, Title
  23.           ctl.SetFocus
  24.           Cancel = True
  25.           Exit For
  26.         End If
  27.       End If
  28.     Next
  29.  
  30. End Sub
FORM UNLOAD CODE
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Unload(Cancel As Integer)
  2.     On Error Resume Next
  3.      Forms(Me.OpenArgs).Visible = True
  4.  
  5.     Forms!Project_Edit.cboCompanyID.Requery
  6. End Sub
Oct 3 '08 #1
Share this Question
Share on Google+
3 Replies


FishVal
Expert 2.5K+
P: 2,653
... The problem is that when my MsgBox comes up and I check the OK button, the previous form opens! ....
What did you do before it? Closed the form?
If so then work up your Form_Unload handler. It doesn't distinguish whether data validation passed or failed.

Regards,
Fish
Oct 4 '08 #2

missinglinq
Expert 2.5K+
P: 3,532
Try removing the

Exit For

line from your code. The first time a validation fails and you click on the OK in your warning messagebox, I think this line is causing you to drop out of your validation routine, whether the fields are populated or not, and Access then moves on to your Form_Unload event.

Linq ;0)>
Oct 4 '08 #3

NeoPa
Expert Mod 15k+
P: 31,709
I don't see why this would happen at all I'm afraid.

I don't agree with Linq on this occasion (quite rare occurrence). The Exit For is exactly right it seems to me. Why it reverts to your other form doesn't seem to be explained by the code shown.

The OpenArgs reference in the Form_Unload procedure may be a problem. My experience has always been that it (Me.OpenArgs) is only available to the called object (form or report) for the first line of code (in Form_Open). After this point it is reset and shows nothing. To get around this it is necessary to save it to a variable whose scope is such that it is available when needed. To be available in a separate procedure it should be an object level variable.
Oct 5 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.