Results 1 to 25 of 25

Thread: [RESOLVED] how to disable a user to change date if it is not the same month

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Resolved [RESOLVED] how to disable a user to change date if it is not the same month

    i have a form that a user can change the punch clock for each employee
    now lets say now it is july
    how do i prevent from the user to change the datepicker to august or june
    and only stay in july month?

    e.x today is 23-7-2014
    if a user tries to change the date to 1-8-2014 or 3-6-2014 then he cannot.
    if today is may then he cannot change to june or april or any other month only this month
    i need the date to be only in the same month as now
    i tried some methods didnt work for me
    it is a little bit tricky
    any help will be appreciated
    salsa31

  2. #2
    New Member
    Join Date
    Jul 2014
    Location
    Indonesia
    Posts
    5

    Re: how to disable a user to change date if it is not the same month

    So the user can only enter this month?

    Maybe like this ?

    Code:
    Private Sub DTPicker1_Change()
          Dim MonthNow, YearNow As Integer
          MonthNow = Format(DTPicker1.Value, "mm")
          YearNow = Format(DTPicker1.Value, "yyyy")
    
          If MonthNow <> Format(Now, "mm") Or YearNow <> Format(Now, "yyyy") Then
                DTPicker1.Value = Format(Now)
          End If
    
    End Sub

  3. #3
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: how to disable a user to change date if it is not the same month

    Well, you can always completely limit the date changing using Windows Group Policy Per user.
    This will require the user to have Admin privilege to change the date\time.

    If that option isn't good for you, you can always time the ticks from when the application started and compare it to the date and time, if something doesnt add up, revert the date back.

    Another way to do it, is by listening to windows messages, you may or may not know, that every time a user changes the time\date, a windows message (WM_TIMECHANGE) is sent to all windows, to notify them of the change.
    You can programmatically disable date changing, (sample the date and revert it back to the sampled date every time your application recieves WM_TIMECHANGE message, EXECEPT from when your application is the one that changes the date, this can be controleld using a boolean)

    Something like
    Code:
    'pseudo code
    
    Dim allowDateChange as Boolean
    Dim sampleDate as Date
    ' initialize allowDateChange = False on Form_load
    ' initialize sampleDate as Date on a low interval Timer
    
    If (You recieved WM_TIMECHANGE) Then
    
        If (allowDateChange = True) Then
    
            ChangeDateTo(...)
    
        Else
    
            Msgbox ("Not Allowed")
    
            ChangeDateTo(sampleDate)
    
        End If
    
    End If
    
    'allowDateChange boolean will only be true when your application does the change, and by your rules (certain month, year, hour, .. etc)
    Note you can retrieve current date ,month , year, time using the Date() and Time() functions..

    Code:
    Dim strDate as String
    
    strDate = Year(Date)
    strDate = Month(Date)
    strDate = Day(Date)
    
    strDate = Hour(Time)
    strDate = Minute(Time)
    strDate = Second(Time)
    Hope this helps.

  4. #4
    gibra
    Guest

    Re: how to disable a user to change date if it is not the same month

    Instead of DataPicker, use a TextBox where user can change only the number of day.
    On DataValidation event check if the number is correct, related to the month.

  5. #5

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how to disable a user to change date if it is not the same month

    hey ricky i tried your method but when i checked the results the datepicker goes to the same date as today
    when i change the datepicker to august and hit the ok button it lets me update
    Code:
     Dim MonthNow, YearNow As Integer
          MonthNow = Format(LblDate.Value, "mm")
          YearNow = Format(LblDate.Value, "yyyy")
    
          If MonthNow <> Format(Now, "mm") Or YearNow <> Format(Now, "yyyy") Then
                LblDate.Value = Format(Now)
          End If
    if a user punchclock in but in the wrong date i want to be able to change the date
    but only for the same month and year

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: how to disable a user to change date if it is not the same month

    Code:
    Private Sub Calendar1_NewMonth()
     If Not Calendar1.Month = Now Then MsgBox ("You must only select from the currect month!")
    End Sub
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how to disable a user to change date if it is not the same month

    e.x lets say the datepicker shows 23/7/2014 and i want to change it to 15/8/2014
    or to change it to 12/6/2014
    it cannot allow me because i need to be able to change only on the same month and year

  8. #8
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: how to disable a user to change date if it is not the same month

    Quote Originally Posted by salsa31 View Post
    e.x lets say the datepicker shows 23/7/2014 and i want to change it to 15/8/2014
    First you ask about preventing the change of month and now you want to allow the user to change the month?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  9. #9

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how to disable a user to change date if it is not the same month

    my mistake.i ment to be able to change only
    the day of the same month
    and prevent from changing the month or year

  10. #10
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: how to disable a user to change date if it is not the same month

    I have change my previous code to

    Code:
    Option Explicit
    
    Private Sub Calendar1_NewMonth()
     If Not Calendar1.Month = Now Then MsgBox ("You must only select from the currect month!")
     Calendar1.Month = Month(Now)
    End Sub
    
    Private Sub Calendar1_NewYear()
     Calendar1.Year = Year(Now)
    End Sub
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  11. #11

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how to disable a user to change date if it is not the same month

    what is Calendar1_NewMonth()?
    calendar.year?
    i dont understand NW
    is there a variable needed?

  12. #12
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: how to disable a user to change date if it is not the same month

    Quote Originally Posted by salsa31 View Post
    what is Calendar1_NewMonth()?
    calendar.year?
    i dont understand NW
    is there a variable needed?
    No, those are built-in events of the control! In the code window where it says the of the event say "Click" click the down arrow to see what events are available for the control.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  13. #13

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how to disable a user to change date if it is not the same month

    im sorry i dont understand
    i have a button that saves the data and a datepiker
    if a user tries to change the month or the year then he gets the msgbox that he can only
    change the day of the same month and year
    how do i do that?
    using the datepicker?

  14. #14
    Hyperactive Member
    Join Date
    Jun 2014
    Location
    Lahore, Pakistan
    Posts
    450

    Re: how to disable a user to change date if it is not the same month

    Quote Originally Posted by Nightwalker83 View Post
    No, those are built-in events of the control! In the code window where it says the of the event say "Click" click the down arrow to see what events are available for the control.
    Man i think he do not know about the calender control.

  15. #15
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,836

    Re: how to disable a user to change date if it is not the same month

    I recommend not using Now() when preventing something from happening. The user can just change the computers clock. Especially if it is important like logging or time keeping. Get it off a server or the internet somewhere.
    Please remember next time...elections matter!

  16. #16
    gibra
    Guest

    Re: how to disable a user to change date if it is not the same month

    Quote Originally Posted by salsa31 View Post
    using the datepicker?
    Use a TextBox to change the DAY only.

    The use of DatePicker is a 'not sense' if the user can't change anything other the day.

    Normally, the DatePicker become usefull when the user must have the capability to change day, month and year.

    IMHO.

  17. #17

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how to disable a user to change date if it is not the same month

    hey gibra
    can u give me example please?

  18. #18
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: how to disable a user to change date if it is not the same month

    Quote Originally Posted by salsa31 View Post
    hey gibra
    can u give me example please?
    Text1.Text = Date

    ... ?

  19. #19
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: how to disable a user to change date if it is not the same month

    Sigh....
    how about setting the DateTimePicker.MinDate and the DateTimePicker.MaxDate properties? Just set the min date to the first of the current month and the max date to the last day of the current month.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  20. #20

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how to disable a user to change date if it is not the same month

    hey tg i tried allot of diffrent methods like converting the month to a number
    or if it is > then the number
    nothing worked.
    i dont know how to use what you suggested sry

  21. #21
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: how to disable a user to change date if it is not the same month

    did ya click the links?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  22. #22

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how to disable a user to change date if it is not the same month

    yes i did a little complicated to me to achieve what i need

  23. #23
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: how to disable a user to change date if it is not the same month

    datetimepicker1.MinDate = DateSerial(year(now), month(now), 1) ' Will set the min date to the first of the current month
    datetimepicker1.MaxDate = DateAdd("d", -1, DateAdd("m", 1, DateTimePicker1.MinDate)) 'Sets the MaxDate to the end of the month.


    we're not launching rockets here... far less complicated than anything posted so far... in my opinion

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  24. #24
    Hyperactive Member
    Join Date
    Jun 2014
    Location
    Lahore, Pakistan
    Posts
    450

    Re: how to disable a user to change date if it is not the same month

    Another solution is in here.. Enjoy!

    Code:
    Private Sub DTPicker1_CloseUp()
    If Not Format(DTPicker1.Value, "mm") = Format(Now, "mm") Then
    MsgBox "You cannot change the month or year.", vbExclamation, "Error"
    DTPicker1.Value = Now
    Exit Sub
    If Format(DTPicker1.Value, "yyyy") = Format(Now, "yyyy") Then
    MsgBox "You cannot change the month or year.", vbExclamation, "Error"
    DTPicker1.Value = Now
    Exit Sub
    End If
    End If
    End Sub
    Last edited by hamza.saleem; Jul 23rd, 2014 at 01:51 PM.

  25. #25

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how to disable a user to change date if it is not the same month

    TG & hamza tnk you amigos

Posting Permissions

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



Click Here to Expand Forum to Full Width