Trying to move a file and add a date timestamp - date

Been reading through some of the questions and answers last couple days and can't work out where I'm going wrong here I would appreciate any help given. So I'm trying to move a file to a different fold before pulling in a new copy but I want the one I'm moving to have a Date timestamp after (just so I have a record of it in future). Apologies if this is a bit rough its my second time trying to do something like this.
DIM Date1
Date1 = Now()
DIM FSO
SET FSO=CreateObject("Scripting.FileSystemObject")
FSO.Movefile "I:\This is it\External Storage\Replen\Data\LOCATIONS\*ITEM INVENTORY BY PURCHASE ORDER.csv*", "I:\This is it\External Storage\Replen\Data\LOCATIONS\ITEM INVENTORY BY PURCHASE ORDER\" & Date1.csv
With CreateObject("Scripting.FileSystemObject")
.MoveFile "C:\Users\s.c\Downloads\*.csv*", "I:\This is it\External Storage\Replen\Data\LOCATIONS\"
End With

This code will move the sSourceFile (ITEM INVENTORY BY PURCHASE ORDER.csv in your case) to sDestinationFolder and rename (to YYYY-MM-DD.csv) it using your computer's date:
Dim sSourceFile
Dim sDestinationFolder
Dim sDate
Dim objFSO
sSourceFile = "I:\This is it\External Storage\Replen\Data\LOCATIONS\ITEM INVENTORY BY PURCHASE ORDER.csv"
sDestinationFolder = "I:\This is it\External Storage\Replen\Data\LOCATIONS\ITEM INVENTORY BY PURCHASE ORDER\"
' Build date string
sDate = Year(Now) & "-" & Month(Now) & "-" & Day(Now)
' Create FileSystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check if File exists
If objFSO.FileExists(sSourceFile) Then
' Move file
objFSO.Movefile sSourceFile, sDestinationFolder & sDate & ".csv"
Else
' File does not exist
End If

Related

creating a time range filter when the data type of dates is text

I have a table with order date, usually most of the data is date, but sometimes I need to write text, so that's why I want to choose text as data type. My data in the table is as follows:
BestellDatum
20.10.2021
22.10.2021
03.01.2022
02.01.2022
23.01.2022
I have a query I created from this table and a form I created from this query. In this form I want to filter by date range (two text fields, names txt.orders_from and txt.orders_to). My code for this:
Private Sub SearchbyDateRange_Click()
Dim strCriteria, task As String
Me.Refresh
If IsNull(Me.txt.orders_from) Or IsNull(Me.txt.orders_to) Then
MsgBox "Please enter a date range"
Me.txt.orders_from.SetFocus
Else
strCriteria = "([OrderDate] >= #" & Format(Me.txt.orders_from, "yyyy/mm/dd") & "# And [OrderDate] <= #" & Format(Me.txt.orders_to, "yyyy/mm/dd") & "#)"
task = "select * from qryOrders where(" & strCriteria & ") order by [OrderDate]"
DoCmd.ApplyFilter task
End If
End Sub
However, if I try to search for the period from 20.10.2021 to 03.01.2022, for example, no dates are displayed. This code works when the OrderDate column has the data type as date/time. Is there anything I can do to use this code with data type as Text or do you have any other idea?
Format expects a Date as the first parameter and you are feeding it a Text. Instead of trying to convert text into different text what you need is a way to convert your text into a date.
The function you are looking for to convert text to a date value and make date comparisons is CDate.
I am not 100% sure CDate will be able to read your specific date format but it is worth a try. If not you will have to do the hard work of breaking it down into a text array and recombining it to a more compatible format.
Convert your text dates to true dates:
strCriteria = "CDate(Replace([OrderDate], ".", "-")) >= #" & Format(Me.txt.orders_from, "yyyy\/mm\/dd") & "# And CDate(Replace([OrderDate], ".", "-")) <= #" & Format(Me.txt.orders_to, "yyyy\/mm\/dd") & "#"

VBA Error 3075 When Filtering Between Dates

I know that there are a lot of answered questions regarding this topic but none of them answer the following question:
I have got this little and simple code:
Private Sub btnHwEOLNext2_Click()
Dim filterString As String
Dim startDate As Date
Dim endDate As Date
startDate = Date
endDate = Date + 730
filterString = "[HW End of Support] BETWEEN '" & startDate & " And " & endDate & "'"
Me.Filter = filterString
DoCmd.SetOrderBy "[HW End of Support] ASC"
Me.FilterOn = True
End Sub
all i am trying to do is to filter between 2 dates. the startDatedate will always be the day you use the form and the endDate will always be 730 days after startDate
why do i keep on getting
error 3075
? Help please!
If you put the date between # signs, then VBA considers it as a date.
As far SQL is using American date system with MM-DD-YYYY, you can change the format between the # signs, before parsing:
"[HW End] BETWEEN #" & Format(sd,"MM-DD-YYYY") & "# And #" & Format(ed,"MM-DD-YYYY") & "#"

Create Dynamic List of Business Dates Since Arbitrary Start Date in Access

Using MS Access 2010, I need to generate a complete list of dates from an arbitrary start date, say #1/1/2015#, to now, Date(). I would like this list to live in its own table, although a query would work, too. I also would prefer to only grab business days/week days. Can anyone help?
I'm tracking business process errors by date. A new error record is made for each error, and each record is tagged with a date. However, there is not an error on every date. So reporting over time does not give a correct visualization, as the dates without errors are not represented.
I appreciate help generating this list in Access, as well as any alternative ideas for representing this information.
Cheers,
Burgess
Update - I've been able to make a list of dates since a start date. Here's my code:
Sub createDatesTable()
'Declare variables'
Dim startDate As Date
Dim endDate As Date
Dim countDate As Date
Dim length As Long
Dim i As Long
Dim dates() As Date
'Initialize'
startDate = #6/23/2015#
endDate = #9/1/2015#
countDate = startDate
length = endDate - startDate
'Define date array length'
ReDim dates(1 To length)
'Generate date list in array'
For i = 1 To (length)
dates(i) = countDate
countDate = countDate + 1
Next i
'Print array to Immediate Window'
For i = 1 To length
Debug.Print dates(i)
Next i
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("tblDateList", dbOpenDynaset)
'Add array data to existing table'
For i = 1 To length
rs.AddNew
rs!DateList = dates(i)
rs.Update
Next i
rs.Close
db.Close
End Sub
Now, I'm very new to VBA, so this may well contain elementary mistakes. Thanks for the feedback.
-Burgess
Your table can have a structure like this:
Column name | Data type
---------------+----------
dt | DateTime
isBusinessDay | Yes/No
And as for the filling, something like this:
public sub fillDates(startDate as date, endDate as date)
dim db as DAO.database, rs as DAO.Recordset
dim strSQL as string
dim d as date
strSQL = "delete from tbl_dates"
docmd.runsql strSQL
set db = currentdb()
set rs = db.openRecordset("tbl_dates", dbOpenDynaset, dbAppendOnly)
for d = startDate to endDate
with rs
.addNew
![dt] = d
![isBusinessDay] = (format(d, "w", vbMonday) <= 5)
.update
end with
next d
rs.close
db.close
end sub

Count X Amount of Working Days from Date Entered

I have a Microsoft Access database where the user is required to enter a Date Opened: value. Once entered, this triggers a calculation in another field, Deadline (25 WD):. This works via the following function in the latter field:
=DateAdd("d",25,[Date opened])
What I want to do, however, is to count 25 working days from the date entered in Date Opened:. I have a table holidays which contains a list of UK holidays up until 2020.
How can I merge to two, so-to-speak, in order to produce a valid Deadline (25 WD): value which does not count any of the dates listed in holidays?
For example, if the date entered is 01/01/2015, then the function would count 25 working days from 01/01/2015, meaning that it would exclude all weekends and any bank holidays that fall within that period and the resulting date value in the field Deadline (25 WD) will also be a working day (i.e. not a weekend or bank holiday).
You might need a UDF to get you through this. Something like,
Function addWorkDays(addNumber As Long, Date2 As Date) As Date
'********************
'Code Courtesy of
' Paul Eugin
'********************
Dim finalDate As Date
Dim i As Long, tmpDate As Date
tmpDate = Date2
i = 1
Do While i <= addNumber
If Weekday(tmpDate) <> 1 And Weekday(tmpDate) <> 7 And _
DCount("*", "tbl_BankHolidays", "bankDate = " & Format(tmpDate, "\#mm\/dd\/yyyy\#")) = 0 Then i = i + 1
tmpDate = DateAdd("d", 1, tmpDate)
Loop
Do While Weekday(tmpDate) = 1 Or Weekday(tmpDate) = 7 Or _
DCount("*", "tbl_BankHolidays", "bankDate = " & Format(tmpDate, "\#mm\/dd\/yyyy\#")) <> 0
tmpDate = DateAdd("d", 1, tmpDate)
Loop
addWorkDays = tmpDate
End Function
So, when you add 25 days to a date, it will skip all weekends and bank holidays stored in your table - tbl_BankHolidays.
? addWorkDays(25, Date())
25/06/2015
Hope this helps !
EDIT: I have added another loop to see if the end date falls on a bank holiday or weekend, if it does it will add one more day until it reaches a weekday.
You can use this function:
Public Function DateAddWorkdays( _
ByVal lngNumber As Long, _
ByVal datDate As Date, _
Optional ByVal booWorkOnHolidays As Boolean) _
As Date
' Adds lngNumber of workdays to datDate.
' 2014-10-03. Cactus Data ApS, CPH
' Calendar days per week.
Const clngWeekdayCount As Long = 7
' Workdays per week.
Const clngWeekWorkdays As Long = 5
' Average count of holidays per week maximum.
Const clngWeekHolidays As Long = 1
' Maximum valid date value.
Const cdatDateRangeMax As Date = #12/31/9999#
' Minimum valid date value.
Const cdatDateRangeMin As Date = #1/1/100#
Dim aHolidays() As Date
Dim lngDays As Long
Dim lngDiff As Long
Dim lngDiffMax As Long
Dim lngSign As Long
Dim datDate1 As Date
Dim datDate2 As Date
Dim datLimit As Date
Dim lngHoliday As Long
lngSign = Sgn(lngNumber)
datDate2 = datDate
If lngSign <> 0 Then
If booWorkOnHolidays = True Then
' Holidays are workdays.
Else
' Retrieve array with holidays between datDate and datDate + lngDiffMax.
' Calculate the maximum calendar days per workweek.
lngDiffMax = lngNumber * clngWeekdayCount / (clngWeekWorkdays - clngWeekHolidays)
' Add one week to cover cases where a week contains multiple holidays.
lngDiffMax = lngDiffMax + Sgn(lngDiffMax) * clngWeekdayCount
datDate1 = DateAdd("d", lngDiffMax, datDate)
aHolidays = GetHolidays(datDate, datDate1)
End If
Do Until lngDays = lngNumber
If lngSign = 1 Then
datLimit = cdatDateRangeMax
Else
datLimit = cdatDateRangeMin
End If
If DateDiff("d", DateAdd("d", lngDiff, datDate), datLimit) = 0 Then
' Limit of date range has been reached.
Exit Do
End If
lngDiff = lngDiff + lngSign
datDate2 = DateAdd("d", lngDiff, datDate)
Select Case Weekday(datDate2)
Case vbSaturday, vbSunday
' Skip weekend.
Case Else
' Check for holidays to skip.
' Ignore error when using LBound and UBound on an unassigned array.
On Error Resume Next
For lngHoliday = LBound(aHolidays) To UBound(aHolidays)
If Err.Number > 0 Then
' No holidays between datDate and datDate1.
ElseIf DateDiff("d", datDate2, aHolidays(lngHoliday)) = 0 Then
' This datDate2 hits a holiday.
' Subtract one day before adding one after the loop.
lngDays = lngDays - lngSign
Exit For
End If
Next
On Error GoTo 0
lngDays = lngDays + lngSign
End Select
Loop
End If
DateAddWorkdays = datDate2
End Function
Public Function GetHolidays( _
ByVal datDate1 As Date, _
ByVal datDate2 As Date, _
Optional ByVal booDesc As Boolean) _
As Date()
' Finds the count of holidays between datDate1 and datDate2.
' The holidays are returned as an array of dates.
' DAO objects are declared static to speed up repeated calls with identical date parameters.
' 2014-10-03. Cactus Data ApS, CPH
' The table that holds the holidays.
Const cstrTable As String = "tblHoliday"
' The field of the table that holds the dates of the holidays.
Const cstrField As String = "HolidayDate"
' Constants for the arrays.
Const clngDimRecordCount As Long = 2
Const clngDimFieldOne As Long = 0
Static dbs As DAO.Database
Static rst As DAO.Recordset
Static datDate1Last As Date
Static datDate2Last As Date
Dim adatDays() As Date
Dim avarDays As Variant
Dim strSQL As String
Dim strDate1 As String
Dim strDate2 As String
Dim strOrder As String
Dim lngDays As Long
If DateDiff("d", datDate1, datDate1Last) <> 0 Or DateDiff("d", datDate2, datDate2Last) <> 0 Then
' datDate1 or datDate2 has changed since the last call.
strDate1 = Format(datDate1, "\#yyyy\/mm\/dd\#")
strDate2 = Format(datDate2, "\#yyyy\/mm\/dd\#")
strOrder = Format(booDesc, "\A\s\c;\D\e\s\c")
strSQL = "Select " & cstrField & " From " & cstrTable & " " & _
"Where " & cstrField & " Between " & strDate1 & " And " & strDate2 & " " & _
"Order By 1 " & strOrder
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
' Save the current set of date parameters.
datDate1Last = datDate1
datDate2Last = datDate2
End If
lngDays = rst.RecordCount
If lngDays = 0 Then
' Leave adatDays() as an unassigned array.
Else
ReDim adatDays(lngDays - 1)
' As repeated calls may happen, do a movefirst.
rst.MoveFirst
avarDays = rst.GetRows(lngDays)
' rst is now positioned at the last record.
For lngDays = LBound(avarDays, clngDimRecordCount) To UBound(avarDays, clngDimRecordCount)
adatDays(lngDays) = avarDays(clngDimFieldOne, lngDays)
Next
End If
' DAO objects are static.
' Set rst = Nothing
' Set dbs = Nothing
GetHolidays = adatDays()
End Function

Date Format changes when inserting into table if start date and end date cross one or more months

I have an Access 2013 form which has two unbound date fields, FromDate and ToDate. I insert these into a table (TblGuestBooking) which has an autonumber key field, so this doesn't feature in the SQL statement to follow.
If the FromDate and ToDate are in the same month, the dates are entered as dd/mm/yy, the format of the form field. If, however, the From date is in one month and the to date is in the next month or later month, the format changes to mm/dd/yy for the subsequent months.
for example 26/2/14 to 3/3/14 results in the following table entries:
26/02/14,
27/02/14,
28/02/14,
03/01/14,
03/02/14,
03/03/14
This is the code snippet I am using to put the dates into the table (BookingID is obtained from the form.)
Dim BookingDate As Date
Dim SQLString As String
....
BookingDate = FromDate
Do
SQLString = "INSERT INTO TblGuestBooking ([BookingDate], [BookingID]) VALUES (#" & BookingDate & "#" & "," & Me.GuestSuiteBookingID & ")"
DoCmd.SetWarnings False
DoCmd.RunSQL SQLString
DoCmd.SetWarnings True
BookingDate = BookingDate + 1
Loop Until BookingDate = ToDate + 1
If you've read this far, thank you for your time. If you can help me out, many, many thanks.
When processing date literals (text values enclosed by "hash marks" #) Access SQL will always interpret ambiguous xx-yy-zzzz dates as mm-dd-yyyy, regardless of the regional setting in place on the computer. So, if your machine is configured to display short dates as dd-mm-yyyy and you create an Access query that uses #04-02-2014# it will always be interpreted as April 2, not February 4.
The solution is to always format date literals as unambiguous yyyy-mm-dd values. In your case, instead of
... VALUES (#" & BookingDate & "#" ...
you would use something like
... VALUES (#" & Format(BookingDate, "yyyy-mm-dd") & "#" ...
In my case I've used following code for an ASP page that create and update a Date field in Access database.
strDay = Request("Day")
strMonth = Request("Month")
strYear = Request("Year")
InputDate = strYear & "-" & strMonth & "-" & strDay
if IsDate(InputDate) Then
InsertDate = CDate(InputDate)
else
'if date typed wrong, use Todays Date
InsertDate = Date()
end if
SQLUPD = "UPDATE MyDataBase SET DateField = '"& FormatDateTime(InsertDate, vbShortDate) &"'"
SQLINS = "INSERT INTO MyDataBase (DateField, Alist, Atype, Acomment) VALUES ('"& FormatDateTime(InsertDate, vbShortDate) &"', .....
Then all sorting and output opration with dates are going well.