Detecting a space in a string

G

Guest

In a text field I can potentially have at least one space in a string. What
code can I use to detect this space, bring up an error message and then
delete the text.

Cheers
 
G

Guest

Alternatively, I can have a message error when the space bar is first pressed.

which ever is easiest.
 
M

missinglinq via AccessMonster.com

This does both! Pops up a warning and doesn't allow the spacebar to be used.

Private Sub YourTextBox_KeyPress(KeyAscii As Integer)
If KeyAscii = 32 Then
KeyAscii = 0
MsgBox "Spaces Are Not Allowed In This Field!"
End If
End Sub
Alternatively, I can have a message error when the space bar is first pressed.

which ever is easiest.

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000

Message posted via AccessMonster.com
 
G

Guest

As Ted would say, "Excellent, dude!"



missinglinq via AccessMonster.com said:
This does both! Pops up a warning and doesn't allow the spacebar to be used.

Private Sub YourTextBox_KeyPress(KeyAscii As Integer)
If KeyAscii = 32 Then
KeyAscii = 0
MsgBox "Spaces Are Not Allowed In This Field!"
End If
End Sub


--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000

Message posted via AccessMonster.com
 
M

missinglinq via AccessMonster.com

Glad it helped!

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000

Message posted via AccessMonster.com
 
P

Peter Hibbs

Scubadiver,

If you don't want a Space entered into the field why don't you just
prevent the field accepting a Space character in the first place by
placing this code :-

If KeyAscii = vbKeySpace Then KeyAscii = 0

in the KeyPress event of the text box. Save the user having to see and
dismiss error messages.

Peter Hibbs.
 
A

Allen Browne

That's a good idea.

In case someone pastes the value in, you might also want to use the
control's AfterUpdate event procedure to remove any space, like this:

Private Sub Text0_AfterUpdate()
Dim strResult As Variant

With Me.[Text0]
If Not IsNull(.Value) Then
strResult = Replace(.Value, " ", vbNullString)
If strResult = vbNullString Then
.Value = Null
Else
.Value = strResult
End If
End If
End If
 
D

Douglas J. Steele

Forward slash is 47, & is 38. From the Immediate Window (Ctrl-G):

?Asc("/")
47
?Asc("&")
38
?Asc(" ")
32

If your intent is to eliminate all 3 possibilities, I'd recommend

Private Sub YourTextBox_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 32, 38, 47
KeyAscii = 0
MsgBox "Invalid character (" & Chr$(KeyAscii) & ") for this field!"
End Select
End Sub
 
G

Guest

thanks

my learning curve goes ever up.

Douglas J. Steele said:
Forward slash is 47, & is 38. From the Immediate Window (Ctrl-G):

?Asc("/")
47
?Asc("&")
38
?Asc(" ")
32

If your intent is to eliminate all 3 possibilities, I'd recommend

Private Sub YourTextBox_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 32, 38, 47
KeyAscii = 0
MsgBox "Invalid character (" & Chr$(KeyAscii) & ") for this field!"
End Select
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top