I am writing a piece of code and it needs to see if a > b etc (a=2000 and
b=500). and it keeps saying b is greater. Ive dimmed a and b as integer,
single, double, currency still doing the same thign. Can someone please help.
Cheers. PS This is the code ive written
Private Sub Form_Load()
On Error Resume Next
Dim a As Long
Dim b As Long
Label2.Caption = "500"
Label1.Caption = "2000"
End Sub
Private Sub Command1_Click()
a = Label1.Caption
b = Label2.Caption
If a > b Then
Label3.Caption = a & " is greater"
Else
Label3.Caption = b & " is greater"
End If
End Sub
Private Sub Command1_Click()
Dim a As Double
Dim b As Double
a = Val(Label1.Caption)
b = Val(Label2.Caption)
If a > b Then
Label3.Caption = a & " is greater"
Else
Label3.Caption = b & " is greater"
End If
End Sub
Dick
--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
> Hi, First off I am not sure if ive posted this onto the right
> discussion group, appologies in advance, but here is my problem.
>
> I am writing a piece of code and it needs to see if a > b etc (a=2000
> and b=500). and it keeps saying b is greater. Ive dimmed a and b as
> integer, single, double, currency still doing the same thign. Can
> someone please help. Cheers. PS This is the code ive written
Where did you dim them (in what procedure)?
Hint here - look up Option Explicit in the help file, and _always use it_.
It stops problems like this.
> Private Sub Form_Load()
> On Error Resume Next
> Dim a As Long
> Dim b As Long
> Label2.Caption = "500"
> Label1.Caption = "2000"
>
>
> End Sub
>
> Private Sub Command1_Click()
>
> a = Label1.Caption
> b = Label2.Caption
>
> If a > b Then
> Label3.Caption = a & " is greater"
> Else
> Label3.Caption = b & " is greater"
> End If
> End Sub
--
Regards
Michael Cole
You are comparing String types, not Long types. You should cast the value
to a Long for numeric comparisons...
However, I don't see how your code works at all as a and b are defined as
sub level variables in your Load Event...
Jake
It works because he isn't using Option Explicit. It just works badly.
--
Regards
Michael Cole