Results 1 to 3 of 3
  1. #1
    dgtampa is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    28

    Data validation: input Thursdays only

    I'm trying to get this code to work in the BeforeUpdate event; the idea is that when the user enters a date which falls on any other day of the week except for a Thursday, a message box prompts them to pick a date which falls on a Thursday. Once they do pick a "Thurs-date", the focus is set on a command button on the form.

    I can kinda-sorta get this to work using different combinations of code, but 4 different types of things invariably happen:

    1) The message box appears as desired, but the focus cannot be re-set;


    2) I get an error which states 'Invalid use of Null' (over and over each time I choose a subsequent date, even Thursday);
    3) I get an error for a data mis-match;
    4) the message box appears over-and-over, no matter if I pick a "Thurs-date" or not...

    Here is the code I am using:

    Code:
    Private Sub cboDatePick_Change()
    If Weekday(Me.cboDatePick) = 5 Then
            Me.btnOpenSchedule.SetFocus
        Else
            Beep
            MsgBox "You need to pick a date which falls on a Thursday...", vbInformation, "Oops!"
            DoCmd.CancelEvent
            DoCmd.ClearMacroError
        End If
    End Sub
    I've tried also changing the first section to
    Code:
    If Weekday(Forms!frmStart.cboDatePick) = 5 Then
    To no avail.

    I'm guessing my "IF" argument is not being written correctly, or something like that...Thanks in advance!

  2. #2
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    When doing something that is based on the Value entered in a Control, you need to use the Control's BeforeUpdate event:
    Code:
    Private Sub cboDatePick_BeforeUpdate(Cancel As Integer)
    If Weekday(Me.cboDatePick) <> 5 Then
      Beep
      MsgBox "You need to pick a date which falls on a Thursday...", vbInformation, "Oops!"
      Cancel = True
      DoCmd.ClearMacroError
    End If
    End Sub
    But you cannot Move to another Control from this event until the Control's Value is saved, so you need to move that piece of code into another event:
    Code:
    Private Sub cboDatePick_Exit(Cancel As Integer)
     Me.btnOpenSchedule.SetFocus
    End Sub
    I'm curious about something, though! The name cboDatePick would seem to indicate that the Control is a Combobox, and if this is true, why are non-Thursday Dates available?

    Also note that I left the line

    DoCmd.ClearMacroError


    in place, although I have no idea why it is there, since it has nothing to do with this task!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    dgtampa is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    28
    Thank you Missinglinq! (feels kinda wierd saying that...)

    Your solution works very well! I wound up putting the SetFocus code in the OnDirty event for the control.

    As for the control name cboDatePick; I was just sloppy with that particular prefix. I am usually more careful with my naming conventions. The control is, in fact, a TextBox, not a ComboBox, but by the time I realized the error myself, I already had several other objects referring to that form and didn't want to change it. The reason for TextBox over ComboBox is that I wanted to have a date picker available for the user to select a Thursday date in the future.

    I have no idea why the ClearMacroError command is there either; I guess I was just trying to get rid of the error boxes somehow?

    Again, I thank you for your help!

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

Similar Threads

  1. Replies: 1
    Last Post: 08-02-2011, 07:21 AM
  2. input Validation
    By injanib in forum Forms
    Replies: 4
    Last Post: 06-16-2011, 10:01 AM
  3. Input Mask/Validation rule
    By SMAlvarez in forum Access
    Replies: 0
    Last Post: 04-11-2011, 07:27 PM
  4. Replies: 0
    Last Post: 03-18-2011, 06:38 AM
  5. Validation and Input masks
    By Santi in forum Access
    Replies: 3
    Last Post: 03-26-2009, 10:53 AM

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