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

text field only for text - no numbers

Hi

I would like some help about the following :

I have a text field and I don't want it contains any numbers. How can I
limit
this situation ?

So far, I couldn't find literature exacly about this here in the newsgroup.

TIA
Paulo


Nov 12 '05 #1
2 6008
"Paulo Rodrigues" <am*******@netvisao.pt> wrote in message news:<ne********************@newsfront4.netvisao.p t>...
Hi

I would like some help about the following :

I have a text field and I don't want it contains any numbers. How can I
limit
this situation ?

So far, I couldn't find literature exacly about this here in the newsgroup.

TIA
Paulo


Paulo,

the online help is your FRIEND.

Private Sub Text0_KeyPress(KeyAscii As Integer)

If KeyAscii >= 48 And KeyAscii <= 57 Then
KeyAscii = 0
End If
End Sub

Just put this code in the KeyPress event of whatever text field you do
not want to contain numbers. OR you could glue thumbtacks to the
number keys on all your users keyboards...
Nov 12 '05 #2
Thought I'd add my 2 cents worth. Avoid magic numbers, especially
when you can use builtin VBA constants. The following is a little
more intuitive to read and a future developer does not have to lookup
48 and 57 to see exactly what is being blocked.

Private Sub Text2_KeyPress(KeyAscii As Integer)

'Cancel any numeric keys
If KeyAscii >= vbKey0 And KeyAscii <= vbKey9 Then
KeyAscii = 0
End If

End Sub

The above solution does not stop the user from cutting and pasting a
numeric character. Its often the small things that get you. <g> I have
a couple of generic functions, which do stop the user from pasting
banned characters and it also allows you to add a series of non
consecutive banned characters such as spaces or quotes or commas etc.
Both functions could be modified for speed but I'm lazy and they work
so I've never bothered tweaking them. Pieter's solution is much faster
but does not allow you to easily add a newly banned character for
numerous text boxes by modifying one constant in one place. I couldn't
find the functions, so I rewrote them. You should test them if you use
them and limit the length of the string to the maximum length of the
field. This will slow down enough to be painful with a huge field so
use it sparingly. You can also trap the "paste" using API calls and
check the string at this point, which will speed things up
considerably but all of this is overkill for 99% of applications.
Have fun. Can you tell I'm bored? I go sailing in two days for four
weeks so I'm just killing time at work.

'String of banned characters for text boxes
Const fstrBANNED = "0123456789"

'Maximum text box field length
Const flngMAX_LENGTH As Long = 255

Private Sub Text0_Change()
StripBannedChars Text0
End Sub

Private Sub Text0_KeyPress(KeyAscii As Integer)
CancelBannedKey KeyAscii, fstrBANNED
End Sub

Public Sub CancelBannedKey( _
ByRef rintKeyASCII As Integer, _
ByVal vstrBanned As String)

'************************************************* *******
'Purpose: Cancels the key press for all banned
' characters specified in the supplied string
'************************************************* *******

'Loop through every character in the supplied string
While Len(vstrBanned) > 0

'If the key pressed is the specified key, cancel it
If rintKeyASCII = _
Asc(Left(vstrBanned, 1)) Then rintKeyASCII = 0

'Strip the leading character
vstrBanned = Right(vstrBanned, Len(vstrBanned) - 1)

Wend 'more characters

End Sub

Private Function strStripBannedChars( _
ByVal vstrSource As String, _
ByVal vstrBanned As String) As String

'************************************************* *******
'Purpose: Strips all banned characters from the
' supplied source string
'************************************************* *******

'Loop through every character in the supplied string
While Len(vstrBanned) > 0

'Replace any occurrence of each banned
'character with an empty string
vstrSource = Replace(vstrSource, _
Left(vstrBanned, 1), vbNullString)

'Strip the leading character from the banned characters
vstrBanned = Right(vstrBanned, Len(vstrBanned) - 1)

Wend 'more characters

strStripBannedChars = vstrSource

End Function

Public Sub StripBannedChars(ByVal vtxt As TextBox, _
Optional ByVal vlngMaxLength As Long = flngMAX_LENGTH)

'************************************************* *******
'Purpose: Strips all banned characters from the supplied
' textbox. The cursor returns to the start of
' the textbox if banned characters are found.
'************************************************* *******

Dim strSource As String
Dim strStripped As String

'The source string to be tested for banned characters
strSource = vtxt.Text

'The source string with banned characters removed
strStripped = strStripBannedChars(strSource, fstrBANNED)

'If the source string has any banned characters
'remove them. This will trigger this function
'again. Beware of recursive event triggers!
If strSource <> strStripped Then
vtxt.Text = Left(strStripped, vlngMaxLength)
End If

'Finished with the local pointer
Set vtxt = Nothing

End Sub
Nov 12 '05 #3

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

Similar topics

3
by: Mark | last post by:
Hi, Im trying to validate a form, all the validating works apart from one field. This particular field must consist of the first 2 characters as letters, & the following 5 as numbers. And if it...
2
by: Michael Hager | last post by:
Hello all, I have a client that has a database that produces certificates using an access report. The certificate has a picture of the person on it. The pictures were stored by SSN number, and...
8
by: shumaker | last post by:
I see other posts where some say fields that will hold a number with leading zeros should be stored as text instead of numbers. This is very inefficient though, as a string of digit characters...
2
by: Todd_M | last post by:
I was wondering what anyone might suggest as "best practice" patterns for streaming out fixed formatted text files with C#? Let's say we get our data in a dataset table and we need to iterate over...
9
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to...
5
by: rajsa | last post by:
Using a setvalue macro, I'm trying to concatenate several fields, all of which are set as 'text', but some of which contain only numbers. (Gathering data from a form into one field, then saving this...
3
by: Klaus Brune | last post by:
Hello all, When one tabs through a form (specifically, in Firefox), all the text in a field is automatically selected. What I'm looking for is a way to put a function (in onFocus perhaps)...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.