Results 1 to 4 of 4

Thread: text box help (new to vb)

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    10

    Im using the following function

    Public Function CheckText(MyText As String) As Boolean

    'function returns True if text is OK else returns False

    If MyText = "" Then
    CheckText = False
    Exit Function
    End If


    If Not IsNumeric(MyText) Then
    CheckText = False
    Exit Function
    End If

    'If we get to here, MyText is a number
    ' So set function to true

    heckText = True

    End Function

    and then calling it when I enter data in a text box to stop errors I was getting if the box was empty or didn't have a number in it.

    The code is


    Private Sub txtsupplyfo2_Change()


    If CheckText(txtsupplyfo2.Text) = True Then

    'do the calculations for related data

    txtmaxpo2.Text = txtsupplyfo2.Text * txtbar.Text

    txtfio2.Text = ((txttotaldump.Text * txtsupplyfo2.Text) - txtconsumption.Text) /(txttotaldump.Text - txtconsumption.Text)
    Else
    txtsupplyfo2.Text = ""
    End If

    End Sub

    The problem is....in the calculation I can have txtsupplyfo2.Text with a value greater than 1 but with this code I am only able to enter whole numbers.
    How can I change it to let decimals be entered. Or should I just have the user enter a whole number (%) and then do a calculation to make the number a fraction before I use it to calculate the other visible data?

    please help.

    Thanks,
    Scott

    PS Does anyone here know where I can get some live one on one lessons or training for vb in Sydney, Australia?
    __________________
    Trying to teach myself visual basic

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Option Explicit
    'allow for backspace and one decimal place
    Public x As Integer
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    
    If KeyAscii = 46 And x < 1 Or _
        KeyAscii = 8 Or _
        KeyAscii >= 48 And KeyAscii <= 57 Then
        KeyAscii = KeyAscii
    Else
        KeyAscii = 13
    End If
    
    If KeyAscii = 46 Then x = 1
    
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    10

    Still trying....almost.

    Ok I used this....Is it right??? Does the equation go where Ive added it or should it run seperately under
    txtxxx_Change()???
    It works but when I delete all the numbers then try to add one I get an error at the equation??

    Thanks,
    Scott

    Option Explicit
    'allow for backspace and one decimal place
    Public x As Integer

    Private Sub txtdepth_KeyPress(KeyAscii As Integer)

    If KeyAscii = 46 And x < 1 Or _
    KeyAscii = 8 Or _
    KeyAscii >= 48 And KeyAscii <= 57 Then
    KeyAscii = KeyAscii
    Else
    KeyAscii = 13
    End If

    If KeyAscii = 46 Then x = 1
    txtbar.Text = (txtdepth.Text / 10) + 1
    End Sub

    Private Sub txtbar_KeyPress(KeyAscii As Integer)

    If KeyAscii = 46 And x < 1 Or _
    KeyAscii = 8 Or _
    KeyAscii >= 48 And KeyAscii <= 57 Then
    KeyAscii = KeyAscii
    Else
    KeyAscii = 13
    End If

    If KeyAscii = 46 Then x = 1
    txtdepth.Text = (txtbar.Text - 1) * 10
    End Sub

    Trying to teach myself visual basic

  4. #4
    Lively Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    88
    Hi I hope this is solve your problem..

    Option Explicit

    Private Sub txtdepth_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
    'If the textbox is not empty, do calculation
    If Len(Trim$(txtdepth.Text)) > 0 Then
    txtbar.Text = (Val(txtdepth.Text) / 10) + 1
    End If
    KeyAscii = 0
    ElseIf Not IsNumeric(Chr$(KeyAscii)) And _
    Chr$(KeyAscii) <> "." And _
    KeyAscii <> vbKeyReturn And _
    KeyAscii <> vbKeyDelete Then
    KeyAscii = 0
    End If
    End Sub

    Private Sub txtbar_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
    'If the textbox is not empty, do calculation
    If Len(Trim$(txtdepth.Text)) > 0 Then
    txtdepth.Text = (Val(txtbar.Text) - 1) * 10
    End If
    KeyAscii = 0
    ElseIf Not IsNumeric(Chr$(KeyAscii)) And _
    Chr$(KeyAscii) <> "." And _
    KeyAscii <> vbKeyReturn And _
    KeyAscii <> vbKeyDelete Then
    KeyAscii = 0
    End If
    End Sub




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