Confused about where to put code

L

Lars Brownies

In a module I have a procedure called 'missingdata() that checks if certain
form fields are filled. If not, the user is asked to first enter the fields.
Missingdata() returns true if data is missing. If the fields áre filled the
current record is being saved by:
if . dirty then .dirty = false.

I call this procedure every time I need to make that the record is complete
and saved, for instance for composing an email which uses the field values.
If missingsdata() returns true I quite the calling procedure.

Now in the form's beforeupdate I also call missingdata(), since if a user
moves to another record I need to make sure the required fields are filled.

Since the .dirty part of missingdata() triggers the form's beforeupdate, I'm
in a loop, because the beforeupdate triggers missingdata() again.

Can someone advise me on how to handle this issue?

Thanks,
Lars
 
D

Daryl S

Lars -

You shouldn't have code .dirty = false in the BeforeUpdate event (e.g. not
in your missingdata() procedure). Instead, remove the .dirty = false line in
your missingdata procedure, and where most code calls missingdata(), do this:

If not missingdata() then
.dirty = false
End if

But in the BeforeUpdate event, just call missingdata(), as the .dirty will
be handled by the update event.
 
L

Lars Brownies

your missingdata procedure, and where most code calls missingdata(), do
this:

If not missingdata() then
.dirty = false
End if

Not sure I get this. In that case missingdata() will be called for the
second time, as the .dirty = false line will trigger the beforeupdate which
in turn calls missingdata again. That doesn't seem like proper coding.

Is that what you mean?

Lars
 
J

John W. Vinson

Since the .dirty part of missingdata() triggers the form's beforeupdate, I'm
in a loop, because the beforeupdate triggers missingdata() again.

Can someone advise me on how to handle this issue?

In the BeforeUpdate event, don't mess with the .dirty property at all; just
set Cancel to true with a warning message if the record is incomplete.

If you want to keep the code in missingdata(), pass it an argument to specify
whether or not to save the record.
 
D

Daryl S

Lars -

You are right in that the missingdata() will be called again in this case,
but I left that in place because you indicated there were several places
where you would call the missingdata() from other than moving to the next
record. If you always make sure any saved record passes the missingdata(),
then I would only put the missingdata() on the BeforeUpdate. If however you
have current data that does not pass the missingdata, and that record does
not change, but a user wants to send the data, then you must also test for
missingdata at this point, because the record will not be dirty.

This will not cause a loop, as your prior code would. If my assumptions on
your data are wrong, by all means code to the correct assumptions, and only
put the missingdata() in the BeforeUpdate.
 
L

Lars Brownies

Thanks all for your replies. I'm sorry but I still find this hard to
understand. Maybe I can make myself more clear. I now have:

1. In form's beforeupdate I check for missing data. If true then cancel =
true
2. In a mail-button I save the current record with if me.dirty then me.dirty
= false
3. The dirty = false triggers the form's beforeupdate
4. With missing data in the beforeupdate, this event gets canceled
5. This cancelation causes error 2101 on the earlier mentioned dirty = false
line

Error 2101: The parameter you have given, is invalid for this property.

I could do an extra missingdata check before the dirty = false and stop the
code from there. But, apart from that I don't really like this solution,
this causes another problem: I have several functions that are called from a
menu. When editing a field value and then choosing a menu item the focus
doesn't move off of the active field. The field doesn't hold the new value
yet. I'm forced to first save the record with dirty = false and I can't
first run the missingdata procedure as this may nog give the right result.

Btw: records should be saved without asking the user.

Lars
 
J

John W. Vinson

Thanks all for your replies. I'm sorry but I still find this hard to
understand. Maybe I can make myself more clear. I now have:

1. In form's beforeupdate I check for missing data. If true then cancel =
true
2. In a mail-button I save the current record with if me.dirty then me.dirty
= false
3. The dirty = false triggers the form's beforeupdate
4. With missing data in the beforeupdate, this event gets canceled
5. This cancelation causes error 2101 on the earlier mentioned dirty = false
line

Error 2101: The parameter you have given, is invalid for this property.

Please post your current actual code for the mail button, the BeforeUpdate
event, and the missingdata function.
 
L

Lars Brownies

Simplified but with the same outcome:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(LastName) Or LastName = "" Then
Cancel = True
End If
End Sub


Private Sub btnMail_Click()
On Error GoTo Err_handler

'Make sure all needed values are saved
'On the last part of this line the error occurs
'as the beforeupdate is canceled
If Me.Dirty Then Me.Dirty = False

'other code

Exit_Sub:
Exit Sub
Err_handler:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") "
Resume Exit_Sub
End Sub

I don't use a missingdata function anymore, but that hasn't solved the
problem yet.

Lars
 
L

Lars Brownies

This is the solution I mentioned earlier, but it doesn't work for procedures
called by a menu item. From the earlier post:

"I could do an extra missingdata check before the dirty = false and stop the
code from there. But, apart from that I don't really like this solution,
this causes another problem: I have several functions that are called from a
menu. When editing a field value and then choosing a menu item the focus
doesn't move off of the active field. The field doesn't hold the new value
yet. I'm forced to first save the record with dirty = false and I can't
first run the missingdata procedure as this may nog give the right result."

Lars


KenSheridan via AccessMonster.com said:
You could make the code conditional in the btnMail control's event
procedure:

If Nz(Me.LastName,"") <> "" Then
Me.Dirty = False
'other code
Else
MsgBox "Please insert last name.", vbExclamation, "Invalid Operation"
End If

Ken Sheridan
Stafford, England

Lars said:
Simplified but with the same outcome:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(LastName) Or LastName = "" Then
Cancel = True
End If
End Sub

Private Sub btnMail_Click()
On Error GoTo Err_handler

'Make sure all needed values are saved
'On the last part of this line the error occurs
'as the beforeupdate is canceled
If Me.Dirty Then Me.Dirty = False

'other code

Exit_Sub:
Exit Sub
Err_handler:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") "
Resume Exit_Sub
End Sub

I don't use a missingdata function anymore, but that hasn't solved the
problem yet.

Lars
Thanks all for your replies. I'm sorry but I still find this hard to
understand. Maybe I can make myself more clear. I now have:
[quoted text clipped - 14 lines]
Please post your current actual code for the mail button, the
BeforeUpdate
event, and the missingdata function.
 
L

Lars Brownies

I found the following code by Allen Browne which traps the error and seems
to work in my particular case (activating functions from menu items).

Function CannotSave(frm as Form) As Boolean
On Error GoTo Err_CannotSave
'Purpose: Save the record in the form.
'Return: True if the record cannot be saved.
If frm.Dirty Then
frm.Dirty = False
End If
Exit_CannotSave:
Exit Function
Err_CannotSave:
If Err.Number <> 2101 Then
MsgBox "Error " & Err.Number & " " & Err.Description, _
vbExclamation, "CannotSave"
End If
CannotSave = True
Resume Exit_CannotSave
End Function

Thanks for your input.
Lars


Lars Brownies said:
This is the solution I mentioned earlier, but it doesn't work for
procedures called by a menu item. From the earlier post:

"I could do an extra missingdata check before the dirty = false and stop
the
code from there. But, apart from that I don't really like this solution,
this causes another problem: I have several functions that are called from
a
menu. When editing a field value and then choosing a menu item the focus
doesn't move off of the active field. The field doesn't hold the new value
yet. I'm forced to first save the record with dirty = false and I can't
first run the missingdata procedure as this may nog give the right
result."

Lars


KenSheridan via AccessMonster.com said:
You could make the code conditional in the btnMail control's event
procedure:

If Nz(Me.LastName,"") <> "" Then
Me.Dirty = False
'other code
Else
MsgBox "Please insert last name.", vbExclamation, "Invalid Operation"
End If

Ken Sheridan
Stafford, England

Lars said:
Simplified but with the same outcome:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(LastName) Or LastName = "" Then
Cancel = True
End If
End Sub

Private Sub btnMail_Click()
On Error GoTo Err_handler

'Make sure all needed values are saved
'On the last part of this line the error occurs
'as the beforeupdate is canceled
If Me.Dirty Then Me.Dirty = False

'other code

Exit_Sub:
Exit Sub
Err_handler:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") "
Resume Exit_Sub
End Sub

I don't use a missingdata function anymore, but that hasn't solved the
problem yet.

Lars

Thanks all for your replies. I'm sorry but I still find this hard to
understand. Maybe I can make myself more clear. I now have:
[quoted text clipped - 14 lines]
Please post your current actual code for the mail button, the
BeforeUpdate
event, and the missingdata function.
 

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