Hi There,

I need some help in VB 6. I have a textbox wherein I need to validate the data entered after the TAB key is pressed. Using KeyDown, KeyUp and KeyPress does not get me the KeyAscii of the Tab Key. It works fine with the Enter Key but the Tab Key doesn't work. Is there any function where I could get the LastKey Pressed so I could place a check on the TAB key in my Validate event. Or is there any other possible way I could try out?

you don't need to get the ascii code of TAB to trap the key. just put your code inside the VALIDATE or LOSTFOCUS event of ur textbox. whenever you press the tab key the code will be automatically fired.

Try this..

Private Sub Text1_LostFocus()
If IsNumeric(Text1.Text) Then MsgBox "You entered a number"
End Sub

you don't need to get the ascii code of TAB to trap the key. just put your code inside the VALIDATE or LOSTFOCUS event of ur textbox. whenever you press the tab key the code will be automatically fired.

Hi Shouvik_The_Expert_Coder

I tried ur option using the validate key but now even if I click on any label or frame the validate option fires. I can set the causesvalidation to false for the command buttons and picture buttons but the labels and frames do not have any causesvalidation event.

My case is like this. I have a table where I am storing some data. The first textbox in the form I use is to enter the code. I need to validate this code and check if this exists in the table or not. So when I press the Enter Key, in the keypress option I check if the record exists. If not then I need to add a New Record using recordset.AddNew. . i.e.

If KeyAscii = 13 then
recordset.movefirst
recordset.find "ACOD = " & "'" & trim(cACod.text) & "'"
'Where cACod.text is the textbox which requires to be validated.
if recordset.EOF then
recordset.AddNew
else
msgbox "Record Exists"
exit sub
endif

The problem arises when I press the Tab Key. The Keypress does not get activated at this instance. Even if I place this condition in the validate option and if I press any label or frame on the form, the validate option gets activated.

So if I can trap the Tab key I can use this keyascii to run the validate condition the same way I use the Enter keyascii. Any clues??

one thing you can do. just put a if condition inside your validate event before the code for checking existence of the textbox's value. check if the textbox has null value in it or not. if it don't then fire the code else just exit the sub. like this :-

private sub text1_validate()
if trim(text1.text)<>"" then
   <your code for checking into database>
else  <as the textbox is empty, so there is no need to check for the code into the database
    exit sub
end if
end sub

regards
Shouvik

one thing you can do. just put a if condition inside your validate event before the code for checking existence of the textbox's value. check if the textbox has null value in it or not. if it don't then fire the code else just exit the sub. like this :-

private sub text1_validate()
if trim(text1.text)<>"" then
   <your code for checking into database>
else  <as the textbox is empty, so there is no need to check for the code into the database
    exit sub
end if
end sub

regards
Shouvik

Hi Shouvik,

Thanks for ur reply. Actually I was doing just that. Only thing was that I had a MsgBox which kept firing when I used to click on a label or a frame. My code read as below

private sub text1_validate(cancel as Boolean)
if trim(text1.text)="" then <as the textbox is empty, so there is no need to check for the code into the database
cancel = true
MsgBox "Enter a Unique Code", vbInformation
else
<your code for checking into database>
end if
end sub

So whenever I used to click on any label or frame where the causesvalidaion cannot be set it used to trigger the MsgBox. So what I've done now is removed the MsgBox and just added an Exit Sub. This should do just fine.

Thanks once again.

This question has already been answered. Start a new discussion instead.