Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update function problem

Status
Not open for further replies.

SCubed

Programmer
Jan 6, 2004
26
US
I have created a user function that has all my save logic in it, including some validation. I call the function from various points in my code: Save button, Close button, Exit Application button.

My problem is if the validation fails, I need to stop the event which called this function. The DoCmd.CancelEvent does not work from the function. I cannot figure out how to pass a value back from my function to the event in order to check it and do the CancelEvent logic from there.

Any help?!?

Thanks in advance!

SCubed
 
so basically an event fires, eg. close, whatever, and from there you call your save function? withing the save function if validation failed you could just have an exit function call, or better yet, have your save code within an if statement which only runs if validation passes.
 
One way to do this as I have a custom modulated Validation code so as I can allow users to click on command buttons without necessarily being forced to run data validation checks on the various controls (such as backing out or resetting of a form, or getting help on something). Each control instead has a function by the name of <ControlName>_Validate(), if this has been setup, which then it passes back to the central ValidateData code either it passes or fails. If it fails, it returns the focus back to the control else it moves on.

Each individual Validate Code returns either a -1 or a 0 value. By the central code having something like:

Result = CallByName(frmValForm, objValCtl.Name & &quot;_Validate&quot;, VbMethod)
If err.Number <> 0 Then
ValidateData = True
Exit Function
ElseIf Result = 0 Then
bolRet = True
ValidateData = 0
objValCtl.SetFocus
Else
ValidateData = -1
End If

Please note, this would only be used in a FE/BE type system else this would not work within Access, and this only works on UNBOUND forms.

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
When the going gets tough, the tough gets going.
 
if the function completes successfully, make it return true else false

then call the function in an if statement.

ex:

if function() = true
(continue)
else
exit sub
end if
 
AmritPaulSingh,

Don't forget to put the 'Then' part at the end on the same line as the If line.

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
When the going gets tough, the tough gets going.
 
OK, let me clarify a little bit. My function only saves if validation passes. The problem is, how do I send back a value to let the Close Event or Exit Event know to NOT continue? I've tried &quot;Return -1&quot; and others, but the syntax is not correct. If I cannot return a value, then my window or app will close instead of letting the user correct the problem.

Thanks again.

SCubed
 
well, if your running a function it should look like the following

function blah(sInformation as string) as boolean
bunch of code
end function

in the bunch of code anywhere you can do this

blah = true/false

then when the function completes that value is returned to the calling procedure. You should be able to then say in the calling procedure &quot;the function returned false, so cancel = true&quot;
 
Thank you JimbOne! That's what I was missing! I didn't have the &quot;As Boolean&quot; after my function name. Then I didn't realize it was as simple as setting my functionname = T/F. You made my day!! :)

Thanks!

SCubed
 
Where I was coming in with the 0 and -1 is cause in VBA False is the value of 0, and True is the value of -1. For If, Then, Else statements, if a value is 0, Null, or any part of the criteria expression returns a Null value not wrapped within the IsNull function, it's considered as False while any other value is considered as True.

Ronald R. Dodge, Jr.
Production Statistician
Master MOUS 2000
When the going gets tough, the tough gets going.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top