Share via

Access 2016 Date Picker

Anonymous
2017-03-23T13:55:25+00:00

Hello,

Apologies for my very novice questions.  I have a form used as a scheduler and need to alter my date picker to exclude weekends.  I located the following code online however I cannot get the message box to display what the code indicates it will display.  Also, I am only prohibited on my first attempt at choosing a weekend date.  Additional attempts are allowed.  I want all Saturday/Sunday dates to be prohibited.

System.DateTime myDate = DateTime.Now();

String myDateString="";

private void dateTimePicker1_ValueChanged(object sender, System.EventArgs e)

{

System.DayOfWeek i = myDate.DayOfWeek;

if((i == System.DayOfWeek.Sunday) || (i == System.DayOfWeek.Saturday))

{

MessageBox.Show("Please try again, can't select a weekend day");

myDateString = dateTimePicker1.Value.ToString();

return;

}

else

myDateString = dateTimePicker1.Value.ToString();

}

***Post moved by the moderator to the appropriate forum category.***

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

11 answers

Sort by: Most helpful
  1. Anonymous
    2017-03-23T18:30:55+00:00

    From the error message, it appears that you are entering the code in the Table's validation property. That's the wrong place! It should be in the BeforeUpdate event property of the form textbox with the date picker.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-03-23T17:43:30+00:00

    Yes I realize I am to edit according to my form specifications.  However, I am not having any success.

    My text box is "Scheduled PM Date".  And "No Weekends Allowed" will do.  I have attempted this and still my message box is not correct.

    I get the "Compile error.  in table-level validation expression". message instead

    Also,  as mentioned before, my first attempt at choosing a prohibited date is refused, but additional attempts are permitted.

    Was this answer helpful?

    0 comments No comments
  3. ScottGem 68,830 Reputation points Volunteer Moderator
    2017-03-23T17:27:48+00:00

    Umm Leah, what Tom gave you was the exact code you should use. The only difference is the name of the control where you are storing the date. Since you did not provide the name of the control Tom used a placeholder ("theNameOfYourTextbox").

    The other change is the message. Tom used "Yo! No weekend days", but you can place anything you want in between the quotes for the MsgBox command.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2017-03-23T16:51:40+00:00

    Ok, so don't make fun of me, but how would this code appear; verbatim.  Again, I am VERY novice, somebody who has had no formal training in Access, but have learned what i know by osmosis and by the examples of smarter people such as yourself :)

    Was this answer helpful?

    0 comments No comments
  5. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2017-03-23T14:35:14+00:00

    This is vb.net code, not vba. That won't work.

    In your textbox' BeforeUpdate event write:

    dim intWeekDay as integer

    intWeekDay = WeekDay(Me.theNameOfYourTextbox)

    if intWeekDay=1 or intWeekDay=7 then

      Msgbox "Yo! No weekend days.", vbExclamation

      Cancel = True    'Important: this stops the user from leaving the field without first correcting it.

    else

      'Nothing to do - value is a work day

    end if

    Was this answer helpful?

    0 comments No comments