Results 1 to 8 of 8
  1. #1
    HMEpartsmanager is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2009
    Posts
    85

    Adding A Time Of Day; With Message To Switchboard

    Hello All,



    In my Switchboard, I have a text box that I change the contents every once in a while. Usually a line or two as a "joke of the day" or maybe some witty saying. Now I thought I would change it to display the username and display that in the box as well as getting a little message based on the time of the day. The part I haven't tested yet is this;


    ' Get a Message based on the Time of Day
    Dim MyTime As Integer
    Dim MyHour As Integer
    Dim MyMsg As String
    '
    MyTime = Time
    MyHour = Hour(MyTime)
    If MyHour <= 5 Then
    MyMsg = "WOW! What are you doing here?"
    ElseIf MyHour >= 6 And MyHour <= 7 Then
    MyMsg = "You are here early today."
    ElseIf MyHour >= 7 And MyHour <= 11
    MyMsg = "Good Morning!"
    ElseIf MyHour >= 12 And MyHour <= 16 Then
    MyMsg = "Good Afternoon."
    ElseIf MyHour >= 16 And MyHour <= 18 Then
    MyMsg = "Are you staying late?"
    ElseIf MyHour >= 19 And MyHour <= 21 Then
    MyMsg = "Do you know what time it is?"
    ElseIf MyHour >= 22 And MyHour <= 24
    MyMsg = "You are a night crawler!"
    Else
    MyMsg = "You really are staying late!"
    End If

    As I don't want to set my clock to test each of these times to see if it works, does the logic with the If/ElseIf/Then flow look correct that it should work for those odd hours of before I get into work, and the times I am already home.

    The flow looks correct, but I don't remember if I have worked to much with the If/ElseIf in a while.

    Thanks for any feedback.

    Tim

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Offhand it looks okay, though I'd probably use Select/Case and dump the variables:

    Select Case Hour(Now)

    but of course I'm lazy. Also, you don't really need the >= test in tests after the first. Both If/Then and Select/Case will drop out after the first "True" test, so you can simply test for <=5, <=7, etc. In other words, if the hour is 7 you already know it's >= 6 since it got past the first test, so you can simply test for <=7.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    SteveF is offline Generally AccessAble
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2010
    Location
    Fourth Corner
    Posts
    123
    Code looks OK to me as long as you change the formatting to get the ElseIf's and their matching THEN's on one line. You also have a couple of places where you've got a range overlap: "ElseIf MyHour >= 12 And MyHour <= 16 Then..." and "ElseIf MyHour >= 16 And MyHour <= 18 Then..." is one of them; I'll let you find the other.

    Personally, for code like this I tend to prefer to use SELECT CASE / END SELECT. To me it's a lot easier to read and follow the flow...

  4. #4
    HMEpartsmanager is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2009
    Posts
    85
    Paul & Steve, thank you for the comments. I grew up with the If/Then crowd and have not used Select Case, but now is a good time to review how that works. Thanks for the tips.

    Tim

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    No problem Tim.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    Just a comment on your statement
    As I don't want to set my clock to test each of these times to see if it works, does the logic with the If/ElseIf/Then flow look correct that it should work for those odd hours of before I get into work, and the times I am already home.
    .

    Instead of working with Time, the system function, for testing/development you just create a variable (eg XXTime) and assign it various values in your code to represent different times. This allows you to check your code, without having to modify your Clock/Time.

    Good luck with your project.

  7. #7
    HMEpartsmanager is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Nov 2009
    Posts
    85
    Quote Originally Posted by orange View Post
    Just a comment on your statement
    .

    Instead of working with Time, the system function, for testing/development you just create a variable (eg XXTime) and assign it various values in your code to represent different times. This allows you to check your code, without having to modify your Clock/Time.

    Good luck with your project.
    You know, after you wrote this, I smiled. I completely forgot about just assigning the value myself for testing. Big old slap on the forehead!

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848

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

Similar Threads

  1. Adding Time help
    By Bozilla in forum Forms
    Replies: 3
    Last Post: 11-08-2011, 09:18 AM
  2. Adding Error Message
    By desibabu90 in forum Forms
    Replies: 3
    Last Post: 08-04-2011, 11:50 AM
  3. Adding Time
    By jlclark4 in forum Queries
    Replies: 7
    Last Post: 05-04-2011, 10:35 AM
  4. Switchboard Error Message
    By bullwinkle55423 in forum Access
    Replies: 2
    Last Post: 12-08-2010, 01:09 PM
  5. Display a message for some time
    By wasim_sono in forum Programming
    Replies: 2
    Last Post: 03-05-2010, 11:11 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