Results 1 to 9 of 9

Thread: VB 6 strComp() Help!

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    6

    VB 6 strComp() Help!

    I'm just learning VB 6 and I don't know what is wrong, it's working, but not for all values I put in (e.g."tt" or "rr") Thank you

    Option Explicit

    Private Sub calcFee_Click()
    Dim intRez, intMsg As Integer
    intRez = 1
    Dim strCode As String
    Const conButton As String = vbOKOnly + vbInformation + vbDefaultButton1 + vbApplicationModal

    strCode = txtCode.Text

    Select Case intRez
    Case Is = StrComp(strCode, "TC", vbTextCompare)
    lblFee.Caption = "The fee is " & "$" & 50
    Case Is = StrComp(strCode, "RV", vbTextCompare)
    lblFee.Caption = "the fee is " & "$" & 15
    Case Is = StrComp(strCode, "OS", vbTextCompare)
    lblFee.Caption = "the fee is " & "$" & 5
    Case Else
    intMsg = MsgBox("Invalid Entry! Try again!", conButton, "List Procedure")
    txtCode.SetFocus
    txtCode.SelStart = 0
    txtCode.SelLength = 2

    End Select

    End Sub

  2. #2
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: VB 6 strComp() Help!

    what exactly is it supposed to do? and what exactly is happening that is wrong?

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: VB 6 strComp() Help!



    strComp returns 0 when there is a match so I think you want to do this

    Code:
    Private Sub calcFee_Click()
    Dim intRez, intMsg As Integer
    intRez = 0 '1
    Dim strCode As String
    Const conButton As String = vbOKOnly + vbInformation + vbDefaultButton1 + vbApplicationModal
    
    strCode = txtcode.Text
    
    Select Case intRez
    Case Is = StrComp(strCode, "TC", vbTextCompare)
    lblfee.Caption = "The fee is " & "$" & 50
    Case Is = StrComp(strCode, "RV", vbTextCompare)
    lblfee.Caption = "the fee is " & "$" & 15
    Case Is = StrComp(strCode, "OS", vbTextCompare)
    lblfee.Caption = "the fee is " & "$" & 5
    Case Else
    intMsg = MsgBox("Invalid Entry! Try again!", conButton, "List Procedure")
    txtcode.SetFocus
    txtcode.SelStart = 0
    txtcode.SelLength = 2
    
    End Select
    
    End Sub

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: VB 6 strComp() Help!

    You could simplify it like this.

    Code:
    Private Sub calcFee_Click()
    Dim intMsg As Integer
    Const conButton As String = vbOKOnly + vbInformation + vbDefaultButton1 + vbApplicationModal
    
    Select Case UCase(txtcode.Text)
        Case "TC"
            lblfee.Caption = "The fee is " & "$" & 50
        Case Is = "RV"
            lblfee.Caption = "the fee is " & "$" & 15
        Case "OS"
            lblfee.Caption = "the fee is " & "$" & 5
        Case Else
            intMsg = MsgBox("Invalid Entry! Try again!", conButton, "List Procedure")
            txtcode.SetFocus
            txtcode.SelStart = 0
            txtcode.SelLength = 2
    End Select
    
    End Sub
    BTW in your original code intRez does not get defined as an Integer but rather as a Variant. In order for both to be Integers you need to do

    Dim intRez As Integer, intMsg As Integer

    or

    Dim intRez As Integer
    Dim intMsg As Integer

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    6

    Re: VB 6 strComp() Help!

    Thank you very much for responding me so promptly!
    You are right, I have changed to "Dim intRez As Integer, intMsg As Integer"

    Using UCase looks much better, I tried that too but it didn't work at all, even using "TC" as an entry value, the MSG box appears.

    Also I tried with intRez=0, because I found this too in a VB reference book, but it didn't work at all !!!!
    With intRez=1 works good, BUT ONLY if I enter TC,RV,OS and "others for Case Else", I mean the values mentioned in the Select Case statement; if I use other values (e.g, tt as an entry) the value is 50 like in TC. Looks like only the first letter is tested. I'll try it again tomorrow

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: VB 6 strComp() Help!

    conButton should be declared as an Integer, not a String. The constants are all numeric values.

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    6

    Re: VB 6 strComp() Help!

    Quote Originally Posted by penagate
    conButton should be declared as an Integer, not a String. The constants are all numeric values.
    Constants can be String as well

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    6

    Re: VB 6 strComp() Help!

    I didn't wait till tomorrow...and I found the problem!!! The program was saved in VB98 from Visual Studio! How did this make so much trouble? (Please understand that I'm new in VB programming )

    Now I saved the same program in the same folder with my other applications, and both variants are working perfect!!!!!: the Ucase and mine (of course this time with intRez=0) Thank you very much MartinLiss!
    Thank you to all of you

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: VB 6 strComp() Help!

    Quote Originally Posted by cami
    Constants can be String as well
    Yes, but these aren't.

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