problem in detecting letters and spacing in vb

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #16
    You could try something like this...
    [CODE=vb]Dim s As String
    Const Digits as String = "0123456789 "
    s = Trim$(text1.Tex t)
    If s = Format(Val(s)) Then
    MsgBox "Looks like a number - hooray!"
    ElseIf s = "0" Then
    ' Zero.
    MsgBox "Zero"
    ElseIf Instr(Digits, Left$(s, 1)) = 0 Then
    ' Doesn't start with a numeric digit.
    MsgBox "Doesn't start with a numeric digit."
    Else
    MsgBox "I'm going to use the number " & Val(s)
    End If[/CODE]

    Comment

    • LeWaltz
      New Member
      • Nov 2007
      • 7

      #17
      hi i have tried out the codes by everyone... but i have some questions about it...

      hi sir killer42, i have edited the code to make it such that only when i key a number, which can also be a decimal value, it will be accepted (such cases are: 123, 123.12, a space bar in the front followed by a number 123, and a space bar at the back after a number 123 . other than this, i will reject the value...

      i have added a trim before your format which i think i hope to allow space bar before and after a number, is this correctly done??? what is actually the format do in the code??

      could you help me see this code is it correctly done when i want only the interger... so i convert it ( i = s )..., or there is some thing i can improve it to be better?

      o ya, and regrading the previous post, the code
      Code:
      If MyString = Str(Val(MyString)) Then
      how do i use it? i still have trouble using.. is there a example? :)

      thanks sir... learned a lot... :)

      Code:
      Private Sub Command1_Click()
      Dim s As String
      Dim i As Long     'to convert s, which maybe in decimal to no decimal...
      
      s = Trim$(Text2.Text)
      
      If s = Format(Trim(Val(s))) Then
          i = s
          MsgBox "Looks like a number - hooray!" & Val(s) & Val(i)
      ' so "s" will be displayed as a interger or a decimal value
      ' and "i" will only display interger value.. (decimal is converted to interger long
      ' where i need for calculation...
      ' so is what i explained correctly written in this code???
          
      Else
        MsgBox "invalid..."
      
      End If
      
      End Sub
      then for Mohan Krishna, hi too.. i have seen your project and and i wish to know what is the use of "CDbl"... and ya u have understood what i mean... i was amazed by the new things i have learned here... the function useful to me is below... and i edited "num" as long instead of double, as i need only intergers only.. but could you help me see is there any error in it so that i can edit and learn more... thanks a lot...
      Code:
      Private Sub Command1_Click()
      On Error GoTo err
          Dim num As Long
          num = CDbl(Text3.Text)
          MsgBox "Numeric" & Val(num)
          Exit Sub
      err:
          MsgBox "Invalid Input"
      End Sub
      and for jaz215 thanks a lot to make me understand lotus18 code that he posted long ago... and lotus18 the code u posted is really good for my application (below).. and of course the rest of the code that all of you posted is also very well written where i can further improve my project.. thanks everyone... :) really grateful to all of you...

      Code:
      Private Sub text1_KeyPress(KeyAscii As Integer)
       
       If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) Then
              KeyAscii = 0
          End If
        
      End Sub
      smile always...
      will*

      Comment

      • lotus18
        Contributor
        • Nov 2007
        • 865

        #18
        Hi LeWaltz

        It's our pleasure to help anyone who are willing to learn : )

        Rey Sean

        Comment

        • jaz215
          New Member
          • Nov 2007
          • 55

          #19
          Originally posted by lotus18
          Hi LeWaltz

          It's our pleasure to help anyone who are willing to learn : )

          Rey Sean
          Yeah everyone here is willing to help :)

          with regards to the cDbl by mohan it converts a string to a double datatype as long as the string is a valid double
          eg.

          Code:
          str as string = "10.10"
          x as double 
          x = cDbl(cstr)
          * x would now be 10.10 but not a string anymore
          also works for cInt for integer and others stuff

          Comment

          • Mohan Krishna
            New Member
            • Oct 2007
            • 115

            #20
            Hi

            CDbl( ) converts string to double and CInt( ) to integer as jaz215 explained. ThanQ Jaz!
            When the string, after conversion is being assigned to numeric, but contains characters, the conversion produces an error... so the code.
            If u want to use only integers, use CInt( ).

            ALL THE BEST!

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #21
              Yes, the Trim function will trim off any leading or trailing spaces.

              I didn't really understand you very clearly. Not sure if this is what you're asking, but the Val() function will convert a string to a number, including decimals.

              The test If MyString = Str(Val(MyStrin g)) Then just uses the Val() function to convert the string to a number, then the Str() function to convert that number into a string, then checks to see whether it is the same as what we started with.
              Last edited by Killer42; Nov 29 '07, 03:41 AM.

              Comment

              • jaz215
                New Member
                • Nov 2007
                • 55

                #22
                Originally posted by Killer42
                Yes, the Trim function will trim off any leading or trailing spaces.

                I didn't really understand you very clearly. Not sure if this is what you're asking, but the Val() function will convert a string to a number, including decimals.

                The test If MyString = Str(Val(MyStrin g)) ThenJust uses the Val() function to convert the string to a number, then the Str() function to convert that number into a string, then checks to see whether it is the same as what we started with.
                now i get this one.. :P

                i kinda need this code to in my vwb using vb.net,
                Thus this code work in vb.net? what's the equivalent? :P

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #23
                  Originally posted by jaz215
                  i kinda need this code to in my vwb using vb.net,
                  Thus this code work in vb.net? what's the equivalent? :P
                  No idea, sorry. Just look up the Str and Val functions - the VB.Net documentation seems to be pretty good on VB6 -> VB.Net upgrades, and should tell you what to use.
                  Last edited by Killer42; Nov 29 '07, 04:04 AM.

                  Comment

                  • Mohan Krishna
                    New Member
                    • Oct 2007
                    • 115

                    #24
                    Originally posted by Killer42
                    You could try something like this...
                    [CODE=vb]Dim s As String
                    Const Digits as String = "0123456789 "
                    s = Trim$(text1.Tex t)
                    If s = Format(Val(s)) Then
                    MsgBox "Looks like a number - hooray!"
                    :
                    :
                    End If
                    [/CODE]
                    Hi Killer!

                    Excellent Code!
                    ThanQ

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #25
                      Originally posted by Mohan Krishna
                      Hi Killer!

                      Excellent Code!
                      Thank you.

                      Comment

                      • LeWaltz
                        New Member
                        • Nov 2007
                        • 7

                        #26
                        once again thank you all... :) learned...

                        smile always...
                        will*

                        Comment

                        Working...