473,320 Members | 2,117 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.

Integers

When asking a user to input a number it's easy to make sure it's higher,
lower etc then a specified amount. What I'm struggling with is if you ask
the user for a number and they enter a letter. How do you code this kind of
thing to make sure that a string is entered as a string or an integer is
entered as an integer?
I'm new to VB so I apologise for asking a question that is probably very
easy.

Jul 17 '05 #1
3 3792

"Roy Riddex" <ro**************@blueyonder.co.uk> wrote in message
news:XL**************@news-binary.blueyonder.co.uk...
When asking a user to input a number it's easy to make sure it's higher,
lower etc then a specified amount. What I'm struggling with is if you ask
the user for a number and they enter a letter. How do you code this kind of thing to make sure that a string is entered as a string or an integer is
entered as an integer?
I'm new to VB so I apologise for asking a question that is probably very
easy.

Several ways.. I usually tap into the keypress routine so assuming you have
a text box, here is how the routine will look:

Private Sub Text1_KeyPress(KeyAscii As Integer)
Call num(KeyAscii)
End Sub

Finally the routine to check for numeric only (plus I added sample for
period (in case decimal, and also backspace). Any other gets ignored:

Private Sub num(KeyAscii As Integer)
If KeyAscii = 8 Or KeyAscii = 9 Or KeyAscii = Asc(".") Then Exit Sub
KeyAscii = IIf(Chr$(KeyAscii) Like "[0-9]", KeyAscii, 0)
End Sub
Jul 17 '05 #2

"Raoul Watson" <Wa*****@IntelligenCIA.com> wrote in message
news:Hw*******************@nwrddc02.gnilink.net...

"Roy Riddex" <ro**************@blueyonder.co.uk> wrote in message
news:XL**************@news-binary.blueyonder.co.uk...
When asking a user to input a number it's easy to make sure it's higher,
lower etc then a specified amount. What I'm struggling with is if you ask the user for a number and they enter a letter. How do you code this kind of
thing to make sure that a string is entered as a string or an integer is
entered as an integer?
I'm new to VB so I apologise for asking a question that is probably very
easy.

Several ways.. I usually tap into the keypress routine so assuming you

have a text box, here is how the routine will look:

Private Sub Text1_KeyPress(KeyAscii As Integer)
Call num(KeyAscii)
End Sub

Finally the routine to check for numeric only (plus I added sample for
period (in case decimal, and also backspace). Any other gets ignored:

Private Sub num(KeyAscii As Integer)
If KeyAscii = 8 Or KeyAscii = 9 Or KeyAscii = Asc(".") Then Exit Sub
KeyAscii = IIf(Chr$(KeyAscii) Like "[0-9]", KeyAscii, 0)
End Sub

Jul 17 '05 #3
> Private Sub num(KeyAscii As Integer)
If KeyAscii = 8 Or KeyAscii = 9 Or KeyAscii = Asc(".") Then Exit Sub
KeyAscii = IIf(Chr$(KeyAscii) Like "[0-9]", KeyAscii, 0)
Two points on the above line. First, it could also be written this way...

KeyAscii = IIf(Chr$(KeyAscii) Like "#", KeyAscii, 0)

where I use the metacharacter # in place of "[0-9]". Second, there is no
need to reassign KeyAscii back to itself if the statement is True... it
retains its value unless changed. So, the above line could be replaced with
this

If Not Chr$(KeyAscii) Like "#" Then KeyAscii = 0

or, using your original test pattern

If Chr$(KeyAscii) Like "[!0-9]" Then KeyAscii = 0

(Note the exclamation point in the brackets.)

Rick - MVP
End Sub

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan Stromberg | last post by:
Does anyone have a python implementation (or python C/C+ extension) that would allow me to perform set operations (union, intersection, difference, number of elements, &c) over very large...
29
by: Chris Dutrow | last post by:
I searched around on the net for a bit, couldn't find anything though. I would like to find some code for a function where I input A Range Of Integers For example: Function( 1, 100 ); And the...
13
by: Jeff Melvaine | last post by:
I note that I can write expressions like "1 << 100" and the result is stored as a long integer, which means it is stored as an integer of arbitrary length. I may need to use a large number of...
4
by: Neal Becker | last post by:
I can do this with a generator: def integers(): x = 1 while (True): yield x x += 1 for i in integers():
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
16
by: aruna | last post by:
Given a set of integers, how to write a program in C to sort these set of integers using C, given the following conditions a. Do not use arrays b. Do not use any comparison function like if/then...
1
by: calvin | last post by:
Can anyone write a code for this? Searching a set of Integers You are given two sets of integers. S1 and S2. The size of S1 is less than sizeof S2, i.e. the number of integers in S1 is less...
7
by: Girish Sahani | last post by:
Hi, Please check out the following loop,here indexList1 and indexList2 are a list of numbers. for index1 in indexList1: for index2 in indexList2: if ti1 == ti2 and not index1 !=...
7
by: mathon | last post by:
hi, i have the following recursive function: unsigned int sum_odds(unsigned int n) { if(n==1) return 1; else
9
by: Mark Morss | last post by:
I would like to construct a class that includes both the integers and None. I desire that if x and y are elements of this class, and both are integers, then arithmetic operations between them,...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.