Weekday function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GDC1970
    New Member
    • Oct 2014
    • 17

    Weekday function

    I am having problems with my code. I am trying to look at a date on a form and show me the weekday number for that date on the same form different text box. I am using MS ACCESS 2013 VBA

    Code:
    Private Sub TxtAppointmentDate_BeforeUpdate(Cancel As Integer)
    
    Dim D As Date
    Dim W As Integer
    D = (TxtAppointmentDate)
    W = Weekday(D)
    Me.test = W
    
    End Sub
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    One thing that strikes me is that you are using the BeforeUpdate event of the textbox concerned. If the date is being entered manually you will need to use the AfterUpdate event instead. BeforeUpdate fires before the textbox has a final value in it, allowing you to cancel the update if necessary. Your assignment statement is not going to work correctly as the textbox by definition has not yet been updated to a final state when you try to make the assignment.

    AfterUpdate fires after the value has been entered, and this is what you need for your assignment statements to operate correctly(as far as I can tell from the little you have mentioned to us).

    Having said all that, there are easier ways you could do this, such as setting the RowSource property of the other textbox to

    Code:
    =weekday([YourFieldOrControlName])
    replacing the fieldname as appropriate to your particular circumstances.

    -Stewart
    Last edited by Stewart Ross; Oct 26 '14, 05:30 PM.

    Comment

    • GDC1970
      New Member
      • Oct 2014
      • 17

      #3
      Excellent thanks for your feedback, ill follow your suggestions.

      Comment

      • twinnyfo
        Recognized Expert Moderator Specialist
        • Nov 2011
        • 3662

        #4
        @Stewart,

        Minor correction: should set the ControlSource property, not RowSource.

        I know what you meant, but wanted to clarify for OP and others.

        Comment

        • Stewart Ross
          Recognized Expert Moderator Specialist
          • Feb 2008
          • 2545

          #5
          Thanks for the correction twinnyfo - see what happens when I rely on my memory of the property involved. -S

          Comment

          • twinnyfo
            Recognized Expert Moderator Specialist
            • Nov 2011
            • 3662

            #6
            No problem. Your advice to OP was spot on.

            Comment

            Working...