** tab key **

MS_Access_Amature

Registered User.
Local time
Today, 12:19
Joined
Nov 10, 2010
Messages
56
The user can't press tab until he types something into the textbox..how do I do this??

This is what i have..

Code:
Private Sub CompanyName_KeyPress(KeyAscii As Integer)
On Error GoTo MyErrorControl

    If KeyCode = 9 and Me![CompanyName].Value = "" Then
        KeyCode = 0
[INDENT]msgbox "Please enter a Company Name"[/INDENT]

     End If
    
    Exit Sub
MyErrorControl:
    Select Case Err.Number
    Case 0
        Resume Next
    Case Else
        MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "CompanyName_KeyPress ERROR"
        Resume Next
    End Select
End Sub


What do I need to fix and in what event do i need to put this??
 
Change this:

If KeyCode = 9 and Me![CompanyName].Value = "" Then


to this:

If KeyCode = 9 and Len(Me!CompanyName & vbNullString) = 0 Then
 
I tired putting Len(Me!CompanyName & vbNullString) = 0 and also turned on the Key Preview property but it's still letting me press Tab without typing anything.
 
Use the same code but use the KeyDown event instead as this one you currently have doesn't have KeyCode it has KeyAscii and if you move the code to that KeyDown event it should work (I've tested).
 
IT SURE WORKS!! THANKS BOB

Code:
Private Sub CompanyName_KeyDown(KeyCode As Integer, Shift As Integer)
On Error GoTo MyErrorControl

    If KeyCode = 9 And Len(Me![CompanyName] & vbNullString) = 0 Then
        KeyCode = 0
        MsgBox "Please enter a company name", vbExclamation, "Required Data"
    End If

    Exit Sub
MyErrorControl:
    Select Case Err.Number
    Case 0
        Resume Next
    Case Else
        MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "CompanyName_KeyDown ERROR"
        Resume Next
    End Select
End Sub
 

Users who are viewing this thread

Back
Top Bottom