Results 1 to 5 of 5
  1. #1
    Alex Motilal is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Nov 2008
    Location
    Coimbatore, India
    Posts
    192

    Message Box

    I am using the following code to generate a Message Box if the Date entered is wrong:

    Private Sub DtFrom_AfterUpdate()
    Dim LResponse As Integer

    If Me.DtFrom < Me.SsnStDt Then
    LResponse = MsgBox("Enter a date between " & Me.SsnStDt & " and " & Me.SsnEndDt & vbCrLf & vbCrLf & "Want change Date?", vbYesNo, "Wrong Date")
    End If

    If LResponse = vbYes Then
    Me.DtFrom.SetFocus
    Else
    Me.DtTo.SetFocus
    End If
    End Sub

    The Message generates correctly, however on clicking the Yes Button, the cursor goes to the DtTo control instead of DtFrom control.
    I want the Cursor to set focus on the DtFrom control if Yes Button is clicked. Please help.

    Regards


    Alex

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,822
    If the date entered must be greater than another field, why offer users a choice?

    Instead of VBA, could try using the Validation Rule and Validation Text properties.


    Regardless, use BeforeUpdate event.

    Private Sub DtFrom_BeforeUpdate(Cancel As Integer)
    If Me.DtFrom < Me.SsnStDt Then
    MsgBox "Enter a date between " & Me.SsnStDt & " and " & Me.SsnEndDt, , "Wrong Date"
    Cancel = True
    End If
    End Sub
    Last edited by June7; 10-15-2017 at 09:48 AM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Alex Motilal is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Nov 2008
    Location
    Coimbatore, India
    Posts
    192
    Thanks June.
    However on selecting a Cotton Season from the Combo Box, the SsnStDt & SsnEndDt will change & these are hidden controls. The dates denotes the Season Start & End Dates. And so to see the report related to the Dialog Box Form, when a user enters a date in the control DtFrom, it should be between the said dates. The method works correctly, however, if a wrong date is entered, when the Yes button of the Message Box is clicked, the cursor should focus on the DtFrom control to enable the user to enter the correct date & not go to the next control.
    Alex

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Shot in the dark, but try setting focus elsewhere and then where you really want it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,822
    Asking again, if date is not acceptable why offer users a choice? Just tell them they must correct and keep focus on the DtFrom textbox.

    Validation Rule and Validation Text properties should accomplish that as well as the BeforeUpdate event.

    Did you try the BeforeUpdate event and suggested code? Even if you want to give them a choice, use BeforeUpdate event.

    Code:
    Private Sub DtFrom_BeforeUpdate(Cancel As Integer)
    If Me.DtFrom < Me.SsnStDt Then
        If MsgBox("Enter a date between " & Me.SsnStDt & " and " & Me.SsnEndDt & vbCrLf & vbCrLf & "Want to change Date?", vbYesNo, "Wrong Date") = vbYes Then
            Cancel = True
        End If
    End If
    End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. VBA message box
    By Homegrownandy in forum Access
    Replies: 2
    Last Post: 03-30-2016, 01:36 AM
  2. Replies: 7
    Last Post: 03-17-2016, 05:53 PM
  3. Replies: 2
    Last Post: 01-23-2014, 12:40 PM
  4. Replies: 15
    Last Post: 11-01-2013, 03:24 PM
  5. Replies: 9
    Last Post: 09-26-2012, 12:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums