Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    ondrejruz is offline Novice
    Windows 10 Office 365
    Join Date
    Oct 2022
    Posts
    16

    Date - conversion to the name of the day in another field


    Hello. I am a beginner in Access. I have a form and it has 2 fields: Date and Name of the Day. I select a date in the Date field. How can I make sure that the appropriate name is displayed in the Names of the day field? I tried using: =If(Weekday([date])=1;[Sunday]) but it doesn't work for me... Can anyone help? Ondrej

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    In the control source of the NameDay control put =Format([YourDateField], "dddd")

    A number of things.
    Don't store this value, just use a query to calculate/display it. If you store it and the date changes you would have to recalculate it and store the new result, which is a recipe with potential for error or not updating.
    [Date] is a very bad name for a field. It is a reserved word and a function in Access. Call it something more meaningful like SalesDate, or OpenDate etc.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    First of all, "Date" is one of many reserved words and should not be used as the name of any object (field, control, form, report etc) A search for Access reserved words will help. InvDate, PurchDate or some thing like that would be much better as it also tells you something about the date.

    Also, avoid spaces in any names that you give. Use PurchDate rather than Purch Date which would need to be enclosed in square brackets: [Purch Date]. This will save you much typing and will save you a headache or two when you forget the brackets.

    Second. forms don't have fields. Tables have fields. Forms have controls (text boxes, combo boxes etc) which may or may not be bound to fields in the forms Record Source.
    To get the name of the day I would use the following expression as a "Calculated Field" in the query used as the forms Record Source or as the Control Source of a textbox on the form.
    You should not be saving the "Day" to a field in a table as this is a calculation. Calculations should rarely be saved, rather calculated whenever and wherever required.

    Post back if you need further explanation or an example of the two different methods suggested.
    WeekdayName(Weekday([NameOfDateFiel]),False,1)
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    ondrejruz is offline Novice
    Windows 10 Office 365
    Join Date
    Oct 2022
    Posts
    16
    I used this function as element source but syntax error... =WeekdayName(Weekday([Datum]),False,1)

    The name of the field in the db is not date but datum... The first texbox contains the date, the second texbox should contain the name of the day, i.e. the text...
    Should I use the function =WeekdayName(Weekday([Date]),False,1) when leaving the first textbox, or as the source of the second textbox?
    Ondrej

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Set the Control Source property of the second text box to:
    =WeekdayName(Weekday([Datum]),False,1)

    EDIT:
    I have attached a db using both methods for you.
    Attached Files Attached Files
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  6. #6
    ondrejruz is offline Novice
    Windows 10 Office 365
    Join Date
    Oct 2022
    Posts
    16
    Hello. Thanks it works for me now... But.... Since I want to display any day of the week, I used the function: =WeekdayName(Weekday([Datum])). But it shows me the name of the day incorrectly, e.g. 13.10.2022 as Saturday. Unás week starts as Monday instead of Sunday, but it doesn't fit either way... You probably need some settings somewhere... Ondrej

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    Quote Originally Posted by ondrejruz View Post
    I used this function as element source but syntax error... =WeekdayName(Weekday([Datum]),False,1)

    The name of the field in the db is not date but datum... The first texbox contains the date, the second texbox should contain the name of the day, i.e. the text...
    Should I use the function =WeekdayName(Weekday([Date]),False,1) when leaving the first textbox, or as the source of the second textbox?
    Ondrej
    Be aware you can easily and quickly test code like this in the immediate window.

    Code:
    ? WeekdayName(Weekday(Date),False,1)
    Monday
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by ondrejruz View Post
    Hello. Thanks it works for me now... But.... Since I want to display any day of the week, I used the function: =WeekdayName(Weekday([Datum])). But it shows me the name of the day incorrectly, e.g. 13.10.2022 as Saturday. Unás week starts as Monday instead of Sunday, but it doesn't fit either way... You probably need some settings somewhere... Ondrej
    I don't understand your requirement now. What are you trying to return with: =WeekdayName(Weekday([Date]))
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  9. #9
    Join Date
    Apr 2017
    Posts
    1,681
    Quote Originally Posted by ondrejruz View Post
    Hello. Thanks it works for me now... But.... Since I want to display any day of the week, I used the function: =WeekdayName(Weekday([Datum])). But it shows me the name of the day incorrectly, e.g. 13.10.2022 as Saturday. Unás week starts as Monday instead of Sunday, but it doesn't fit either way... You probably need some settings somewhere... Ondrej
    Optional parameter for Weekday()/Weekdayname() functions can have values from 1 or left out (week starts at Sunday), up to 7 (week starts at Saturday). To have week started at Monday, you need set the optional parameter to 2 for both of them: =WeekdayName(Weekday([Datum],2),2)

  10. #10
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Just out of idle curiosity what was wrong with using the simpler format() expression I suggested, which always works regardless of the weekday setting?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  11. #11
    ondrejruz is offline Novice
    Windows 10 Office 365
    Join Date
    Oct 2022
    Posts
    16
    Thanks everyone for the good advice! The function =WeekdayName(Weekday([Datum];2);False;2) in the source control on TextBox2 works for me. Another question: In TextBox1 is the Date, in TextBox2 is the name of the day. Is there a way to make the day name appear on TextBox2 as soon as the date is selected in TextBox1? Without having to click on TextBox2? Ondrej

  12. #12
    Join Date
    Apr 2017
    Posts
    1,681
    Quote Originally Posted by ondrejruz View Post
    Is there a way to make the day name appear on TextBox2 as soon as the date is selected in TextBox1? Without having to click on TextBox2?
    I assume the source of your TextBox is a table field where date is stored, and in TextBox2 formula you use same date field. The problem is, that when you change the date in TextBox1, it is changed in textbox (a form control), not in table. The changes you made in any form control are made in table only when the record is saved - e.g. when you select another record in form, or when you run a script where a command to save record is given.

    Your options to get date change reflected immediately in TextBox2 are (on fly):
    a) to force saving of record in AfterUpdate event of TextBox1 (and probably refreshing TextBox 2 after that);
    b) instead referring to date field in TextBox2 formula, referring to value of TextBox1 control (and probably running AfterUpdte event of TextBox1 to refresh TextBox2)

  13. #13
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I'm not sure if my post (#2) is visible to anybody.
    Or is there an echo in here?

    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    Quote Originally Posted by Minty View Post
    I'm not sure if my post (#2) is visible to anybody.
    Or is there an echo in here?

    I saw it.
    I also know the feeling.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  15. #15
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Quote Originally Posted by Minty View Post
    I'm not sure if my post (#2) is visible to anybody.
    Or is there an echo in here?

    Hello, hello, hello, hello, ...
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Problem with date conversion
    By WAVP375 in forum Access
    Replies: 2
    Last Post: 07-07-2018, 02:02 PM
  2. Conversion of Date/Time field from Access to SQL
    By Robert2150 in forum SQL Server
    Replies: 4
    Last Post: 10-02-2016, 01:37 PM
  3. Type Conversion Failure of date field in Access
    By accessmatt in forum Queries
    Replies: 4
    Last Post: 05-01-2015, 03:39 PM
  4. Date Conversion
    By mslenker in forum Access
    Replies: 1
    Last Post: 01-30-2013, 09:52 AM
  5. Date Conversion
    By mkc80 in forum Access
    Replies: 1
    Last Post: 06-27-2012, 04:04 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