473,320 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

problem in detecting letters and spacing in vb

hi everyone, i am a newbie here, and i have a problem in my programming code...
i wanted to make sure that i can only detect positive numbers, and not letters... examles: "abc", "ab100", "-100", "100ab"
all these will display a message box "invalid values"
however here i have a problem in detecting negative numbers "-100" and also a number followed by letters or characters other than numbers "100ab" (this happens when user accidentally keyed a char on keyboard).

at the same time, if i key a "space" of the keyborad after a number, example: "100 " or vice versa, it will allow to be pass to a textbox as "100" (without the space)

i've tried trim function...

value = trim(text1.text) 'to trim what i have keyed and jus display the
'letters and numbers
but will this solve my spacing problem??? i wish to know more about this trim function too please do tell me more.. (really keen in learning... :) )

the code is below...

Expand|Select|Wrap|Line Numbers
  1. dim value as integer
  2.  
  3. value = val(trim(text1.text))
  4.  
  5. if value <> 0 then
  6.  
  7. msgbox("you have keyed a number")
  8. text2.text = value
  9.  
  10. else
  11. if text1.text = "0" then
  12.  
  13. msgbox("you have keyed zero!")
  14. text2.text = value
  15.  
  16. else
  17. msgbox("invalid values")
  18. end if
  19.  
thanks a lot of helping guys.. really need help in my programming...

will*
Nov 14 '07 #1
25 3569
jaz215
55
i dont know if this is what you really want..

you could just lock the textbox keypress event to allow only numbers that way it solves the problem of letters being inputted and also the - negative symbol just pure numbers.

im not sure on the exact code of this try searching on it..
i'll try to provide a code later on.
hope this helped :P
Nov 14 '07 #2
debasisdas
8,127 Expert 4TB
You need to handle the keypress event of the textbox and capture the keyascii values .
Nov 14 '07 #3
lotus18
866 512MB
i dont know if this is what you really want..

you could just lock the textbox keypress event to allow only numbers that way it solves the problem of letters being inputted and also the - negative symbol just pure numbers.

im not sure on the exact code of this try searching on it..
i'll try to provide a code later on.
hope this helped :P
I came up with these codes that allows only numbers

Try this:

Expand|Select|Wrap|Line Numbers
  1. Public Function Numeric(KeyAscii As Integer)
  2.     If KeyAscii = 8 Or KeyAscii = vbKeyDecimal Then
  3.         KeyAscii = 8
  4.     Else
  5.         If KeyAscii < 48 Or KeyAscii > 57 Then
  6.             KeyAscii = 0
  7.         End If
  8.     End If
  9. End Function
  10.  
Nov 14 '07 #4
Killer42
8,435 Expert 8TB
You need to handle the keypress event of the textbox and capture the keyascii values .
Alternatively, you could do something like the code shown above, but instead of just checking for non-zero, try comparing the string with the same string converted to numeric and back. For instance...
If MyString = Str(Val(MyString)) Then
This would come after you've used Trim() to chuck spaces, and perhaps an IsNumeric or similar check to catch anything obvious.

Once you've got a number, of course, it's simple to detect negatives by just checking whether it's less than zero.
Nov 21 '07 #5
Robbie
180 100+
Alternatively, you could do something like the code shown above, but instead of just checking for non-zero, try comparing the string with the same string converted to numeric and back. For instance...
If MyString = Str(Val(MyString)) Then
This would come after you've used Trim() to chuck spaces, and perhaps an IsNumeric or similar check to catch anything obvious.
Yep, that's a good thing to do whenever you need to store a string as a numeric value and then display it a string as well, because it also removes reduntant digits and formats it more desirably (e.g. ' .3000 ' becomes '0.3').
Nov 21 '07 #6
AHMEDYO
112 100+
Alternatively, you could do something like the code shown above, but instead of just checking for non-zero, try comparing the string with the same string converted to numeric and back. For instance...
If MyString = Str(Val(MyString)) Then
This would come after you've used Trim() to chuck spaces, and perhaps an IsNumeric or similar check to catch anything obvious.

Once you've got a number, of course, it's simple to detect negatives by just checking whether it's less than zero.
very nice Killer42, i like this one, it was never passed in my mind :)

Kind Regards
Nov 21 '07 #7
Mohan Krishna
115 100+
I came up with these codes that allows only numbers

Try this:
:
:
Hello Lotus18

I tried like this earlier in my projects
Expand|Select|Wrap|Line Numbers
  1. vb
  2.     If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) Then
  3.         KeyAscii = 0
  4.     End If
  5.  
...but I never thought of Decimal places but when I tried your code and also mine the decimal point is not working. Its Ascii value is 46 and vbKeyDecimal is 110.

ThanQ
Nov 23 '07 #8
lotus18
866 512MB
Hello Lotus18

I tried like this
Expand|Select|Wrap|Line Numbers
  1. vb
  2.     If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) Then
  3.         KeyAscii = 0
  4.     End If
  5.  
Also I never thought of Decimal places but when I tried your code and also mine the decimal point is not working. Its Ascii value is 46 and vbKeyDecimal is 110.

ThanQ
Hi Mohan

Maybe I didn't explain it clear. This function only requires only numbers from 0 to 9. You can apply the suggestion of killer42 : )

Have a nice day!
Nov 23 '07 #9
Mohan Krishna
115 100+
Hi Mohan

Maybe I didn't explain it clear. This function only requires only numbers from 0 to 9. You can apply the suggestion of killer42 : )

Have a nice day!
But, as KILLER42 suggestion is acceptible only after the TextBox LostFocus() to check whether the entered text is Numeric or not, I think to my knowledge!
Nov 23 '07 #10
Killer42
8,435 Expert 8TB
But, as KILLER42 suggestion is acceptible only after the TextBox LostFocus() to check whether the entered text is Numeric or not, I think to my knowledge!
Definitely. The conversion to numeric and back will probably only work with the whole thing, somewhere like LostFocus.
Nov 24 '07 #11
thanks a lots guys, i am still trying out what you all have suggested, i but i still have some doubts about some stuffs.. will post again soon about my doubts..

hope you all don't mind...

thanks again
will*
Nov 24 '07 #12
Hi.

After I tried, I realise I didn't really explain clearly what I am actually doing... em the project is like this...

There is a textbox for user to type in the numbers he wants to use for a calculation. But in order to send he needs to click a button to do the calculations. So all the function code is inside the button click command
(Private Sub Command1_Click())

Inside this function, I want to make sure the textbox will do what the user keyed in. Below is the code...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command_Click()
  2. Dim value As Integer
  3.  
  4. value = UCase(Trim(Val(Text1.Text)))
  5. If value <> 0 Then
  6.   'do the calculations when the value is smaller than or bigger than zero.
  7. Else
  8.   If Text1.Text = "0" Then
  9.     'just print zero in the result of calculations.
  10.   Else
  11.     'other than a number (which I planned to do but failed)
  12.     MsgBox ("Invalid values")
  13.   End If
  14. End If
  15. End Sub
This is it. So I'm still not sure how I should use the code that you all have suggested to me in my code to modify it better with all of your help. :)

By the way, currently after I tried mine, the result of this code are...
1) If user keyed in textbox1 with a number only, do the calculations.
2) If user keyed in textbox1 with a zero only, print "zero".
3) If user keyed in textbox1 with letters 1st followed by numbers (eg: abc123), print "invalid input.
4) If user keyed in textbox1 with numbers 1st followed by letters (eg: 123abc), (here's the error) print out (123) just the numbers that is keyed 1st before the letters.

Once again, thanks a lot everyone.. hope to hear from you all soon. :)

Smile always.
will*
Nov 25 '07 #13
jaz215
55

this is it... so i still not sure how should i use the code that you all have suggested to me in my code to modify it better with all of your help... :)


smile always
will*
the codes that have been suggested to prevents a user from entering letters in the textbox. to use the codes given, insert in to the textbox's keypress event suchas given below

Expand|Select|Wrap|Line Numbers
  1. Private Sub text1_KeyPress(KeyAscii As Integer)
  2.  
  3.  If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) Then
  4.         KeyAscii = 0
  5.     End If
  6.  
  7.  
  8. End Sub
it will only allow numbers 0-9 to be inputed in your textboxes, i don't know about the decimal though, just probably add the keyascii value of the decimal and also for the negative symbol

then just do your checking for those
Nov 27 '07 #14
Mohan Krishna
115 100+
:
:
this is it... so i still not sure how should i use the code that you all have suggested to me in my code to modify it better with all of your help... :)

by the way, currently after i tried mine, the result of this code are...
1) if user keyed in textbox1 with a number only, do the calculations.
2) if user keyed in textbox1 with a zero only, print "zero".
3) if user keyed in textbox1 with letters 1st followed by numbers (eg: abc123), print "invalid input.
4) if user keyed in textbox1 with numbers 1st followed by letters (eg: 123abc), (here's the error) print out (123) just the numbers that is keyed 1st before the letters...
:
:
Hi

This code may help u, if I understood u...


ALL THE BEST!
Attached Files
File Type: zip StringFunctions.zip (2.6 KB, 117 views)
Nov 27 '07 #15
Killer42
8,435 Expert 8TB
You could try something like this...
Expand|Select|Wrap|Line Numbers
  1. Dim s As String
  2. Const Digits as String = "0123456789"
  3. s = Trim$(text1.Text)
  4. If s = Format(Val(s)) Then
  5.   MsgBox "Looks like a number - hooray!"
  6. ElseIf s = "0" Then
  7.   ' Zero.
  8.   MsgBox "Zero"
  9. ElseIf Instr(Digits, Left$(s, 1)) = 0 Then
  10.   ' Doesn't start with a numeric digit.
  11.   MsgBox "Doesn't start with a numeric digit."
  12. Else
  13.   MsgBox "I'm going to use the number " & Val(s)
  14. End If
Nov 27 '07 #16
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
Expand|Select|Wrap|Line Numbers
  1. 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... :)

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. Dim s As String
  3. Dim i As Long     'to convert s, which maybe in decimal to no decimal...
  4.  
  5. s = Trim$(Text2.Text)
  6.  
  7. If s = Format(Trim(Val(s))) Then
  8.     i = s
  9.     MsgBox "Looks like a number - hooray!" & Val(s) & Val(i)
  10. ' so "s" will be displayed as a interger or a decimal value
  11. ' and "i" will only display interger value.. (decimal is converted to interger long
  12. ' where i need for calculation...
  13. ' so is what i explained correctly written in this code???
  14.  
  15. Else
  16.   MsgBox "invalid..."
  17.  
  18. End If
  19.  
  20. End Sub
  21.  
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...
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. On Error GoTo err
  3.     Dim num As Long
  4.     num = CDbl(Text3.Text)
  5.     MsgBox "Numeric" & Val(num)
  6.     Exit Sub
  7. err:
  8.     MsgBox "Invalid Input"
  9. End Sub
  10.  
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...

Expand|Select|Wrap|Line Numbers
  1. Private Sub text1_KeyPress(KeyAscii As Integer)
  2.  
  3.  If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) Then
  4.         KeyAscii = 0
  5.     End If
  6.  
  7. End Sub
  8.  
smile always...
will*
Nov 28 '07 #17
lotus18
866 512MB
Hi LeWaltz

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

Rey Sean
Nov 28 '07 #18
jaz215
55
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.

Expand|Select|Wrap|Line Numbers
  1. str as string = "10.10"
  2. x as double 
  3. x = cDbl(cstr)
* x would now be 10.10 but not a string anymore
also works for cInt for integer and others stuff
Nov 28 '07 #19
Mohan Krishna
115 100+
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!
Nov 28 '07 #20
Killer42
8,435 Expert 8TB
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(MyString)) 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.
Nov 29 '07 #21
jaz215
55
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(MyString)) 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
Nov 29 '07 #22
Killer42
8,435 Expert 8TB
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.
Nov 29 '07 #23
Mohan Krishna
115 100+
You could try something like this...
Expand|Select|Wrap|Line Numbers
  1. Dim s As String
  2. Const Digits as String = "0123456789"
  3. s = Trim$(text1.Text)
  4. If s = Format(Val(s)) Then
  5.   MsgBox "Looks like a number - hooray!"
  6. :
  7. :
  8. End If
  9.  
Hi Killer!

Excellent Code!
ThanQ
Nov 29 '07 #24
Killer42
8,435 Expert 8TB
Hi Killer!

Excellent Code!
Thank you.
Nov 29 '07 #25
once again thank you all... :) learned...

smile always...
will*
Dec 4 '07 #26

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Danny | last post by:
I have a small <div> element which contains two text blocks - one within <h5> tags and the other within <p> tags. I don't want any extra line spacing between elements so use the display:inline...
3
by: JimC | last post by:
I pose a question here concerning what I think is a classic computer problem, but I don't know of an algorithm for solving it. It resembles the Towers of Hanoi prolem. A few matrices follow. ...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
1
by: Dario | last post by:
Hi Everybody! I have a report . There are some text-boxes on the report. Text-boxes' values come from the table. In the text-boxes I must let about 5 mm free space between digits.I mean, more...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
13
by: athiane | last post by:
I want a way to parse out all function names that appear in a couple of C files. When the parsing logic finds a function name in a file, it should print out the Function name, line number and file...
10
by: phil-news-nospam | last post by:
I have a table with 3 columns in 1 row. I want to increase the spacing _between_ the columns (gutter) _without_ increasing the spacing between those columns and the table itself. Is there a way...
3
by: Allen | last post by:
I've got a control that you can resize the contents of one of the text fields inside it. When the contents are resized to smaller than the text, I remove some of the end of the text and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.