 |
| Access VBA Discuss using VBA for Access programming. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 19th, 2006, 06:44 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
force a valid entry befor exiting
Hi,
found another little problem. What I would like is that in a date field on a form you can not enter a date in the future. If you enter a date in the future the user should receive a messagebox. This I can do and it works.
Now what I would like is that after the user presses 'enter' on that message box, the active field on the form becomes the date field again (so the user has to enter a valid date).
The code I'm using is:
Private Sub Datum_AfterUpdate()
If Me.Datum > Date Then
MsgBox "De ingevoerde datum bevindt zich in de toekomst!" & vbCrLf & "Gelieve een andere datum in te voeren." _
, vbExclamation, "Ongeldige invoer"
DoCmd.GoToControl "Datum"
End If
End Sub
but with this code, the active field when the user presses 'enter' on the message box becomes the next field in the tab order. What do I have to change to my code to make this work???
Thanks
|
|

May 19th, 2006, 09:23 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
Use the .SetFocus method. That is, Me.Datum.SetFocus
Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
|
|

May 19th, 2006, 09:42 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Where should I insert this me.datum.setfocus command?
Should I replace the docmd.gotocontrol "datum" with this command or should I add this somewhere else in the code. Sorry for asking, but I'm trying to understand how this works, because it is the first time I'm making something in access...
|
|

May 19th, 2006, 10:07 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
Yes, replace your DoCmd statement with the statement I suggested.
Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
|
|

May 19th, 2006, 10:19 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
If I change the code to
Private Sub Datum_AfterUpdate()
If Me.Datum > Date Then
MsgBox "De ingevoerde datum bevindt zich in de toekomst!" & vbCrLf & "Gelieve een andere datum in te voeren." _
, vbExclamation, "Ongeldige invoer"
me.datum.setfocus
End If
End Sub
it does also not work. It just gives me the messagebox and then moves to the next field of the form...
|
|

May 19th, 2006, 11:16 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
How about doing this at the table design level with data validation stating that the date entered cannot be later than today?
mmcdonal
|
|

June 20th, 2006, 09:31 PM
|
|
Registered User
|
|
Join Date: Jun 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
There is a bug in the setfocus feature of Access 2003. You have to set the focus to the prior field on your form, and the to the field you really want to set the focus on.
Me!MINREQFLAG.SetFocus
Me!EmailAddress.SetFocus
Also, me.undo is hosed. Use this code instead:
DoCmd.RunCommand acCmdUndo
Karen S. Long
MarkXX Consulting, LLC
103 Clearview Drive
McMurray, PA 15317
(724)942-4831
|
|

June 22nd, 2006, 09:52 AM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
Here's the fastest solution that I've found and I use it in
tons of application using Access VBA. I'll cite your example:
Private Sub Datum_Exit(Cancel As Integer)
If Me.Datum > Date Then
MsgBox "You may not enter a date after the current date", vbExclamation, "ERROR"
DoCmd.CancelEvent
End If
End Sub
They can't get out of the field until they either:
#1 Enter a valid date
#2 Press the [Esc] key to get out of the field.
Hope that helps.
Warren
|
|
 |