PDA

Click to See Complete Forum and Search --> : Now thats a problem


casparas
Dec 29th, 1999, 06:13 PM
people i have a realy serious problem


general declarations

Function Plus(Z As Double, X As Double, Y As Double, A As Double)
Plus = Z + X + Y + A
End Function

Private Sub txt1_Change()
If txt1.Text = "" Then
txt1.Text = 0
End If
If txt2.Text = "" Then
txt2.Text = 0
End If
If txt3.Text = "" Then
txt3.Text = 0
End If
If txt4.Text = "" Then
txt4.Text = 0
End If
txt1234.Text = Plus(txt1.Text, txt2.Text, txt3.Text, txt4.Text)
End Sub

Private Sub txt2_Change()
If txt1.Text = "" Then
txt1.Text = 0
End If
If txt2.Text = "" Then
txt2.Text = 0
End If
If txt3.Text = "" Then
txt3.Text = 0
End If
If txt4.Text = "" Then
txt4.Text = 0
End If
txt1234.Text = Plus(txt1.Text, txt2.Text, txt3.Text, txt4.Text)
End Sub

Private Sub txt3_Change()
If txt1.Text = "" Then
txt1.Text = 0
End If
If txt2.Text = "" Then
txt2.Text = 0
End If
If txt3.Text = "" Then
txt3.Text = 0
End If
If txt4.Text = "" Then
txt4.Text = 0
End If
txt1234.Text = Plus(txt1.Text, txt2.Text, txt3.Text, txt4.Text)
End Sub

Private Sub txt4_Change()
If txt1.Text = "" Then
txt1.Text = 0
End If
If txt2.Text = "" Then
txt2.Text = 0
End If
If txt3.Text = "" Then
txt3.Text = 0
End If
If txt4.Text = "" Then
txt4.Text = 0
End If
txt1234.Text = Plus(txt1.Text, txt2.Text, txt3.Text, txt4.Text)
End Sub

ok now here is my first program on VB
(Snif snif)

and my problem is:
when i enter text in the textbox(not numbers)
program gives me an error type mismatch or
something like that
is there any posible way to display your own error message
(for egzample:sorry but you entered some text
you shoud enter numbers)
is there any way of writing only numbers
(i mean you can not write text at all just numbers)
how to make my code shorter?

sorry for my english(it's not my native language)

Dec 29th, 1999, 06:37 PM
You can check for numbers like..

dim mystring as string

mystring = "Kayoca"

If IsNumeric(mystring) Then

else

msg = msgbox("Error", VbokOnly, "Number Error")

end if

RogerH
Dec 29th, 1999, 06:43 PM
The error message results from the parameter declaration "double". The txt.text is of type "string".

Change call to


txt1234.Text = Plus(Val(txt1.Text), Val(txt2.Text), Val(txt3.Text), Val(txt4.Text))


You can prevent entering anything else then numbers with the following code:


Private Sub txt1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub


RogerH

Mark Sreeves
Dec 29th, 1999, 06:55 PM
casparas,
May I make a suggestion about your programming style?!

It's YUK!
Use an array of text boxes
you can then do this:

Private Sub txt_Change(Index As Integer)
Dim i As Integer
For i = 0 To txt().UBound
If Not IsNumeric(txt(i).Text) Then
txt(i).Text = 0
End If
Next i

txt1234.Text = Plus(Val(txt(0).Text), Val(txt(1).Text), Val(txt(2).Text), Val(txt(3).Text))

End Sub




------------------
Mark Sreeves
Analyst Programmer

Mark.Sreeves@Softlab.co.uk
A BMW Group Company

casparas
Dec 29th, 1999, 10:24 PM
thanx people

(i'll try to do change my style Mark)

[This message has been edited by casparas (edited 12-30-1999).]