Home > Back-end >  Mismatch problem with textbox and label! vb6
Mismatch problem with textbox and label! vb6

Time:10-20

enter image description hereI have tried to change the code but it didnt works. Here is my code:

Private Sub CTcash_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        bill = Val(CTcash.Text - CLtotaloutput.Caption)
        If Val(bill) > 0 Then
            Change.CLchange.Caption = Val(bill)
            Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
            Change.Show
        End If
        If Val(bill) < 0 Then
            MsgBox "Not Enough Money", vbOKOnly, "Invalid"
        End If
    End If
End Sub

The highlighted code

bill = Val(CTcash.Text - CLtotaloutput.Caption)

CodePudding user response:

Both CTcash.Text and CLTotaloutput.Caption are text strings so you can't subtract them directly. Try:

bill = Val(CTcash.text) - Val(CLtotaloutput.Caption)

You really should ensure that both strings are numerical first though!

CodePudding user response:

At first I miss the const, i put it in the other sub:

Private Sub CTcash_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        bill = Val(CTcash.Text) - totalsum
        Select Case True
            Case bill > 0
               Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
               Change.Show
            Case bill < 0
               MsgBox "Not Enough Money", vbOKOnly, "Invalid"
            Case Else
        End Select
    End If
End Sub
Private Sub CTbarcode_KeyUp(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case Is = vbKeyF6
            totaldiscount = 200
            totalsum = Val(CLsuboutput.Caption) - totaldiscount
            CLtotaloutput.Caption = Format(totalsum, "Rp\. ###,###,###. ,-")
            CLdiscoutput.Caption = Format(totaldiscount, "Rp\. ###,###,###. ,-")
            CTcash.Enabled = True
            CTcash.SetFocus
    End Select
End Sub

Here is it after i edit it:

Private Sub CTcash_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        totaldiscount = 200
        totalsum = Val(CLsuboutput.Caption) - totaldiscount
        bill = Val(CTcash.Text) - totalsum
        Select Case True
            Case bill > 0
               Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
               Change.Show
            Case bill < 0
               MsgBox "Not Enough Money", vbOKOnly, "Invalid"
            Case Else
        End Select
    End If
End Sub

CodePudding user response:

@John Eason is correct. You also do not have to use Val on bill since it is already an integer. And you are not doing anything if bill is 0 but this could be fine based on your requirements.

Private Sub CTcash_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        bill = Val(CTcash.Text) - Val(CLtotaloutput.Caption)
        If bill > 0 Then ' Note that when bill is 0 nothing happens but that might be fine
            'Change.CLchange.Caption = bill 'Not sure why this is needed but it may be
            Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
            Change.Show
        End If
        If bill < 0 Then
            MsgBox "Not Enough Money", vbOKOnly, "Invalid"
        End If
    End If
End Sub

I think a better approach would be to use a Select Case statement.

Private Sub CTcash_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        bill = Val(CTcash.Text) - Val(CLtotaloutput.Caption)

        Select Case True
            Case bill > 0 
               Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
               Change.Show 
            Case bill < 0
               MsgBox "Not Enough Money", vbOKOnly, "Invalid"
            Case Else
               ' Do nothing - this documents that this is done on purpose
        End Select
     
    End If
End Sub

You can use IsNumeric function to check if the inputs are numeric and parse accordingly. Use the Right function to remove any non-numbers from the left side. For example, Right("$123", Len("$123") - 1) will remove the leading $.

To strip all non-numerical values from a string:

Dim ResultString As String
myString = "aaa34BB12,000xcv9.9zz"

Dim i As Integer

For i = 1 To Len(myString)
    myChar = Mid(myString, i, 1)
    If IsNumeric(myChar) = True Then
        ResultString = ResultString   myChar
    End If
Next

MsgBox ResultString

Note that this may cause an issue with decimal amounts since 1.23 will before 123. Not something you want. Try using the CCur function. I have never used it myself but it might be exactly what you want.

Source: https://www.vbforums.com/showthread.php?437526-RESOLVED-Removing-non-numeric-chars-from-a-string&p=2686411&viewfull=1#post2686411

  • Related