trying to do an undo in a public function not working

K

Keith G Hicks

I'm putting the following function in the "Before Update" event of a date
control on a form like this:

=fncTestDateLimit()

Here's the function (it's in a regular module, not a form module):

Public Function fncTestDateLimit()
If Screen.ActiveControl < dteLowDateLimit Or dteDatePassed >
dteHighDateLimit Then
If MsgBox("Date entered is outside recommended bounds. Continue?",
vbYesNo + vbQuestion) = vbNo Then
Screen.ActiveControl.Undo
End If
End If
End Function

Where dteLowDateLimit and dteHighDateLimit are public variables set to
certain dates when the program starts.

The function is called just fine and there are no compile errors. But it
doesn't undo the value if the line "Screen.ActiveControl.Undo" runs.

Can anyone help me out here? Undo seems to be very persnikity. I always seem
to have trouble with it.

Thanks,

Keith
 
K

Keith G Hicks

Sorry, I just made a correction to the code below. This was not the problem,
I just forgot to change it for the post (after I changed it in my original
code).

I'm putting the following function in the "Before Update" event of a date
control on a form like this:

=fncTestDateLimit()

Here's the function (it's in a regular module, not a form module):

Public Function fncTestDateLimit()
If Screen.ActiveControl < dteLowDateLimit Or Screen.ActiveControl >
dteHighDateLimit Then
If MsgBox("Date entered is outside recommended bounds. Continue?",
vbYesNo + vbQuestion) = vbNo Then
Screen.ActiveControl.Undo
End If
End If
End Function

Where dteLowDateLimit and dteHighDateLimit are public variables set to
certain dates when the program starts.

The function is called just fine and there are no compile errors. But it
doesn't undo the value if the line "Screen.ActiveControl.Undo" runs.

Can anyone help me out here? Undo seems to be very persnikity. I always seem
to have trouble with it.

Thanks,

Keith
 
K

Ken Snell [MVP]

Undo will "work" only if the control is bound to a field in the form's
recordsource.
 
K

Keith G Hicks

I am aware of that. The field that I'm working with is in fact bound to a
field in the form's source.

Undo will "work" only if the control is bound to a field in the form's
recordsource.
 
K

Ken Snell [MVP]

Does that control have an Undo property? I note that your code is written as
generic function, but not every control supports Undo.

What type of control is giving you problems?
 
K

Keith G Hicks

Yes. It works if I just run the same code in the Before Update event "code".
It's just a text box control bound to a date field. The UNDO works just fine
if I run it from the control itself. But it doesn't work from the generic
function as you can see.

Does that control have an Undo property? I note that your code is written as
generic function, but not every control supports Undo.

What type of control is giving you problems?
 
R

Rick Brandt

Keith said:
Yes. It works if I just run the same code in the Before Update event
"code". It's just a text box control bound to a date field. The UNDO
works just fine if I run it from the control itself. But it doesn't
work from the generic function as you can see.

It might be a scope problem. I would rewrite the function to return a
boolean and then in your BeforeUpdate event you can use...

If YourFunctionName() = True Then Me.Undo

(or similar)
 
K

Keith G Hicks

Yep. That's my original code. I have a public function that returns true or
false and if it's false in the calling routine then I do
me.<controlname>.undo. That works just fine. Like I said earlier, what I'm
trying to do is to use a function in the property setting of the control to
make it more generic. That way I can just paste the function into the
BeforeUpdate event of every control on the form and not have to deal with
code at all for that feature. It seems silly to write Before Update code for
50 date controls on a form. It makes more sense to select all 50 controls
and paste the function into all of their BeforeUpdate event properties all
at once.

Obviously this is a problem with MS Access that nobody can solve. I'll just
have to surrrender to a code event for every control. I know it's not hard
but it just seems to be a waste of time.

I usually don't like to handle things this way since debugging functions
called that way can be about as much of a pain as the old macros. It just
seemed to make sense here.

Thanks to you all for the effort.

Keith said:
Yes. It works if I just run the same code in the Before Update event
"code". It's just a text box control bound to a date field. The UNDO
works just fine if I run it from the control itself. But it doesn't
work from the generic function as you can see.

It might be a scope problem. I would rewrite the function to return a
boolean and then in your BeforeUpdate event you can use...

If YourFunctionName() = True Then Me.Undo

(or similar)
 
D

Dirk Goldgar

Keith G Hicks said:
Yep. That's my original code. I have a public function that returns
true or false and if it's false in the calling routine then I do
me.<controlname>.undo. That works just fine. Like I said earlier,
what I'm trying to do is to use a function in the property setting of
the control to make it more generic. That way I can just paste the
function into the BeforeUpdate event of every control on the form and
not have to deal with code at all for that feature. It seems silly to
write Before Update code for 50 date controls on a form. It makes
more sense to select all 50 controls and paste the function into all
of their BeforeUpdate event properties all at once.

Obviously this is a problem with MS Access that nobody can solve.
I'll just have to surrrender to a code event for every control. I
know it's not hard but it just seems to be a waste of time.

I usually don't like to handle things this way since debugging
functions called that way can be about as much of a pain as the old
macros. It just seemed to make sense here.

Thanks to you all for the effort.

What's wrong with this?

'---- start of revised code ----
Public Function fncTestDateLimit()

If Screen.ActiveControl < dteLowDateLimit _
Or Screen.ActiveControl > dteHighDateLimit _
Then
If MsgBox( _
"Date entered is outside recommended bounds.
Continue?", _
vbYesNo + vbQuestion) _
= vbNo _
Then
DoCmd.CancelEvent
Screen.ActiveControl.Undo
End If
End If

End Function
'---- end of revised code ----

It seems to me that it would do what you want.
 

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