Results 1 to 5 of 5

Thread: Now thats a problem

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 1999
    Location
    Vilnius, Lithuania
    Posts
    50

    Post

    people i have a realy serious problem

    Code:
    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)


  2. #2
    Guest

    Post

    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

  3. #3
    Lively Member
    Join Date
    Dec 1999
    Location
    Karlsruhe, Germany
    Posts
    122

    Post

    The error message results from the parameter declaration "double". The txt.text is of type "string".

    Change call to

    Code:
    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:

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

  4. #4
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845

    Post

    casparas,
    May I make a suggestion about your programming style?!

    It's YUK!
    Use an array of text boxes
    you can then do this:
    Code:
    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

    [email protected]
    A BMW Group Company

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 1999
    Location
    Vilnius, Lithuania
    Posts
    50

    Post

    thanx people

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

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

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