Results 1 to 9 of 9

Thread: Percentage input to textbox

  1. #1

    Thread Starter
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250

    Percentage input to textbox

    Sorry for the simple question, but..

    Anyone point me to an example of how i can validate the contents of a textbox to only allow a percentage, ie 0 to 100.00?


    All comments gratefully received unless i fall asleep *smile*


    lee

  2. #2
    Frenzied Member Mega_Man's Avatar
    Join Date
    Mar 2001
    Location
    North of England, South-East of Iceland
    Posts
    1,067
    Why would you want to validate a percentage to 2 decimal places.
    Can;t you just make sure the contents of the test box falls between 0 and 100 instead of 100.00?

    Mega.
    "If at first you don't succeed, then skydiving is not for you"

  3. #3
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    This any good?
    VB Code:
    1. Private Sub Text1_Validate(Cancel As Boolean)
    2.  
    3. If Val(Text1.Text) < 1 Then
    4.     MsgBox "Must be at least 1%", vbInformation
    5.     Cancel = True
    6. ElseIf Val(Text1.Text) > 100 Then
    7.     MsgBox "Can't be more than 100%", vbInformation
    8.     Cancel = True
    9. End If
    10.  
    11. End Sub

  4. #4
    Megatron
    Guest
    Try this.
    VB Code:
    1. Dim sInput As String
    2.  
    3. sInput = InputBox("Enter a percent:")
    4.  
    5. If IsNumeric(sInput) Then
    6.     If CSng(sInput) <= 100 And CSng(sInput) >= 0 Then sInput = Format$(sInput, "###.00")
    7. End If

  5. #5
    DaoK
    Guest
    What is that : CSng?

  6. #6
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    converts a number to single

  7. #7
    AIS_DK
    Guest
    Converts numbers and strings to a single

  8. #8
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    well if we're going to split hairs...converts an expression to a single data type

  9. #9
    AIS_DK
    Guest
    Well, since we at it.
    Converts any string expression or numeric expression to a single.

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