Boolean variable not holding state

  • Thread starter Jordan C. Schroeder
  • Start date
J

Jordan C. Schroeder

Hello,

I have a form that calls another sub to validate text boxes. I am having
difficulty with getting my boolean variable to carry back to the original
sub.

Code:

**1st module
Private Sub btnSaveRecord_Click()
On Error GoTo Err_btnSaveRecord_Click

Dim validate As Boolean

validateAccount (validate)

If validate = false then
exit sub
else
docmd.save
end if
end sub

**2nd module
Public Sub validateAccount(validate)
If (Account.Value & vbNullString = vbNullString) Then
MsgBox "Account is required!", vbOKOnly
validate = False
Account.Undo
Else
validate = True
End If
End Sub
 
D

Douglas J. Steele

You've declared validate inside of btnSaveRecord_Click. That means you can
only refer to that variable inside that routine.

To declare a variable that can be used by all routines within the module,
declare the variable at the top of the module before any code.

The fact, though, that you're not getting an error implies that you haven't
told Access to require that all variables be declared. Go into the VB Editor
and select Tools | Options from the menu. Make sure that the "Require
Variable Declaration" box is checked on the Editor tab. If you had done
that, then you'd have quickly realized that your sub validateAccount knew
nothing about the variable validate.
 
G

Graham Mandeno

Hi Jordan

In addition to Doug's good advice on setting the "Require Variable
Declaration" option, please note that this will affect only new modules that
you create, not existing ones.

You should add the following line:
Option Explicit
at the top of every existing module in your database project.
 
D

Douglas J. Steele

Thanks, Graham.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Graham Mandeno said:
Hi Jordan

In addition to Doug's good advice on setting the "Require Variable
Declaration" option, please note that this will affect only new modules
that you create, not existing ones.

You should add the following line:
Option Explicit
at the top of every existing module in your database project.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

Jordan C. Schroeder said:
Hello,

I have a form that calls another sub to validate text boxes. I am having
difficulty with getting my boolean variable to carry back to the original
sub.

Code:

**1st module
Private Sub btnSaveRecord_Click()
On Error GoTo Err_btnSaveRecord_Click

Dim validate As Boolean

validateAccount (validate)

If validate = false then
exit sub
else
docmd.save
end if
end sub

**2nd module
Public Sub validateAccount(validate)
If (Account.Value & vbNullString = vbNullString) Then
MsgBox "Account is required!", vbOKOnly
validate = False
Account.Undo
Else
validate = True
End If
End Sub
 
G

Graham Mandeno

Hi Bob

Yes, you are quite right. I was just adding to what Doug said and didn't
look at the code.

The reason is that putting a value in parentheses causes VB to make a
temporary copy of the value, so even though the argument is implicitly
passed by reference, the value which is changed is the temporary copy.

These are equivalent and will work as expected:
validateAccount validate
and
Call validateAccount (validate)

These are equivalent and will NOT work as expected:
validateAccount (validate)
and
Call validateAccount ((validate))

Personally, Jordan, I would make ValidateAccount a function, returning a
boolean:

Public Function ValidateAccount() As Boolean
If (Account.Value & vbNullString = vbNullString) Then
MsgBox "Account is required!", vbOKOnly
ValidateAccount = False
Account.Undo
Else
ValidateAccount = True
End If
End Sub

Then you don't even need the boolean variable:

If ValidateAccount then
DoCmd.RunCommand acCmdSaveRecord
else
exit sub
end if

Note also that DoCmd.Save does not save changes to the current record, it
saves design changes to the current object (in this case, your form). Use
DoCmd.RunCommand as shown above.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand
 
D

Douglas J. Steele

Bob Quintal said:
Even with Option Explicit the sub does not generate any error.

The reason he's not getting an error is that he's used the
implicitly defined variant validate in the function by treating it
as a parameter.

The actual problem is that he's put validate inside parentheses, and
I don't know why, but that seems to prevent the call from changing
the variable
'This works:
validateAccount validate
'This does not:
validateAccount (validate)

Sorry, you're right Bob. I missed the fact that he was calling the second
sub from the first.

The reason it doesn't work is that putting parentheses around the argument
changes how you pass from the default ByRef to ByVal.

Using

Call validateAccount(validate)

would also work.
 

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