C
chris
I need the code for not allowing special characters in our data entry form.
Example: - < > ?
Thanks in advance for anyone that helps.
Example: - < > ?
Thanks in advance for anyone that helps.
I need the code for not allowing special characters in our data entry form.
Example: - < > ?
Thanks in advance for anyone that helps.
KenSheridan via AccessMonster.com said:Put the following in the control's KeyPress event procedure:
Const EXCLUDELIST = "-<>?"
If InStr(EXCLUDELIST, Chr(KeyAscii)) > 0 Then KeyAscii = 0
Just add whatever other characters you want excluding to the EXCLUDELIST
constant.
Ken Sheridan
Stafford, England
--
Message posted via AccessMonster.com
.
John W. Vinson said:There are a couple of ways to do this. One would be to put a validation rule
on the table field restricting the input to the set of valid characters;
another would be to use code in the textbox's KeyPress event:
Private Sub textboxname_KeyPress(KeyAscii As Integer)
Dim strChr As String
strChr = Chr(KeyAscii) ' convert ASCII code to string
If InStr(strChr, "-<>|\/") > 0 Then ' see if it's in the list of baddies
KeyAscii = 0 ' cancel the keystroke
End If
End Sub
This is my fault for not thinking about this before hand and I apologize, is
there a code to turn those "Baddies" to null if they copy them into the field
and tab out?
In the control's AfterUpdate event procedure execute some code
which calls the Replace function repeatedly to replace each of the
offending characters with a zero-length string, "".
[email protected] said:Ok, I think I've almost got this. This is only my second week messing around
with coding. So here's what I've got.
Private Sub company_BeforeUpdate(Cancel as Integer)
Dim Badlist As Variant
Dim i As Integer
Dim strIn As String
strIn = Me!textboxname
Badlist = Array("<", ">", "?", "/", "\")
For i = 0 to Ubound(Badlist) - 1
If InStr(strIn, Badlist(i)) > 0 Then
strIn = Replace(strIn, Badlist(i), "("<", ">", "?", "/", "\")") '
replace with a null string
End If
Next i
Me!company = strIn
End Sub
It keeps giving me an error and bring me to "replace". I am assuming it has
something to do with "replace with a null string"
Thanks Again John
-Chris
strIn = Me!company *I have this too
Private Sub company_BeforeUpdate(Cancel as Integer)
Dim Badlist As Variant
Dim i As Integer
Dim strIn As String
strIn = Me!textboxname
Badlist = Array("<", ">", "?", "/", "\")
For i = 0 to Ubound(Badlist) - 1
If InStr(strIn, Badlist(i)) > 0 Then
strIn = Replace(strIn, Badlist(i), "("<", ">", "?", "/", "\")") '
replace with a null string
End If
Next i
Me!company = strIn
End Sub
I never replace with "", though -- I always use vbNullString.
John W. Vinson said:Well, that isn't what I suggested. What I suggested would work fine -
replacing the current element of the Badlist array with an empty string "".
You changed it and now it won't work.
The text "replace with a null string" is after a ' character, which makes it a
comment in the VBA code. The comment is of course optional and can be omitted.
Replace that line with
strIn = Replace(strIn, Badlist(i), "")
What the code will do is loop through all the elements in the Badlist array -
using i as a subscript to check elements 0, 1, 2 and so on; it wil then
replace that character with an empty string "", so a text string
3<5
will be converted to
35
They're synonyms - any reason to prefer the builtin constant to the literal
constant? I guess it's easier to read, but is it faster, or just cleaner code?
[email protected] said:Ok, I think I've almost got this. This is only my second week messing
around
with coding. So here's what I've got.
Private Sub company_BeforeUpdate(Cancel as Integer)
Dim Badlist As Variant
Dim i As Integer
Dim strIn As String
strIn = Me!textboxname
Badlist = Array("<", ">", "?", "/", "\")
For i = 0 to Ubound(Badlist) - 1
If InStr(strIn, Badlist(i)) > 0 Then
strIn = Replace(strIn, Badlist(i), "("<", ">", "?", "/", "\")") '
replace with a null string
End If
Next i
Me!company = strIn
End Sub
It keeps giving me an error and bring me to "replace". I am assuming it
has
something to do with "replace with a null string"
Thanks Again John
-Chris
Here's what I'm getting with that code:
Run-time error '2115':
The macro or function set to the BeforeUpdate or validationRule property for
this field is preventing Microsoft Office Accesss from saving the data in the
field.
John W. Vinson said:Please post your current code and the validation rule.
It's likely that you don't want *both* the code and the validation rule - they
are two ways to accomplish the same task; it may be that the belt is getting
in the way of fastening the suspenders.
They're synonyms - any reason to prefer the builtin constant to
the literal constant? I guess it's easier to read, but is it
faster, or just cleaner code?
See:
http://groups.google.com/group/comp.databases.ms-access/browse_frm/
thread/791b330af688f878
It's another example of Michka's philosophy of trying to squeeze
every last iota of performance out of the software instead of
fixing the bugs . It's also an example of nearly meaningless
pedantry.
In loops, it really can make a difference.
Here are both codes for this field:
Private Sub company_AfterUpdate()
Me!company = UCase(Me!company)
End Sub
Private Sub company_BeforeUpdate(Cancel As Integer)
Dim Badlist As Variant
Dim i As Integer
Dim strIn As String
strIn = Me!company
Badlist = Array("<", ">", "?", "/", "\")
For i = 0 To UBound(Badlist) - 1
If InStr(strIn, Badlist(i)) > 0 Then
strIn = Replace(strIn, Badlist(i), "") ' replace with a null string
End If
Next i
Me!company = strIn
End Sub
It's faster because the memory is already allocated, whereas ""
requires allocating memory on the fly, each time you use it (this is
what Michael Kaplan explained to me about it, back in the days
before I really had much understanding of VBA compilation).
Doesn't really matter for a single line, but in a loop it makes a
difference. But I try to practice efficient coding even when it
doesn't matter in order to insure that I have the habit of doing it
efficiently when it *does* matter.
[email protected] said:Ok, I think I've almost got this. This is only my second week messing
around
with coding. So here's what I've got.
Private Sub company_BeforeUpdate(Cancel as Integer)
Dim Badlist As Variant
Dim i As Integer
Dim strIn As String
strIn = Me!textboxname
Badlist = Array("<", ">", "?", "/", "\")
For i = 0 to Ubound(Badlist) - 1
If InStr(strIn, Badlist(i)) > 0 Then
strIn = Replace(strIn, Badlist(i), "("<", ">", "?", "/", "\")") '
replace with a null string
End If
Next i
Me!company = strIn
End Sub
It keeps giving me an error and bring me to "replace". I am assuming it
has
something to do with "replace with a null string"
Thanks Again John
-Chris