Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

500 is not greater than 2000!!! Interger Validation

1 view
Skip to first unread message

Arda

unread,
Dec 12, 2008, 10:09:00 PM12/12/08
to
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


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

Dick Grier

unread,
Dec 13, 2008, 1:04:47 PM12/13/08
to
What is the result of:

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.


Michael Cole

unread,
Dec 14, 2008, 5:59:51 PM12/14/08
to
Arda <Ar...@discussions.microsoft.com> wrote in message
<news:5F0AEE48-017B-4A70...@microsoft.com>

> 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


Jacob Janzen

unread,
Dec 17, 2008, 11:19:28 AM12/17/08
to
"Arda" <Ar...@discussions.microsoft.com> wrote in message
news:5F0AEE48-017B-4A70...@microsoft.com...

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

Michael Cole

unread,
Dec 17, 2008, 4:53:46 PM12/17/08
to
Jacob Janzen <jja...@community.nospam> wrote in message
<news:EDEB030E-D010-4118...@microsoft.com>


It works because he isn't using Option Explicit. It just works badly.

--
Regards

Michael Cole


0 new messages