473,378 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Code to Force a Date Into a Field

USTRAGNU1
Team Bytes,

I have two date fields:
App_Last
App_Next

I have a number field:
App_Frequency

I have various check boxes:
For the days of the week, 1st of month, quarter, etc.

If you would be so kind as to provide a solution for the example scenario below, I can create the rest of the combinations I need. I just need one example to get me started.

Example: If check box for Monday is checked and App_Frequency = 7, then return current date for that Monday in App-Last, and return next Monday's date in App_Next.

With all of the various combinations I can put together from an example you provide, I feel I can tailor it to where there will be little to no need for the users to manually select dates with the date picker.

Would you suggest these auto populated fields occur during form load event? Any other place?

If you have questions or require additional information, please let me know.

As always, thank you for your expertise and your great site.

Mike M
May 2 '17 #1

✓ answered by PhilOfWalton

This may get you started

Expand|Select|Wrap|Line Numbers
  1. Function NthDay(Nth As Integer, XDay As Integer, XMonth As Integer, TheYear As Integer) As Date
  2. '?nthday(3, 4, 2, 2012)   3rd Wed Feb 2012
  3. 'Print NthDay(-2, 6, 5, 2012); last but 1; Fri; May; 2012
  4. ' Sunday is day 1
  5.  
  6.     On Error GoTo NthDay_Err
  7.  
  8.     Dim Dt As Date
  9.     Dim i As Integer, Count As Integer
  10.     Dim DayOfWeek As Integer
  11.     Dim LastDay As Integer
  12.  
  13.     If Nth = 0 Or Nth > 5 Or Nth < -5 Then
  14.         MsgBox "Impossible date", vbCritical
  15.         Exit Function
  16.     End If
  17.  
  18.     If Nth > 0 Then
  19.         i = 1
  20.         Do Until Count = Nth
  21.             Dt = DateValue(CStr(i) & "/" & CStr(XMonth) & "/" & CStr(TheYear))
  22.             'Debug.Print Format(Dt, "medium date")
  23.             DayOfWeek = Weekday(Dt)
  24.             If DayOfWeek = XDay Then
  25.                 Count = Count + 1
  26.             End If
  27.             i = i + 1
  28.         Loop
  29.     Else                            ' work backwards
  30.         LastDay = Day(DateSerial(TheYear, XMonth + 1, 0))  ' Last day of month
  31.         i = LastDay
  32.         Do Until Count = -Nth       ' Nth is negative
  33.             Dt = DateValue(CStr(i) & "/" & CStr(XMonth) & "/" & CStr(TheYear))
  34.             'Debug.Print Format(Dt, "medium date")
  35.             DayOfWeek = Weekday(Dt)
  36.             If DayOfWeek = XDay Then
  37.                 Count = Count + 1
  38.             End If
  39.             i = i - 1
  40.         Loop
  41.     End If
  42.  
  43.     NthDay = Format(Dt, "Medium Date")
  44.  
  45.     Exit Function
  46.  
  47. NthDay_Err:
  48.     If Err = 13 Then                ' Date doesn't exist
  49.         Dt = 0
  50.         NthDay = 0
  51.     Else
  52.         MsgBox "Error:" & Err & "  " & Err.Description
  53.     End If
  54.  
  55. End Function
  56.  
  57.  
Phil

2 811
PhilOfWalton
1,430 Expert 1GB
This may get you started

Expand|Select|Wrap|Line Numbers
  1. Function NthDay(Nth As Integer, XDay As Integer, XMonth As Integer, TheYear As Integer) As Date
  2. '?nthday(3, 4, 2, 2012)   3rd Wed Feb 2012
  3. 'Print NthDay(-2, 6, 5, 2012); last but 1; Fri; May; 2012
  4. ' Sunday is day 1
  5.  
  6.     On Error GoTo NthDay_Err
  7.  
  8.     Dim Dt As Date
  9.     Dim i As Integer, Count As Integer
  10.     Dim DayOfWeek As Integer
  11.     Dim LastDay As Integer
  12.  
  13.     If Nth = 0 Or Nth > 5 Or Nth < -5 Then
  14.         MsgBox "Impossible date", vbCritical
  15.         Exit Function
  16.     End If
  17.  
  18.     If Nth > 0 Then
  19.         i = 1
  20.         Do Until Count = Nth
  21.             Dt = DateValue(CStr(i) & "/" & CStr(XMonth) & "/" & CStr(TheYear))
  22.             'Debug.Print Format(Dt, "medium date")
  23.             DayOfWeek = Weekday(Dt)
  24.             If DayOfWeek = XDay Then
  25.                 Count = Count + 1
  26.             End If
  27.             i = i + 1
  28.         Loop
  29.     Else                            ' work backwards
  30.         LastDay = Day(DateSerial(TheYear, XMonth + 1, 0))  ' Last day of month
  31.         i = LastDay
  32.         Do Until Count = -Nth       ' Nth is negative
  33.             Dt = DateValue(CStr(i) & "/" & CStr(XMonth) & "/" & CStr(TheYear))
  34.             'Debug.Print Format(Dt, "medium date")
  35.             DayOfWeek = Weekday(Dt)
  36.             If DayOfWeek = XDay Then
  37.                 Count = Count + 1
  38.             End If
  39.             i = i - 1
  40.         Loop
  41.     End If
  42.  
  43.     NthDay = Format(Dt, "Medium Date")
  44.  
  45.     Exit Function
  46.  
  47. NthDay_Err:
  48.     If Err = 13 Then                ' Date doesn't exist
  49.         Dt = 0
  50.         NthDay = 0
  51.     Else
  52.         MsgBox "Error:" & Err & "  " & Err.Description
  53.     End If
  54.  
  55. End Function
  56.  
  57.  
Phil
May 2 '17 #2
Wow, thank you! I thought there would be less to it than this. Sorry about that!

UTS
May 2 '17 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them...
1
by: Liz Malcolm | last post by:
Hello and TIA. I have a DE form with an option group that if daily is selected todays date is used for start and end date, if weekly is selected Monday - Friday is used. I am trying to add a...
1
by: Graham Feeley | last post by:
I have a date field named rcdate it is a general date eg: 12/08/2006 3:30:00 PM 12/08/2006 3:00:00 PM I used to update another empty field named rdate with the with using in a update query "...
10
by: satishrajana | last post by:
Hi, My SQL returns a NULL in a datefield if there is no date in that field. If there is a NULL in this column, I want to replace it with spaces in my SELECT statement when I am selecting these...
4
by: andrewbda | last post by:
I have tickbox on a form, and I would like to use a date field as the control source. i.e. I would like to have it display as ticked when a date exists in the field, and vice versa. Also,...
3
by: Finn Stampe Mikkelsen | last post by:
Hi I have defined a table in my database, with 2 date-fields. I have set a default value to DBNull. I have integrated a nullable datetimepicker control to my project and set the apropriate...
7
by: sheri | last post by:
I have a field called date, the date is in the field like this 70925 (which means Sept. 25, 2007). I have another field called day, it is a text field. How do I write a query to populate the day...
10
by: Jes | last post by:
Dear all I have a date field on a HTML form where the user is asked to key in dd/mm/yyyy However, when that is written to MySql it is either not accepted or another value is tored in the...
8
beacon
by: beacon | last post by:
Hi everybody, I'm having a terrible time trying to accomplish what I thought would be extremely easy. I have three date fields on a form (DischargeDate, ReceivedDate, and AuditedDate) and I'm...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.