Code not giving the correct results

B

Bob07790

Hi
The following code runs but sometimes displaying the label when it shouldn’t.
If the Me.Fault result isn’t YES I sometimes get the label displaying. I
also get the same sometimes with the Me.Recoverable. If the result is
recoverable instead of not recoverable it sometimes displays the label. How
can I improve this code so that I get accurate results?

Thanks Bob


Private Sub Form_Current()

If Me.Fault = "YES" And Me.Recoverable = "NOT RECOVERABLE" And Not
IsNull(Me.NameOfOtherParty) And Me.InsuranceCompany = "BR" Or
Me.InsuranceCompany = "Br Insurance Company Ltd" Then
Me.Label476.Visible = True
Else
Me.Label476.Visible = False
End If
 
S

Stefan Hoffmann

hi Bob,

How can I improve this code so that I get accurate results?
I think there are parentheses missing:

Private Sub Form_Current()

Label476.Visible = (Me.Fault = "YES") And _
(Me.Recoverable = "NOT RECOVERABLE") And _
Not IsNull(Me.NameOfOtherParty) And _
((Me.InsuranceCompany = "BR") Or _
(Me.InsuranceCompany = "Br Insurance Company Ltd"))

End Sub

As you see, you don't need the If construct. btw, I would really
recommend that you give controls a meaningful name, if they are
referenced in code or anywhere else.


mfG
--> stefan <--
 
B

Bob07790

Stefan

Than sorted the problem, I have never seen it done like that before. I have
a few If statements like the one below, would it be better to change them to
the way you have coded it? I have also taken on board your point about
naming controls.

Thanks for your help.

Bob
 
B

Bob07790

Stefan

Changing the following code

If Isnull (EMFund) Then
FundNote.Visible = False
Else
FundNote.Visible =True
End If

To
FundNote.Visible = (Me.EMFund = True)

This works but gives the error 94 invalid use of null when the result is False
EMFund is a Tick Box and does not have any where to allow the use of null
values.

I have changed some of my other code to this and it all works fine except
for this one. What have I done wrong?

Thanks Bob
 
S

Stefan Hoffmann

hi Bob,

Changing the following code

If Isnull (EMFund) Then
FundNote.Visible = False
Else
FundNote.Visible =True
End If

To
FundNote.Visible = (Me.EMFund = True)

This works but gives the error 94 invalid use of null when the result is False
EMFund is a Tick Box and does not have any where to allow the use of null
values.

I have changed some of my other code to this and it all works fine except
for this one. What have I done wrong?
The simple trick is that you can use the entire If clause - which is in
fact a boolean expression - and assign it to any boolean variable or
property.

So you need:

FundNote.Visible = Not IsNull(EMFund)


mfG
--> stefan <--
 
S

Stefan Hoffmann

hi Bob,

Than sorted the problem, I have never seen it done like that before. I have
a few If statements like the one below, would it be better to change them to
the way you have coded it?
When you only assign one property or variable in the If, then it is the
better approach, as it is shorter in terms of Lines Of Code.


mfG
--> stefan <--
 

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