KeyPress Problems

D

Daniel

Hello,

I am trying to intercept the space bar key entry in a textbox to insert
special characters.

Private Sub ClientEmail_KeyPress(KeyAscii As Integer)
Dim strEmail As String

'Simplify e-mail address entry using spacebar
If KeyAscii = 32 Then
strEmail = Me.ClientEmail
If InStr(strEmail, "@") = 0 Then
Me.ClientEmail.Value = strEmail & "@"
Else
Me.ClientEmail.Value = strEmail & "."
End If
End If
End Sub

I'm experiencing 2 problems.
1- an error occurs at line 'strEmail = Me.ClientEmail' stating that it is
null even after entering a string value. I can get around this issue by
placing a me.dirty=false before the line, but I still don't understand the
underlying problem as there is a string value in the textbox when the error
is triggered?

2- My real issue, is that after this code runs, the textbox gets blanked?!?!
If I do an undo the proper text appear. Why oh Why is my code blanking the
entry altogether?

Daniel P
 
D

Daniel

With a little fiddling I figured out that I need to use Me.ClientEmail.Text
rather than Me.ClientEmail.

However, i now get the extra characters to show but I also get the space
(Chr(32)). How can I eliminate it from the entry. I want the spcae to
trigger the action but not show in the entry.

Thank you,

Daniel P
 
D

Daniel

Problem solved. the saying is true: "you learn something new every day!"

The solution is below should anyone ever be trying to solve this same issue.

Private Sub ClientEmail_KeyPress(KeyAscii As Integer)
Dim strEmail As String

'Simplify e-mail address entry using spacebar
If KeyAscii = 32 Then
strEmail = Me.ClientEmail.Text
If InStr(strEmail, "@") = 0 Then
Me.ClientEmail.Text = strEmail & "@"
Else
Me.ClientEmail.Text = strEmail & "."
End If
DoCmd.CancelEvent 'Stops the space from being placed in the string
End If
End Sub
 
D

Dirk Goldgar

In
Daniel said:
DoCmd.CancelEvent 'Stops the space from being placed in the
string

An interesting solution. The more common solution is to set the event
procedure's KeyAscii argument to zero:

KeyAscii = 0
 

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