Code not giving the correct results

  • Follow


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

0
Reply Utf 3/6/2010 3:41:01 PM

hi Bob,

On 06.03.2010 16:41, Bob07790 wrote:
> 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 <--
0
Reply Stefan 3/6/2010 6:10:18 PM


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

"Stefan Hoffmann" wrote:

> hi Bob,
> 
> On 06.03.2010 16:41, Bob07790 wrote:
> > 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 <--
> .
> 
0
Reply Utf 3/7/2010 2:42:01 PM

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
"Bob07790" wrote:

> 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
> 
> "Stefan Hoffmann" wrote:
> 
> > hi Bob,
> > 
> > On 06.03.2010 16:41, Bob07790 wrote:
> > > 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 <--
> > .
> > 
0
Reply Utf 3/7/2010 3:52:01 PM

hi Bob,

On 07.03.2010 16:52, Bob07790 wrote:
> 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 <--
0
Reply Stefan 3/7/2010 6:40:31 PM

hi Bob,

On 07.03.2010 15:42, Bob07790 wrote:
> 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 <--
0
Reply Stefan 3/7/2010 6:42:06 PM

5 Replies
148 Views

(page loaded in 0.104 seconds)


Reply: