Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with KeyPress event and KeyAscii 2

Status
Not open for further replies.

MikeBronner

Programmer
May 9, 2001
756
US
What I am trying to do is create a textbox validation routine for dates. This routine should automatically insert the slashes while the user is typing in the dates.

I thought I might try this best using the KeyPress event on the specified textbox, but it doesn't quite seem to work. Here's the code I'm using:

Prtivate Sub txtBox_keyPress(Index As Integer, KeyAscii As Integer)
Select Case (len(txtBox.Text))
Case 0, 2, 3, 5, 6, 7, 8, 9
KeyAscii = KeyAscii
Case 1, 4
KeyAscii = KeyAscii
txtBox.Text = txtBox.Text & "/"
End Select
End Sub

For some reason the function will not insert the "/" correctly, or it will not insert the KeyAscii as desired after the shlash has been inserted (depending on the sequence of statements). Either way, it won't insert a slash after the second and fifth numbers have been typed.

Any ideas? Best Regards and many Thanks!
Michael G. Bronner X-)

"The problem with the world is that everyone is a few drinks behind." Humphrey Bogart

 
Hello:
This might be helpful although its not exactly what you want.

on the lostfocus event

if isnumeric(text1.text) then
text1.text=format(text1.text,"mm/dd/yy")
else
msgbox"This is not a valid date!"
end if

good luck
 
Hmm, not exactly what I am looking for. But thanks anyway.
I have locked all keys except the numeric and backspace keys, so that the user won't be able to input any invalid dates in the first place.

Basically all I need is for routine to add the "/" where they belong as the user types in the numbers - a typeof autoformatting, I guess. Best Regards and many Thanks!
Michael G. Bronner X-)

"The problem with the world is that everyone is a few drinks behind." Humphrey Bogart

 
Or let me rephrase my question like this:

How can I insert a character or a string into the corresponding textbox of a KeyPress event? Best Regards and many Thanks!
Michael G. Bronner X-)

"The problem with the world is that everyone is a few drinks behind." Humphrey Bogart

 
I managed to find a somewhat cumbersome workaround using the Change event. Each time the textbox changes, it will check for the length of the text: when there are either 2 or 5 characters present, it will insert a "/".

This works for now, I suppose. Does anyone have any shorter/better way of accoomplishing this? Best Regards and many Thanks!
Michael G. Bronner X-)

"The problem with the world is that everyone is a few drinks behind." Humphrey Bogart

 
If you still want to do it in the Keypress Event, make minor modificationss as below:

Private Sub txtBox_keyPress(KeyAscii As Integer)
Select Case (Len(txtbox.Text))
Case 0, 2, 3, 5, 6, 7, 8, 9
KeyAscii = KeyAscii
Case 1, 4
txtbox.Text = txtbox.Text & Chr(KeyAscii) + "/"
txtbox.SelStart = Len(txtbox.Text)
KeyAscii = 0
End Select
End Sub
 
Hello!
You can use instead of textbox MskEditBox.
There you have property Mask, where you can put any mask you need.

Irina.
 
If Text1.SelStart = 2 Then
Text1.Text = Text1.Text & "/"
Text1.SelStart = 4
End If

If Text1.SelStart = 5 Then
Text1.Text = Text1.Text & "/"
Text1.SelStart = 7
End If

Catrina
 
Thanks Catrina and strongm, I got it to work :).

I declined using the masked edit controls on this one, but I'll try them another time.

Thanks! Best Regards and many Thanks!
Michael G. Bronner X-)

"The problem with the world is that everyone is a few drinks behind." Humphrey Bogart

 
I went ahead and tried the Masked Edit control, but how do you bind it to a recordset, as there is no .Text property? Best Regards and many Thanks!
Michael G. Bronner X-)

"The problem with the world is that everyone is a few drinks behind." Humphrey Bogart

 
There is such property as mask.
This is example how to use it:

msk1.Mask = "99/99999/999"
 
Sure, I can set the Mask property, but that won't bind it to a recordset. Best Regards and many Thanks!
Michael G. Bronner X-)

"Life is too Short to Drink Cheap Beer"
 
Maskeditbox has the same properties as textbox such as
Datasource, datafield,...
You don't have to have any problems with recordset.
 
if you have
msk1.Mask = "99/99999/999"
then you cantake data from maskeditbox:

strParcel = Mid(Trim(msk1.Text), 1, 2) & Mid(Trim(msk1.Text), 4, 5) & Mid(Trim(msk1.Text), 10, 3)

Using Mid you can put data into maskeditbox and it doesn't matter from where this data is from recordset or anywhere else.
 
True, that would work, but it would almost be as much work as validating my input using a regular textbox :) hehe

Thanks though! Best Regards and many Thanks!
Michael G. Bronner X-)

"Life is too Short to Drink Cheap Beer"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top