Results 1 to 7 of 7

Thread: [RESOLVED] Why is "10" in a label less than "7" in a textbox?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    113

    Resolved [RESOLVED] Why is "10" in a label less than "7" in a textbox?

    I have a label that increases by +1 on the push of a button.

    It then checks to see if the label contents is more than (using ">") a textbox value and displays a msgbox if it is more.

    Now this works until it gets to 10. The textbox is set to 7 and it displays a msgbox on 8 and 9 then when it gets to 10, 11, 12 etc.. it just stops displaying the msgbox.

    Anyone know why?

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Why is "10" in a label less than "7" in a textbox?

    You cannot compare the .Text value of two controls for a numeric value.

    You must do something like

    If CLng(Text1.Text)>CLng(Label1.Caption) Then

    You must convert to a numeric datatype.

    You are falling into the trap of datatype coercion - search the forum here for that and you will find lots of posts about it.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    Junior Member
    Join Date
    Sep 2008
    Posts
    20

    Re: Why is "10" in a label less than "7" in a textbox?

    i can i see your code...

    but i think... you should make sure that logical operation(>) is using integer as data type....

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Why is "10" in a label less than "7" in a textbox?

    Quote Originally Posted by djwk
    I have a label that increases by +1 on the push of a button.

    It then checks to see if the label contents is more than (using ">") a textbox value and displays a msgbox if it is more.

    Now this works until it gets to 10. The textbox is set to 7 and it displays a msgbox on 8 and 9 then when it gets to 10, 11, 12 etc.. it just stops displaying the msgbox.

    Anyone know why?
    You're comparing text, not numerical values. In alphanumeric sorting, "1" comes before "7".

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    113

    Re: Why is "10" in a label less than "7" in a textbox?

    Thanks guys, I've actually had this problem before but just forgot. I went through some of my old code. I changed my textboxes to labels and used the following:

    Val(Label1)

  6. #6
    Hyperactive Member Arachnid13's Avatar
    Join Date
    Jan 2003
    Location
    England
    Posts
    327

    Re: Why is "10" in a label less than "7" in a textbox?

    Quote Originally Posted by leinad31
    You're comparing text, not numerical values. In alphanumeric sorting, "1" comes before "7".
    Just to clarify this a bit further, as 1 also comes before 7 in numerical sorting.

    For ASCii values 0 - 127, sorting alphanumerically is the same as sorting by the ASCii values of the characters, e.g. a < A (65 < 97), however once you get past 128 the sorting goes a bit erratic, fortunately all the commonly typed characters are in the 0 - 127 range.

    If anyone wants to take a look at how weird things go from 128 onwards, make a new form and put a list box on it, then stick this code in:

    Code:
    Private Sub Form_Load()
        Dim blnCharacterUsed(0 To 255) As Boolean
        Dim lngCurrentLowest As Long
        Dim i As Long
        Dim j As Long
        
        List1.Clear
        
        For i = 0 To 255
            lngCurrentLowest = -1
            For j = 0 To 255
                If Not blnCharacterUsed(j) Then
                    If lngCurrentLowest = -1 Then
                        lngCurrentLowest = j
                    Else
                        If Chr(lngCurrentLowest) > Chr(j) Then
                            lngCurrentLowest = j
                        End If
                    End If
                End If
            Next j
            blnCharacterUsed(lngCurrentLowest) = True
            List1.AddItem CStr(lngCurrentLowest)
        Next i
        
    End Sub
    I'm quite curious about this now so does anyone know why VB treats the higher value characters like this?
    Do you wake up in the morning feeling sleepy and grumpy? Then you must be Snow White

  7. #7
    Hyperactive Member Arachnid13's Avatar
    Join Date
    Jan 2003
    Location
    England
    Posts
    327

    Re: [RESOLVED] Why is "10" in a label less than "7" in a textbox?

    As this thread is resolved I've continued the discussion here for anyone who's interested
    Do you wake up in the morning feeling sleepy and grumpy? Then you must be Snow White

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width