Consulting

Results 1 to 3 of 3

Thread: Date calculation and formula

  1. #1
    VBAX Regular
    Joined
    Feb 2018
    Posts
    15
    Location

    Date calculation and formula

    Hi

    I am creating an maintenance inspection schedule userform, whereby the user can input the day the item was inspected. I am trying now to take that date entered and extend it for a specific period, whether it be monthly (30days) quarterly (90 days) or what ever the inspection frequency is required.

    The dates are shown in textboxes.

    I have tried the following code but it isn't working to well, because i believe it doesn't recognize the value of cur1 (textbox) as a date.

    (extending the date with 30 days)
    PHP Code:
    insp1.Value cur1.Value "30" 
    I have attached the workbook if that might help.
    Attached Files Attached Files

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It worked fine for me (your file has problems so I couldn't use that). What datatype do you have for cur1? I used Date.
    ____________________________________________
    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

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    try:
    insp1.Value = Format(DateValue(cur1.Value) + 30, "yyyy/mm/dd")

    Separately, since you're using DTPicker controls on the form, consider using them to display dates instead of the textboxes; their native data type is date. This means that you could use proper dates throughout, which in turn means that date calculations could be more straightforward, with the possiblility of using many worksheet date functions within the code, so that it (a) becomes easy to ensure dates fall on working days, and (b) month(s)/year differences wouldn't be out by a day or two (you're using day counts for all the inspection intervals).

    One downside to using DTPickers is that when they're disabled so that the user can't alter them - the dates become more difficult to see (although not much more difficult than what you have at the moment). One way around this is to put them in a frame (your text boxes are already in a frame), and when that frame is disabled, the DTPickers are also disabled, but retain their enabled visibility (which you can configure anyway).
    Another downside is how to display them when they contain no date; you could hide them altogether.
    The upside is that you might want to allow the user to override suggested inspection dates manally, which DTPickers would allow them to do without their inputting invalid dates.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

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