473,386 Members | 1,830 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,386 software developers and data experts.

Don't allow text to be entered in a text box

15
I have a text box that I only want a user to be able to enter integers. I have it so that if a number with a decmail place is enter a error message comes up. But when i was testing the code, I entered a letter in and i got a runtime error.. I was wondeirng what I would use give an error if a letter what typed in.
Dec 22 '09 #1
7 3850
debasisdas
8,127 Expert 4TB
you need to check the ascii value of integers and handle the same in key press event.
Dec 23 '09 #2
Guido Geurs
767 Expert 512MB
dear,

I hope this will help

Expand|Select|Wrap|Line Numbers
  1. Private Sub Text1_Change()
  2.     With Text1
  3. '§ check for letters
  4.         If Not IsNumeric(.Text) And (.Text) <> "" Then
  5.             MsgBox ("Enter a NUMBER !")
  6.         Else
  7. '§ check for DOT or COMMA
  8.             If InStr(.Text, ".") Or InStr(.Text, ",") Then
  9.                 MsgBox ("Enter an INTEGER value !")
  10.             Else
  11. '§ enter your code....
  12. '            MsgBox ("This is OK")
  13.             End If
  14.         End If
  15.     End With
  16. End Sub
br,
Dec 23 '09 #3
debasisdas
8,127 Expert 4TB
How that restricts the entry of characters ?
Dec 24 '09 #4
Guido Geurs
767 Expert 512MB
dear,

The "If Not IsNumeric(.Text) ..." checks if the string in text1 is a value.
From the moment you type a character, it gives a textbox with a warning.
Just try it.(see attachment)
Notice: I have also entered 2 commands which delete the last wrong character and sets the cursor to the last position.

br,
Attached Files
File Type: zip Dont allow text to entered in textbox.zip (1.7 KB, 142 views)
Dec 24 '09 #5
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtBoxName_Change()
  2.     With txtBoxName
  3. '§ check for letters
  4.         If Not IsNumeric(.Text) And (.Text) <> "" Then
  5.             MsgBox ("Enter a NUMBER !")
  6. '§ clear the last character
  7.             .Text = Left(.Text, Len(.Text) - 1)
  8. '§ set the cursor at the end of the string
  9.             .SelStart = Len(.Text)
  10.         Else
  11. '§ check for DOT or COMMA
  12.             If InStr(.Text, ".") Or InStr(.Text, ",") Then
  13.                 MsgBox ("Enter an INTEGER value !")
  14.             Else
  15. '§ enter your code....
  16. '            MsgBox ("This is OK")
  17.             End If
  18.         End If
  19.     End With
  20. End Sub
Sep 8 '13 #6
This piece of code does exactly the work. But I would like to have a Windows Sound rather than a message box. Is it possible?
Sep 8 '13 #7
'I found it from another source


'Copy the following and paste it in frm module (in declatrations division) of your project where you want the sound to be played




Expand|Select|Wrap|Line Numbers
  1. Private Declare Function MessageBeep Lib "user32" _
  2.   (ByVal wType As Long) As Long
  3. Private Const MB_ICONASTERISK = &H40&
  4. Private Const MB_ICONEXCLAMATION = &H30&
  5. Private Const MB_ICONHAND = &H10&
  6. Private Const MB_ICONINFORMATION = MB_ICONASTERISK
  7. Private Const MB_ICONMASK = &HF0&
  8. Private Const MB_ICONQUESTION = &H20&
  9. Private Const MB_ICONSTOP = MB_ICONHAND
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. 'Copy the following and paste it in the textbox_change() module
  17.  
  18.  
  19. Private Sub txtBoxName_Change()
  20.  
  21.  With txtDate1
  22. '§ check for letters
  23.         If Not IsNumeric(.Text) And (.Text) <> "" Then
  24.               Call MessageBeep(MB_ICONINFORMATION)
  25.  
  26. '§ clear the last character
  27.             .Text = Left(.Text, Len(.Text) - 1)
  28. '§ set the cursor at the end of the string
  29.             .SelStart = Len(.Text)
  30.         Else
  31. '§ check for DOT or COMMA and OTHERS
  32.             If InStr(.Text, ",") Or InStr(.Text, "$") Or InStr(.Text, "-") Or InStr(.Text, "+") Or InStr(.Text, "\") Or InStr(.Text, "*") Then
  33.  
  34.  
  35.                  Call MessageBeep(MB_ICONINFORMATION)
  36.  
  37.                   .Text = Left(.Text, Len(.Text) - 1)
  38.             Else
  39. '§ enter your code....
  40. '            MsgBox ("This is OK")
  41.             End If
  42.         End If
  43.     End With
  44. End Sub
Sep 8 '13 #8

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

Similar topics

2
by: Renie83 | last post by:
Hi I really am going crazy! I'm using VBScript, ASP, and SQL My page reminds me of a shopping cart but looking at shopping cart examples has not helped! What I have is a page that brings in...
1
by: Stuart | last post by:
This is probably a frequent question, but I can't find the answer anywhere. I'm developing a search page for a form, and since the results change with each keypress, I need to allow spaces (ie...
6
by: Cro | last post by:
Dear Access Developers, The 'Allow Additions' property of my form is causing unexpected results. I am developing a form that has its 'Default View' property set to 'Continuous Forms' and am...
0
by: antonyliu2002 | last post by:
The full code is pasted below. It looks scarily long, but it's pretty simple. It is adapted from a sample code at MSDN. If you run it, and check it out from your browser, you'll see: ...
1
by: Kermit Piper | last post by:
Hello, I have a function that lets me convert one character and throw an alert with the corresponding ASCII value, but what I am having trouble with is applying it to a text box. What I'm trying...
1
by: rickn | last post by:
Being new to VB and programming, I'm not sure how to modify the following or if required have a Select statement to NOT allow any duplicates. I'm trying not to have any duplicates in the LOTNUM_72...
5
by: cameron | last post by:
I am working on an assignment where I have to allow end-users to enter in data and to have C++ make a calculation. From there I have to set up a loop to allow users to go back and enter new data or...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
0
by: Steve | last post by:
Hi guys, I am working on a Team Process Template for TFS 2008. I have created my own work item type, which has (amongst other controls) a HTML control to hold process guidance text: <FIELD...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.