Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
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
 
Old May 19th, 2006, 06:44 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

 
Old May 19th, 2006, 09:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Use the .SetFocus method. That is, Me.Datum.SetFocus

Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old May 19th, 2006, 09:42 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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...

 
Old May 19th, 2006, 10:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Yes, replace your DoCmd statement with the statement I suggested.

Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old May 19th, 2006, 10:19 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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...

 
Old May 19th, 2006, 11:16 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

How about doing this at the table design level with data validation stating that the date entered cannot be later than today?


mmcdonal
 
Old June 20th, 2006, 09:31 PM
Registered User
 
Join Date: Jun 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old June 22nd, 2006, 09:52 AM
Authorized User
 
Join Date: Oct 2003
Posts: 75
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Enter as a valid entry in a textbox (VB) Grimgore BOOK: Visual Basic 2008 Programmer's Reference ISBN: 978-0-470-18262-8 2 February 9th, 2009 04:37 PM
How to access exiting WinMain BrianWren Pro VB 6 11 April 3rd, 2008 02:07 PM
prompt user before exiting sandeep Javascript How-To 1 September 5th, 2005 01:42 PM
A better way of exiting jaucourt VB.NET 2002/2003 Basics 1 March 31st, 2004 07:44 PM
exiting MyAccount.aspx robert BOOK: ASP.NET Website Programming Problem-Design-Solution 2 July 17th, 2003 11:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.