Consulting

Results 1 to 18 of 18

Thread: Solved: Allowing macro beetwen 4 - 5 PM

  1. #1
    VBAX Tutor
    Joined
    Sep 2007
    Posts
    265
    Location

    Solved: Allowing macro beetwen 4 - 5 PM

    Hi guys,

    I need your help to fix the following my VBA Code:
    If function is allowed between 4.00 PM until 5 PM. However, the code doesn't work. My PC is setting as Indonesian in Language option and Bangkok-Hanoi-Jakarta for date and time.

    Any help and suggestion, would be appreciate

    [VBA]Sub Submission()
    Dim CurrDate As Date
    Dim CrTime As String

    CurrDate = Format(Now, "Short Time")

    If CurrDate <= #3:59:00 PM# And CurrDate >= #5:01:00 PM# Then
    MsgBox "You can't allowed at this time," & vbCr _
    & "Please try between 4 PM until 5 PM !!", vbOKOnly, "System Alert.."
    Else
    Call SaveUserButton
    End If

    End Sub[/VBA]

  2. #2
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    228
    Location
    Quote Originally Posted by slamet Harto
    Hi guys,

    I need your help to fix the following my VBA Code:
    If function is allowed between 4.00 PM until 5 PM. However, the code doesn't work. My PC is setting as Indonesian in Language option and Bangkok-Hanoi-Jakarta for date and time.

    Any help and suggestion, would be appreciate

    [vba]Sub Submission()
    Dim CurrDate As Date
    Dim CrTime As String

    CurrDate = Format(Now, "Short Time")

    If CurrDate <= #3:59:00 PM# And CurrDate >= #5:01:00 PM# Then
    MsgBox "You can't allowed at this time," & vbCr _
    & "Please try between 4 PM until 5 PM !!", vbOKOnly, "System Alert.."
    Else
    Call SaveUserButton
    End If

    End Sub[/vba]
    See below, works fine for me

    [vba]
    Sub Submission()

    Dim CurrTime As String

    CurrTime = format(Now, "h:mm:ss")

    If CurrTime >= #3:59:00 PM# And CurrTime <= #5:01:00 PM# Then
    MsgBox "You can't allowed at this time," & vbCr _
    & "Please try between 4 PM until 5 PM !!", vbOKOnly, "System Alert.."
    Else
    Call SaveUserButton
    End If

    End Sub
    [/vba]

    Hopefully it will work for you

    Marshybid

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Suggest a small change

    [vba]

    If CurrTime > #3:59:59 PM# And CurrTime < #5:00:01 PM# Then
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    [vba]If CurrDate <= #3:59:00 PM# Or CurrDate >= #5:01:00 PM# Then[/vba]

    EDIT
    Bob is right, the timestamps can be refined by adding seconds.
    So it will be
    [vba]If CurrDate <= #3:59:59 PM# Or CurrDate >= #5:00:01 PM# Then[/vba]

    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  5. #5
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    228
    Location
    Quote Originally Posted by xld
    Suggest a small change

    [vba]

    If CurrTime > #3:59:59 PM# And CurrTime < #5:00:01 PM# Then
    [/vba]
    Good point xld. I spotted that the OP had the <> the wrong way round in original code, but by removing the = the time matches the message box criteria... between 4PM and 5PM

    Marshybid

  6. #6
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    228
    Location
    Quote Originally Posted by JimmyTheHand
    [vba]If CurrDate <= #3:59:00 PM# Or CurrDate >= #5:01:00 PM# Then[/vba]

    EDIT
    Bob is right, the timestamps can be refined by adding seconds.
    So it will be
    [vba]If CurrDate <= #3:59:59 PM# Or CurrDate >= #5:00:01 PM# Then[/vba]

    Jimmy
    Hi Jimmy, why OR?? The And seemed to work for me when I changed the times to test??

    Marshybid

  7. #7
    Because the OP wants to call SaveUserButton between 4 and 5 PM, and he wants to display the warning outside of this time interval. I think your solution does the opposite. Maybe I'm wrong. I didn't test it

    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  8. #8
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    [VBA]Sub Submission1()
    If Time <= "15:59:00" Or Time >= "17:01:00" Then
    MsgBox "You can't allowed at this time," & vbCr _
    & "Please try between 4 PM until 5 PM !!", vbOKOnly, "System Alert.."
    Else
    Call SaveUserButton
    End If

    End Sub[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  9. #9
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    228
    Location
    Quote Originally Posted by Simon Lloyd
    [vba]Sub Submission1()
    If Time <= "15:59:00" Or Time >= "17:01:00" Then
    MsgBox "You can't allowed at this time," & vbCr _
    & "Please try between 4 PM until 5 PM !!", vbOKOnly, "System Alert.."
    Else
    Call SaveUserButton
    End If

    End Sub[/vba]
    Hi Simon,

    I tried Time in my first attempt at this one and when I ran it it returned an error, variable/object not defined???

    Marshybid

  10. #10
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    228
    Location
    Quote Originally Posted by JimmyTheHand
    Because the OP wants to call SaveUserButton between 4 and 5 PM, and he wants to display the warning outside of this time interval. I think your solution does the opposite. Maybe I'm wrong. I didn't test it

    Jimmy
    Your right Jimmy, I rad it the other way round

    Marshybid

  11. #11
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    It works perfect for me, obviously i changed the Call SaveUSerButton for a msgbox but it worked fine!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  12. #12
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    228
    Location
    Quote Originally Posted by Simon Lloyd
    It works perfect for me, obviously i changed the Call SaveUSerButton for a msgbox but it worked fine!
    Ditto, changed SaveUserButton to a msgbox that read 'working'

    Can't seem to get Time recognised though??

    Marshybid

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by marshybid
    Ditto, changed SaveUserButton to a msgbox that read 'working'

    Can't seem to get Time recognised though??

    Marshybid
    Select the code that says Time, Shift-F2 and see if it takes you anywhere.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  14. #14
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    you may be missing a library reference!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  15. #15
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    oops sorry didn't see your toes there Bob!!!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  16. #16
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    228
    Location
    Quote Originally Posted by xld
    Select the code that says Time, Shift-F2 and see if it takes you anywhere.
    I remembered that you advised to do that when I couldn't get 'format' to work

    Tried, no good, will require further investigation on my part

    Thanks for the pointer though Bob.

    Marshybid

  17. #17
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    TIME belongs to the VBA DateTime library (Microsoft Office 11.0 (12.0 for me) Object Library, find it in Tools References from the VBE)
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  18. #18
    VBAX Tutor
    Joined
    Sep 2007
    Posts
    265
    Location
    Dear all,

    Apologize for late reply and thank you so muach for your assistance.
    highly Appreciate it.

    Rgds, Harto

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •