Repeating code - validation -

G

Guest

Hello -

I have the following type of code that checks controls to see that they're
filled in. I need to use it in several places. I need to put it into a Sub
that can just be called.
The problem is when I do that, it exits out of it's own sub if something
isn't filled in, but continues doing whatever is in the main sub it's in.

<snip>
txtCallerName.SetFocus
If txtCallerName.Text = "" Then
MsgBox "Please fill in the Caller Name!"
Exit Sub
End If

txtPhone.SetFocus
If txtPhone.Text = "" Then
MsgBox "Please fill in the phone number!"
Exit Sub
End If
<snip>

What's the best way to handle a situation like this?

Any help will be greatly appreciated!
 
S

Sandra Daigle

Sandy,

I see several problems here - the first is that you are using the Text
property of each control. Apparently you've discovered that the text
property is only available when the control has focus but this is for a
reason. The text property holds whatever is currently typed into the control
before it it is saved as the control's value. The Value property is the one
you need. Since the value property is the default property of data controls
you don't even have to specify it. So, you can eliminate the Setfocus
commands and modify your comparisons to either of the following:

if txtCallerName=" " Then

or

if txtCallerName.value=" " Then

Next problem - most programmers would consider the 'Exit Sub' type of
branching to be bad form. It is only slightly better than using a goto
statement. The only time most programmers use either the Exit Sub or a Goto
statement is to implement error handling. Instead, we go with structured
programming - you build an if-then-elseif structure that visually and
logically structures the code. Now to get the results of this procedure to
pass back some meaningful information to the calling procedure I would turn
it into a function that returns a boolean result.


Function IsDataValid() as boolean
dim fDataValid as boolean
'initial value of above variable is false.
'Value is true if all of the validations are ok

If txtCallerName = "" Then
MsgBox "Please fill in the Caller Name!"
Elseif txtPhone.Text = "" Then
MsgBox "Please fill in the phone number!"
else
fDataValid=true
End If
IsDataValid=fDataValid

End Function

Then in the calling code:

if DataValid() then
'continue
else
'what do you want to happen?
endif

Where are you calling your code?
 
G

Guest

Change it to a function and have it return a value. Then call the function
and determine what to do next based on the return from the function.
 
B

Bob Hairgrove

Hello -

I have the following type of code that checks controls to see that they're
filled in. I need to use it in several places. I need to put it into a Sub
that can just be called.
The problem is when I do that, it exits out of it's own sub if something
isn't filled in, but continues doing whatever is in the main sub it's in.

<snip>
txtCallerName.SetFocus
If txtCallerName.Text = "" Then
MsgBox "Please fill in the Caller Name!"
Exit Sub
End If

Change this to:

If IsNull(txtCallerName) Then
MsgBox "Please fill in the Caller Name!"
Exit Sub
End If

etc. No need to set focus just to check the value of a text box.
<snip>

What's the best way to handle a situation like this?

Any help will be greatly appreciated!

The best way is to validate input in the BeforeUpdate event of the
form -- that is, if you are using a bound form. If any required data
is missing, set Cancel=True and exit the subroutine.

If you are using an unbound form, you will probably have a command
button to update or insert the data. Except for the "Cancel=True"
part, you can run the code in the click event procedure for the
button.
 
G

Guest

Hi Sandy -

Thanks so much for your response. I can't get your code to work. What am I
doing wrong? I have the following:

Function IsDataValid() As Boolean

Dim fDataValid As Boolean

If txtCallerName = "" Then
MsgBox "Please fill in the Caller Name!"
ElseIf cboState.Value = "" Then
MsgBox "Please select a State!"
ElseIf cboDeclarationNo.Value = "" Then
MsgBox "Please select a Declaration Number!"
ElseIf cboCallTypeID.Value = "" Then
MsgBox "Please select a Call Type!"
ElseIf cboCallTypeID.Value = "Application Issued" Then
If txtAppAmt = "" Then
MsgBox "Please fill in the number of applications!"
End If
ElseIf IsNull(OptLoanType) Then
MsgBox "Please select a Loan Type!"
Else
fDataValid = True
End If
IsDataValid = fDataValid
End Function

In a cmdSave button I have:

If IsDataValid() Then
DoCmd.GoToRecord , , acNewRec

End If

The only thing the above catches is OptLoanType.

Again, any help is greatly appreciated!
--
Sandy


Sandra Daigle said:
Sandy,

I see several problems here - the first is that you are using the Text
property of each control. Apparently you've discovered that the text
property is only available when the control has focus but this is for a
reason. The text property holds whatever is currently typed into the control
before it it is saved as the control's value. The Value property is the one
you need. Since the value property is the default property of data controls
you don't even have to specify it. So, you can eliminate the Setfocus
commands and modify your comparisons to either of the following:

if txtCallerName=" " Then

or

if txtCallerName.value=" " Then

Next problem - most programmers would consider the 'Exit Sub' type of
branching to be bad form. It is only slightly better than using a goto
statement. The only time most programmers use either the Exit Sub or a Goto
statement is to implement error handling. Instead, we go with structured
programming - you build an if-then-elseif structure that visually and
logically structures the code. Now to get the results of this procedure to
pass back some meaningful information to the calling procedure I would turn
it into a function that returns a boolean result.


Function IsDataValid() as boolean
dim fDataValid as boolean
'initial value of above variable is false.
'Value is true if all of the validations are ok

If txtCallerName = "" Then
MsgBox "Please fill in the Caller Name!"
Elseif txtPhone.Text = "" Then
MsgBox "Please fill in the phone number!"
else
fDataValid=true
End If
IsDataValid=fDataValid

End Function

Then in the calling code:

if DataValid() then
'continue
else
'what do you want to happen?
endif

Where are you calling your code?


--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.

Hello -

I have the following type of code that checks controls to see that
they're filled in. I need to use it in several places. I need to
put it into a Sub that can just be called.
The problem is when I do that, it exits out of it's own sub if
something isn't filled in, but continues doing whatever is in the
main sub it's in.

<snip>
txtCallerName.SetFocus
If txtCallerName.Text = "" Then
MsgBox "Please fill in the Caller Name!"
Exit Sub
End If

txtPhone.SetFocus
If txtPhone.Text = "" Then
MsgBox "Please fill in the phone number!"
Exit Sub
End If
<snip>

What's the best way to handle a situation like this?

Any help will be greatly appreciated!
 
S

Sandra Daigle

The code is only going to catch the first error condition found in the
sequence (you probably already gathered that but just in case . . .)

You probably need to use the IsNull function on all fields unless you really
expect zero-length strings in some of them. There is a difference between
the two but for most applications, Zero Length Strings should be disallowed
in the field defination of the table. So try this:

If isnull(txtCallerName) Then
MsgBox "Please fill in the Caller Name!"
ElseIf isnull(cboState.Value) Then
MsgBox "Please select a State!"
etc.

If you really need to allow ZeroLength Strings then you can modify your
tests to this:

if len(txtCallerName & "")=0 then
MsgBox "Please fill in the Caller Name!"
etc.

This will catch either a zero-length string or a null.

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.

Hi Sandy -

Thanks so much for your response. I can't get your code to work.
What am I doing wrong? I have the following:

Function IsDataValid() As Boolean

Dim fDataValid As Boolean

If txtCallerName = "" Then
MsgBox "Please fill in the Caller Name!"
ElseIf cboState.Value = "" Then
MsgBox "Please select a State!"
ElseIf cboDeclarationNo.Value = "" Then
MsgBox "Please select a Declaration Number!"
ElseIf cboCallTypeID.Value = "" Then
MsgBox "Please select a Call Type!"
ElseIf cboCallTypeID.Value = "Application Issued" Then
If txtAppAmt = "" Then
MsgBox "Please fill in the number of applications!"
End If
ElseIf IsNull(OptLoanType) Then
MsgBox "Please select a Loan Type!"
Else
fDataValid = True
End If
IsDataValid = fDataValid
End Function

In a cmdSave button I have:

If IsDataValid() Then
DoCmd.GoToRecord , , acNewRec

End If

The only thing the above catches is OptLoanType.

Again, any help is greatly appreciated!
Sandy,

I see several problems here - the first is that you are using the
Text property of each control. Apparently you've discovered that the
text property is only available when the control has focus but this
is for a reason. The text property holds whatever is currently typed
into the control before it it is saved as the control's value. The
Value property is the one you need. Since the value property is the
default property of data controls you don't even have to specify it.
So, you can eliminate the Setfocus commands and modify your
comparisons to either of the following:

if txtCallerName=" " Then

or

if txtCallerName.value=" " Then

Next problem - most programmers would consider the 'Exit Sub' type of
branching to be bad form. It is only slightly better than using a
goto statement. The only time most programmers use either the Exit
Sub or a Goto statement is to implement error handling. Instead, we
go with structured programming - you build an if-then-elseif
structure that visually and logically structures the code. Now to
get the results of this procedure to pass back some meaningful
information to the calling procedure I would turn it into a function
that returns a boolean result.


Function IsDataValid() as boolean
dim fDataValid as boolean
'initial value of above variable is false.
'Value is true if all of the validations are ok

If txtCallerName = "" Then
MsgBox "Please fill in the Caller Name!"
Elseif txtPhone.Text = "" Then
MsgBox "Please fill in the phone number!"
else
fDataValid=true
End If
IsDataValid=fDataValid

End Function

Then in the calling code:

if DataValid() then
'continue
else
'what do you want to happen?
endif

Where are you calling your code?


--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.

Hello -

I have the following type of code that checks controls to see that
they're filled in. I need to use it in several places. I need to
put it into a Sub that can just be called.
The problem is when I do that, it exits out of it's own sub if
something isn't filled in, but continues doing whatever is in the
main sub it's in.

<snip>
txtCallerName.SetFocus
If txtCallerName.Text = "" Then
MsgBox "Please fill in the Caller Name!"
Exit Sub
End If

txtPhone.SetFocus
If txtPhone.Text = "" Then
MsgBox "Please fill in the phone number!"
Exit Sub
End If
<snip>

What's the best way to handle a situation like this?

Any help will be greatly appreciated!
 
G

Guest

Hi Sandra -

Thank you very much for your response.

IsNull works, but I am still faced with the problem of every msgbox being
triggered after a cbo or txt that is null. The only way out I can see is
with an archaic Exit Function after each msgbox.

Any thoughts on it?
--
Sandy


Sandra Daigle said:
The code is only going to catch the first error condition found in the
sequence (you probably already gathered that but just in case . . .)

You probably need to use the IsNull function on all fields unless you really
expect zero-length strings in some of them. There is a difference between
the two but for most applications, Zero Length Strings should be disallowed
in the field defination of the table. So try this:

If isnull(txtCallerName) Then
MsgBox "Please fill in the Caller Name!"
ElseIf isnull(cboState.Value) Then
MsgBox "Please select a State!"
etc.

If you really need to allow ZeroLength Strings then you can modify your
tests to this:

if len(txtCallerName & "")=0 then
MsgBox "Please fill in the Caller Name!"
etc.

This will catch either a zero-length string or a null.

--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.

Hi Sandy -

Thanks so much for your response. I can't get your code to work.
What am I doing wrong? I have the following:

Function IsDataValid() As Boolean

Dim fDataValid As Boolean

If txtCallerName = "" Then
MsgBox "Please fill in the Caller Name!"
ElseIf cboState.Value = "" Then
MsgBox "Please select a State!"
ElseIf cboDeclarationNo.Value = "" Then
MsgBox "Please select a Declaration Number!"
ElseIf cboCallTypeID.Value = "" Then
MsgBox "Please select a Call Type!"
ElseIf cboCallTypeID.Value = "Application Issued" Then
If txtAppAmt = "" Then
MsgBox "Please fill in the number of applications!"
End If
ElseIf IsNull(OptLoanType) Then
MsgBox "Please select a Loan Type!"
Else
fDataValid = True
End If
IsDataValid = fDataValid
End Function

In a cmdSave button I have:

If IsDataValid() Then
DoCmd.GoToRecord , , acNewRec

End If

The only thing the above catches is OptLoanType.

Again, any help is greatly appreciated!
Sandy,

I see several problems here - the first is that you are using the
Text property of each control. Apparently you've discovered that the
text property is only available when the control has focus but this
is for a reason. The text property holds whatever is currently typed
into the control before it it is saved as the control's value. The
Value property is the one you need. Since the value property is the
default property of data controls you don't even have to specify it.
So, you can eliminate the Setfocus commands and modify your
comparisons to either of the following:

if txtCallerName=" " Then

or

if txtCallerName.value=" " Then

Next problem - most programmers would consider the 'Exit Sub' type of
branching to be bad form. It is only slightly better than using a
goto statement. The only time most programmers use either the Exit
Sub or a Goto statement is to implement error handling. Instead, we
go with structured programming - you build an if-then-elseif
structure that visually and logically structures the code. Now to
get the results of this procedure to pass back some meaningful
information to the calling procedure I would turn it into a function
that returns a boolean result.


Function IsDataValid() as boolean
dim fDataValid as boolean
'initial value of above variable is false.
'Value is true if all of the validations are ok

If txtCallerName = "" Then
MsgBox "Please fill in the Caller Name!"
Elseif txtPhone.Text = "" Then
MsgBox "Please fill in the phone number!"
else
fDataValid=true
End If
IsDataValid=fDataValid

End Function

Then in the calling code:

if DataValid() then
'continue
else
'what do you want to happen?
endif

Where are you calling your code?


--
Sandra Daigle [Microsoft Access MVP]
Please post all replies to the newsgroup.


Sandy wrote:
Hello -

I have the following type of code that checks controls to see that
they're filled in. I need to use it in several places. I need to
put it into a Sub that can just be called.
The problem is when I do that, it exits out of it's own sub if
something isn't filled in, but continues doing whatever is in the
main sub it's in.

<snip>
txtCallerName.SetFocus
If txtCallerName.Text = "" Then
MsgBox "Please fill in the Caller Name!"
Exit Sub
End If

txtPhone.SetFocus
If txtPhone.Text = "" Then
MsgBox "Please fill in the phone number!"
Exit Sub
End If
<snip>

What's the best way to handle a situation like this?

Any help will be greatly appreciated!
 
S

Sandra Daigle

I don't see why every msgbox would be triggered. Using the if..then..elseif
statement only one branch should be executed each time you call the
function. If the first condition is true you would get the first msgbox and
control would fall down to the endif statement. If the first is false (ie
txtCallerName is not null) then the second condition is evaluated and so on
until one of the conditions is met or the else logic is executed setting the
flag to true.
 

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