Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    May 2003
    Sacramento, California, USA
    Thanked 0 Times in 0 Posts

    Incorrect Result from code (A2k)

    The following code is not returing the correct result:

    Option Compare Database
    Option Explicit

    Private Sub cmdCompare_Click()
    Dim iResult As Integer
    iResult = StrComp(txtFirstString.Value, txtSecondString.Value)

    Select Case iResult

    Case -1
    MsgBox "The first string is less than the second"
    Case 0
    MsgBox "Both Strings are equal"
    Case 1
    MsgBox "The first string is greater than the second"
    Case Else
    MsgBox "One or more strings is null"

    End Select

    End Sub

    On my form I have two text boxes and one command button-When I enter my name(Gabrielle Lyn Fonseca) in the txtFirstString and my fiance's in the other (Keith Van Winkle) and then I then click the cmdCompare, the message box returns "The first string is less than the second". This is clearly not true, as there are many more characters in my name than in Keith's. What am I doing wrong??? <img src=/S/dizzy.gif border=0 alt=dizzy width=15 height=15>

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Thanked 31 Times in 31 Posts

    Re: Incorrect Result from code (A2k)

    String comparison is alphabetic first; only if one string is entirely contained in the other does the length come into play. In this case, your name is "less" than that of your significant other because your name begins with a G and his with a K. Access doesn't look further than that.
    Think of it as entries in the phone book. Fonseca comes before Winkle even if it contains one more letter.

  3. #3
    4 Star Lounger
    Join Date
    Feb 2001
    Richland, Washington, USA
    Thanked 0 Times in 0 Posts

    Re: Incorrect Result from code (A2k)

    StrComp returns -1 if the first string would show up before the second in an alphabetized list (earlier in the phone book, using Hans' analogy), 0 if they're the same (character-for-character), and 1 if the first string would show up later in an alphabetized list. In effect, the function first checks the first character of each string. If they're different then that determines the order (the result of the function), right off. If they're the same, then it moves to the next character of each string and checks them. Only if it moves through every character without finding a difference will the function return a zero.

    If you truly want to compare the lengths of the strings, then compare the results of the len() function for each string (e.g., If len(txtFirstString.Value) > len(txtSecondString.Value) Then...).

Posting Permissions

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