Trax needs VB6 help :p

Traxata

Junior Administrator
Code:
Private Sub txtSterling_KeyPress(KeyAscii As Integer)
If Key ascii >57 <48 then
msgbox Sorry box only allows numbers 
endif
End Sub
 

Ronin Storm

Administrator
Staff member
I wonder if you're approaching that in the right way?

I'm assuming you have a textbox with a string, e.g. "MyUserName". You want to ensure that only alphabetic characters are used. However, the subroutine you have their is, in style, for checking characters not strings. Presumably you're not doing something crazy like iterating all characters in the string on each key press to check that each character is acceptable?

I can't speak to the syntax as my VB is very old and I've tried to forget the bad things it made me do but I'll have a scan about to see if there's something on form validation that would be stronger.

I'm struck by the thought that regex would help you here.
 

Traxata

Junior Administrator
What i'm trying to do is validate Numerical characters only. otherwise display message box and set focus on that text box
 

Traxata

Junior Administrator
Currency converter, Numbers entered into that box get attacked by this lovelly brick of code

Code:
Option Explicit
Dim Sterling As Double
Dim ToConvert As Double
Dim Commission As Double
Dim DollarRate As Double
Dim YenRate As Double
Dim EuroRate As Double
Dim Converted As Double

Private Sub Form_Load()
    YenRate = 12.23
    DollarRate = 2.01
    EuroRate = 1.75
End Sub

Private Sub cmdConvert_Click()

    If txtSterling.Text = "" Then
        MsgBox ("Please Enter a Sum to Convert")
        txtSterling.SetFoc us
    Else
        Sterling = txtSterling.Text
        Commission = Sterling * 0.05
        ToConvert = Sterling - Commission
    
        If optDollar.Value = True Then
            Converted = ToConvert * DollarRate
            lblConverted.Caption = "Total Dollars"
            lblRate.Caption = "Your £" & Sterling & " was converted into $" & Converted & " at a rate of " & DollarRate & ". The Commission was £" & Commission & ". "
        ElseIf optYen.Value = True Then
            Converted = ToConvert * YenRate
            lblConverted.Caption = "Total Yen"
            lblRate.Caption = "Your £" & Sterling & " was converted into ¥" & Converted & " at a rate of " & YenRate & ". The Commission was £" & Commission & ". "
        ElseIf optEuro.Value = True Then
            Converted = ToConvert * EuroRate
            lblConverted.Caption = "Total Euros"
            lblRate.Caption = "Your £" & Sterling & " was converted into €" & Converted & " at a rate of " & EuroRate & ". The Commission was £" & Commission & ". "
        Else
            MsgBox ("Please Select a Currency to convert to.")
        End If
        
        txtSterling.Text = ""
        txtOrigSterling.Text = Sterling
        txtCommission.Text = Commission
        lblCommission.Caption = "Commission at 5%"
        txtRemainder.Text = ToConvert
        txtConverted.Text = Converted
        
    End If
    
End Sub

Private Sub cmdNew_Click()

    optDollar.Value = False
    optYen.Value = False
    optEuro.Value = False
    txtSterling.Text = ""
    txtOrigSterling.Text = ""
    txtCommission.Text = ""
    txtRemainder.Text = ""
    txtConverted.Text = ""
    lblRate.Caption = ""
    lblCommission = "Commission at"

End Sub

Private Sub cmdExit_Click()
    End
End Sub

Private Sub txtSterling_Change()

End Sub

Private Sub txtSterling_KeyPress(KeyAscii As Integer)
    If KeyAscii > 57 < 48 Then
        MsgBox ("Sorry, Numbers only please.")
        txtSterling.SetFocus
    End If
End Sub

Private Sub txtSterling_Validate(Cancel As Boolean)

End Sub
 

Ronin Storm

Administrator
Staff member
In which case, regex still applies. Just pick a suitable expression that will only allow numbers.
 

thatbloke

Junior Administrator
Not sure about VB6 but the C library has a handy function called "isdigit" which allows you to check whether or not a character is a number. There should be a Key Pressed event that you can use to get at the character that has just been pressed on the keyboard. You can check it there and then and then if it doesnt match replace the character with 0 (i.e. the Null Character).
 

Traxata

Junior Administrator
Ok after working out what you meant ( I was at home before :p ) RegEx seems overly complicated for what I'm doign as I effectivly need to learn the vbscript language to find an expression to block strings, I'll stick with the KeyAscii :p
 
Top