Don't want <Enter> to go to next record

M

markmarko

I've got a simple form for user to login. It only has 2 bound textboxes (User
and Word (password)), plus an Submit button. (I know this doesn't count for
'real' security, but it works for our purposes.)

I would like the user to be able to click Submit button, or hit <Enter>
after entering their info, which will then send their username to the
underlying table, close the form, and open the SalesOrder form.

Currently, typing <Enter> moves to the next record. And I'm having a hard
time getting it to send the data to the table.

I'm looking for suggestions in terms of Events to use, and code....

Here's the code I have so far.

Private Sub Form_BeforeUpdate(Cancel As Integer)
ValidateUser
End Sub

Private Sub SubmitButton_Click()
ValidateUser
End Sub

Public Sub ValidateUser()
Dim UsernameID As Integer

If Not (IsNull(Me!User)) Then
UsernameID = Me!User

If Word = DLookup("pWord", "pWords", "AssociatedContractor = " & Me!User)
Then
TempVars![TempUser] = UsernameID '
DoCmd.RunCommand acCmdSaveRecord 'this causes an error because of
BeforeUpdate
DoCmd.CancelEvent 'can't remember why I included this,
but there was a reason
DoCmd.Close
DoCmd.OpenForm ("Entry-SalesOrder")
Else
MsgBox ("Password Incorrect")
Me!User = UsernameID
Me!Word = ""
Me!Word.SetFocus
DoCmd.CancelEvent
End If

Else
MsgBox ("No PromoCode entered.")
Me!Word = ""
Me!User.SetFocus
DoCmd.CancelEvent
End If
End Sub

Private Sub SubmitButton_GotFocus()
ValidateUser
End Sub
 
J

Jeanette Cunningham

Markmarko,
there are a couple of property settings to help with the behaviour of the
enter key.
1. on the form's property dialog, other tab, set the Cycle property to
Current Record.
2. on the submit button's property dialog, other tab, set the Default
property to Yes. Depending on how your form is set up, this will often allow
pressing enter to make the button run its click event.
3. I will have a look at the code problems later.

Jeanette Cunningham
 
J

Jeanette Cunningham

MarkMarko,
Use the form's Before Update event to run the validateuser function.
Use the click of the submit button to force the form to save the record.
Saving the record forces the before update event to run.
Here is some suggested code. It will not work without some debugging and
tweaking, but it is a good start for what you are trying to do.
If validateuser fails, the form will not open the Sales order form.
There isn't any code to allow the user to cancel and close the form - maybe
you will want a button to do that.
I haven't included the bit for No Promo Code Entered - there wasn't enough
info for me to use.

---------------------------------
Private Sub SubmitButton_Click()
If Me.Dirty = True Then
Me.Dirty = False
DoCmd.OpenForm ("Entry-SalesOrder")
End If
End Sub
---------------------------------

---------------------------------
Private Sub Form_BeforeUpdate(Cancel As Integer)
If ValidateUser = False Then
Cancel = True
Me!User = UsernameID
Me!Word = ""
Me!Word.SetFocus
End If
End Sub
--------------------------------


-------------------------------
Public Function ValidateUser() As Boolean
Dim UsernameID As Integer

If Not IsNull(Me!User) Then
UsernameID = Me!User

If Word = DLookup("pWord", "pWords", "AssociatedContractor = " &
Me!User) Then
TempVars![TempUser] = UsernameID
ValidateUser = True
Else
ValidateUser = False
MsgBox ("Password Incorrect")
End If
End If

End Function
------------------------------

Jeanette Cunningham

markmarko said:
I've got a simple form for user to login. It only has 2 bound textboxes
(User
and Word (password)), plus an Submit button. (I know this doesn't count
for
'real' security, but it works for our purposes.)

I would like the user to be able to click Submit button, or hit <Enter>
after entering their info, which will then send their username to the
underlying table, close the form, and open the SalesOrder form.

Currently, typing <Enter> moves to the next record. And I'm having a hard
time getting it to send the data to the table.

I'm looking for suggestions in terms of Events to use, and code....

Here's the code I have so far.

Private Sub Form_BeforeUpdate(Cancel As Integer)
ValidateUser
End Sub

Private Sub SubmitButton_Click()
ValidateUser
End Sub

Public Sub ValidateUser()
Dim UsernameID As Integer

If Not (IsNull(Me!User)) Then
UsernameID = Me!User

If Word = DLookup("pWord", "pWords", "AssociatedContractor = " & Me!User)
Then
TempVars![TempUser] = UsernameID '
DoCmd.RunCommand acCmdSaveRecord 'this causes an error because of
BeforeUpdate
DoCmd.CancelEvent 'can't remember why I included this,
but there was a reason
DoCmd.Close
DoCmd.OpenForm ("Entry-SalesOrder")
Else
MsgBox ("Password Incorrect")
Me!User = UsernameID
Me!Word = ""
Me!Word.SetFocus
DoCmd.CancelEvent
End If

Else
MsgBox ("No PromoCode entered.")
Me!Word = ""
Me!User.SetFocus
DoCmd.CancelEvent
End If
End Sub

Private Sub SubmitButton_GotFocus()
ValidateUser
End Sub
 
M

markmarko

Thank you Jeanette! The code needed a little modification to function, so
I'll post it here.


Public Sub Form_BeforeUpdate(Cancel As Integer)
If ValidateUser = False Then
Cancel = True
Me!User = UsernameID
Me!Word = ""
Me!Word.SetFocus
End If
End Sub

Private Sub SubmitButton_Click()
If ValidateUser = True Then
DoCmd.Close
DoCmd.OpenForm ("Entry-SalesOrder")
End If
End Sub

Public Function ValidateUser() As Boolean
Dim UsernameID As Integer
If Not (IsNull(Me!User)) Then
UsernameID = Me!User
If Word = DLookup("pWord", "pWords", "AssociatedContractor = " & Me!User)
Then
TempVars![TempUser] = UsernameID
ValidateUser = True
Else
MsgBox ("Password Incorrect")
Me!User = UsernameID
Me!Word = ""
Me!Word.SetFocus
ValidateUser = False
End If

Else
MsgBox ("No PromoCode entered.")
Me!Word = ""
Me!User.SetFocus
DoCmd.CancelEvent
End If
End Function
 

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