CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: IF

  1. #1
    Join Date
    Sep 2001
    Location
    South Dakota
    Posts
    20

    IF

    Can anyone tell me why this does not work? When I step through it, it skips the intIDLen = 7 and goes to the intIDLen = 9 no matter what.
    Thanks, Pat

    If Left(cmbSite.Text, 7) < 8 Then
    intIDLen = 7
    ElseIf Left(cmbSite.Text, 9) > 8 Then
    intIDLen = 9
    End If


  2. #2
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: IF

    It seems you're missing Len(Left(...))!


  3. #3
    Join Date
    Sep 2001
    Location
    South Dakota
    Posts
    20

    Re: IF

    I put the Len in, but it's still missing. I think what the problem is is that it is reading the seven digits of the 7 digit code, returning that, and then only reading the first seven of the nine digit code? But I don't know. And if that is the case, I'm not sure how to fix it.

    Thanks

    If Len(Left(cmbSite.Text, 9) = 9) Then
    intIDLen = 9
    ElseIf Len(Left(cmbSite.Text, 7) = 7) Then
    intIDLen = 7
    End If


  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: IF

    If you compare the lenght of the string than it will not work because you take the left 7 chars
    In case you need the value the mistake is that
    You compare string with number. Chhange the code to the following
    If CInt(Left(cmbSite.Text, 7)) < 8 Then
    intIDLen = 7
    ElseIf CInt(Left(cmbSite.Text, 9)) > 8 Then
    intIDLen = 9
    End If

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  5. #5
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: IF

    The code should be:

    If len(Left(cmbSite.Text, 9)) = 9 then
    intIDLen = 9
    ElseIf len(Left(cmbSite.Text, 7)) = 7 then
    intIDLen = 7
    End If






  6. #6
    Join Date
    Sep 2001
    Location
    South Dakota
    Posts
    20

    Re: IF

    Ok, I understand what you are saying here, but there is one more thing that I forgot to mention. The first two digits are not numeric. They are either IT or GP, followed by either 5 or 7 numeric numbers.



  7. #7
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: IF

    What are you exactly trying to do? Are you interested in lenght of string, in lenght of numeric part of string, in numeric value of a part of your string or what?


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  8. #8
    Join Date
    Sep 2001
    Location
    South Dakota
    Posts
    20

    Re: IF

    Numeric part of string


  9. #9
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: IF

    Extract numeric part of the string using mid or right function

    right(left(YourString,7),2)

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  10. #10
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: IF


    'have not read all the answers, so do not know if someone else already told you
    'this:
    'If it is a fixed lenght string, you can use mid$ function to extract the whole
    'number. If it is not fixed lenght, you can use mid$ function to extrat one
    'char at a time and see if it is numeric. It may be a little slow for long
    'strings.

    'Example of last:
    '(one textbox, one commandbutton)
    private Sub Command1_Click()
    Dim charToTest as string
    Dim totalNumber as string
    Dim i as Integer
    If len(Text1.Text) > 0 then
    for i = 1 to len(Text1.Text)
    charToTest = mid$(Text1.Text, i, 1)
    If IsNumeric(charToTest) then
    totalNumber = totalNumber & charToTest
    End If
    next i
    MsgBox totalNumber
    End If
    End Sub




    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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

Featured