Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
No Weekends
William DeGrandis 
      
15 months ago
I am trying to make a database to keep track of all my doctor's appointments. I want to make It so I can't put an appointment In on a weekend. Can someone help me? I am just learning Access. I keep going over the videos. I think If I keep watching the videos I will start to understand. I am handicap.
William DeGrandis OP  @Reply  
      
15 months ago
Do I just use the Weekday function?
Kevin Robertson  @Reply  
          
15 months ago
You could do something like this:

Private Sub AppointmentDate_BeforeUpdate(Cancel As Integer)

    If Weekday(AppointmentDate) = 1 Or Weekday(AppointmentDate) = 7 Then
        MsgBox "This is a weekend"
        Cancel = True
    End If
    
End Sub
William DeGrandis OP  @Reply  
      
15 months ago
Thank you I will try It.
William DeGrandis OP  @Reply  
      
15 months ago
I thought that's how you did It. I just didn't know that was the best way. Thanks again
Bill Carver  @Reply  
      
15 months ago
the best way early on is one that works... until you find a better way.
William DeGrandis OP  @Reply  
      
15 months ago
I just thought of another thing I want where you can't make an appointment on weekend, and it has to be between 9 am and 5pm weekdays? I am trying to figure It out for myself.
Richard Rost  @Reply  
           
15 months ago
William DeGrandis OP  @Reply  
      
15 months ago
I am sorry I only need hours and minute. I would like If It after 5PM or before 9 AM it stays on the appointmentdate field.
William DeGrandis OP  @Reply  
      
15 months ago
I have a database with a field name appointmentdate and the before update I put In my code:Private Sub btnSave_Click()

    ' Assuming Appointmentdate is the control on the form
    Dim appointment As Date
    appointment = Me.appointmentdate.Value ' Get the value from the form control

    ' Check if the appointmentdate falls on a weekend (Saturday or Sunday)
    If Weekday(appointment, vbMonday) > 5 Then
        MsgBox "Appointments cannot be scheduled on weekends. Please choose a weekday.", vbExclamation
        Me.appointmentdate.SetFocus ' Set focus back to the appointment date field
        Exit Sub
    End If

    ' Check if the appointment time is outside 9:00 AM to 5:00 PM
    If TimeValue(appointment) < TimeValue("9:00:00 AM") Or TimeValue(appointment) > TimeValue("5:00:00 PM") Then
        MsgBox "Appointments must be scheduled between 9:00 AM and 5:00 PM.", vbExclamation
        Me.appointmentdate.SetFocus ' Set focus back to the appointment date field
        Exit Sub
    End If

    ' If all validations pass, save the record
    DoCmd.RunCommand acCmdSaveRecord

End Sub
It stop at the setfocus. Can someone tell me why?
Kevin Robertson  @Reply  
          
15 months ago
Because of the Exit Sub.
Kevin Robertson  @Reply  
          
15 months ago
I would set a flag to determine if the condition has been validated.
Something similar to this:

DetailsPrivate Sub btnSave_Click()

    Dim Appointment As Date, isValidated as Boolean

    isValidated = True

    Appointment = AppointmentDate

    If Weekday(Appointment, vbMonday) > 5 Then
        MsgBox "Appointments cannot be scheduled on weekends. Please choose a weekday.", vbExclamation
        AppointmentDate.SetFocus
        isValidated = False
    End If

    If TimeValue(Appointment) < TimeValue("9:00:00 AM") Or _
         TimeValue(Appointment) > TimeValue("5:00:00 PM") Then
        MsgBox "Appointments must be scheduled between 9:00 AM and 5:00 PM.", vbExclamation
        AppointmentDate.SetFocus
        isValidated = False
    End If

    If isValidated Then DoCmd.RunCommand acCmdSaveRecord

End Sub
William DeGrandis OP  @Reply  
      
15 months ago
Is the before update the right place to put it?
William DeGrandis OP  @Reply  
      
15 months ago
Can I use a Setfocus In a Date time field? I want to check the time if it after 5PM return to that feild. I have a date field on my form and It set to date/time In the table.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/1/2026 9:04:30 PM. PLT: 1s