Function works, but I'm not sure why. (1 Viewer)

JCross

Registered User.
Local time
Today, 03:02
Joined
Feb 28, 2002
Messages
116
Morning all,

I have a function that validates some info, and returns a true or false depending on whther the info was good. If true, the form saves, if false it doesn't. This is the call to the function - which works but is ugly as hell and never seems to get to the "Call" part. Is that line superfluous after the x= line?

I guess I shouldn't worry since it works, but I'm not good at passing things back and forth and I'd like to understand a little better.

'validates, and saves the form if ok.
Dim x As Byte
x = PrepSaveValid(formname)

If x = False Then
Exit Sub
Else
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
End If

Call PrepSaveValid("frmPrepItem")


Thank you!


Jennifer
 

doulostheou

Registered User.
Local time
Yesterday, 21:02
Joined
Feb 8, 2002
Messages
314
Is this the function or is the function stored in a module elsewhere? If so, what does the function look like?
 

JCross

Registered User.
Local time
Today, 03:02
Joined
Feb 28, 2002
Messages
116
This is the call to the function:

Call PrepSaveValid("frmPrepItem")

The funtion itself looks like this (and is stored in a General module.

Public Function PrepSaveValid(formname As String) As Boolean

'validation for all prep items on save
Dim f As Form
Set f = Forms(formname)
Dim blnSuccess As Boolean

On Error Resume Next

'validation before saving
If IsNull(f.ItemNumber.Value) Then
MsgBox "Please enter an Item Number"
f.ItemNumber.SetFocus
blnSuccess = False
Exit Function
ElseIf IsNull(f.Description.Value) Then
MsgBox "Please enter a description."
f.Description.SetFocus
blnSuccess = False
Exit Function
ElseIf IsNull(f.BatchSize.Value) Or f.BatchSize.Value < 0 Then
MsgBox "Please enter a Batch Size greater than 0."
f.BatchSize.SetFocus
blnSuccess = False
Exit Function
ElseIf IsNull(f.RecipeUofM.Value) Then
MsgBox "Please enter the Recipe Unit of Measure."
f.RecipeUofM.SetFocus
blnSuccess = False
Exit Function
Else
blnSuccess = True

End If

PrepSaveValid = blnSuccess

End Function


Like I said - it seems to work just fine, but I think parts of it may be superfluous?

Jennifer
 

doulostheou

Registered User.
Local time
Yesterday, 21:02
Joined
Feb 8, 2002
Messages
314
I don't understand the part where you are setting x = to the function. What I describe below would only save a couple lines of space, but it makes more sense to me.

It seems to me that this line should be removed from the function:

Dim blnSuccess As Boolean

And then it should be placed in the module as a Global Variable:

Public blnSuccess as Boolean

Then you can call the function and use blnSuccess to determine wether or not to save the form:

Call PrepSaveValid("frmPrepItem")
If blnSuccess = False Then
Exit Sub
Else
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
End If
 

JCross

Registered User.
Local time
Today, 03:02
Joined
Feb 28, 2002
Messages
116
Thank you! I knew that I had some unnecessary lines in there, I just couldn't figure out what they were!


Jennifer
 

Users who are viewing this thread

Top Bottom