I have the following code and have been asked to remove the user input box and automate pulling in files. One issue is if the current day is Monday, we would need Friday's date pulled in (not yesterday). I am sorry for being extremely basic, but I believe I would need to replace InputBox("Input Date") with something like DateAdd(Day, DateDiff(Day, 1, GETDATE()), 0) - but it does not like Day or DD.
Any ideas? (and thank you for being patient with me in advance)
DoCmd.SetWarnings False
Dim InvDateStr As String
Dim InvDate As Date
Dim Directory_Sheets As String
Dim filename_sheets As String
Dim db As Database
Dim rst As DAO.Recordset
Dim startdate As Date
Dim EndDate As Date
'startdate = #9/23/2010#
'enddate = #1/12/2011#
'InvDate = startdate
'Do Until InvDate = enddate
InvDateStr = InputBox("Input Date")
If InvDateStr = "" Then
Exit Sub
End If
InvDate = CDate(InvDateStr)
Directory_Sheets = FilePath & "Inventory_Surveys\"
On Error Resume Next
' Pull Stores with Inventory
I'm only going to do the date part, you can implement it:
InvDate = Date - 1 'Yesterday
Do While Weekday(InvDate, vbMonday) > 5 'Saturday or Sunday
InvDate = InvDate - 1
Loop
Related
I need help, because when I run de code, Error appears when some date field is empty. I have a table with information and I run this code since the generator.
Eliminate #Error when I run the code vba in ACCESS
I will grateful for you help.
Option Compare Database
Public Function WorkingDays2(FECHA_DE_VALIDACION_FA As Date, FECHA_IMPRESIÓN As Date) As Integer
'....................................................................
' Name: WorkingDays2
' Inputs: StartDate As Date
' EndDate As Date
' Returns: Integer
' Author: Arvin Meyer
' Date: May 5,2002
' Comment: Accepts two dates and returns the number of weekdays between them
' Note that this function has been modified to account for holidays. It requires a table
' named tblHolidays with a field named HolidayDate.
'....................................................................
Dim intCount As Integer
Dim rst As DAO.Recordset
Dim DB As DAO.Database
Set DB = CurrentDb
Set rst = DB.OpenRecordset("SELECT [DIAFESTIVO] FROM DIASFESTIVOS", dbOpenSnapshot)
'StartDate = StartDate + 1
'To count StartDate as the 1st day comment out the line above
intCount = 0
Do While FECHA_DE_VALIDACION_FA <= FECHA_IMPRESIÓN
rst.FindFirst "[DIAFESTIVO] = #" & FECHA_DE_VALIDACION_FA & "#"
If Weekday(FECHA_DE_VALIDACION_FA) <> vbSunday And Weekday(FECHA_DE_VALIDACION_FA) <> vbSaturday Then
If rst.NoMatch Then intCount = intCount + 1
End If
FECHA_DE_VALIDACION_FA = FECHA_DE_VALIDACION_FA + 1
Loop
WorkingDays2 = intCount
Exit_WorkingDays2:
Exit Function
WorkingDays2 = intCount
Exit_WorkingDays2:
Exit Function
Err_WorkingDays2:
Select Case Err
Case Else
MsgBox Err.Description
Resume Exit_WorkingDays2
End Select
End Function
It depends a bit where you are calling this function from within your database. Probably as a calculated field in a query? Something like this:
WorkingDays: WorkingDays3([YourDateField])?
Try this instead:
WorkingDays: WorkingDays3(Nz([YourDateField],Date())
Your original question included code for a function named WorkingDays3, which takes one date parameter.
Your illustration shows a function named WorkingDays2, which takes two date parameters.
I think you will need to give more detailed information about the data you are working with, and under which conditions you are seeing the #Error.
I am making an Access form that will allow the user to process contract upgrades, and within the form I have a button that changes 3 dates for each contract, the date connected, end date, and earliest upgrade date.
When changing the end date, I use this code to actually change the date:
[End Date] = DateAdd("m", Number, FirstDate)
The end date being the date I want to change, then adding to the date using DateAdd, changing the month of the date, with a number that the user inputs, usually 24, then the FirstDate is the current end date. FirstDate is set as a variable at the beginning of the code.
What I want to change it to is this:
[End Date] = DateAdd("m", Number, Startdate)
Where it does everything the same, however using a different variable that is used earlier in the code to change the start date of the contract, which does work in that section of code, but when I try to use it again here the code crashes.
This also still happens even when I change the Startdate to [Date Connected].
Can anyone help me figure why the code won't use the Startdate variable a second time in the section of code after the first one.
This is the whole private sub that the code takes place in, this being the new code I want to use:
Private Sub cmdUpgrade_Click()
Dim Startdate As String
Dim Number As String
Dim Tariff As String
Startdate = InputBox("Enter Start Date (00/00/0000):", "Inputbox")
If StrPtr(Startdate) = 0 Then
MsgBox "You pressed Cancel" 'Option 1
Else
If Startdate = "" Then
MsgBox "Input Left Empty" ' Option 2
Else
[Date Connected] = Startdate
End If
End If
Number = InputBox("Enter Contract Length + Rollover:", "Inputbox")
If StrPtr(Number) = 0 Then
MsgBox "You pressed Cancel" 'Option 1
Else
If Number = "" Then
MsgBox "Input Left Empty" ' Option 2
Else
**[End Date] = DateAdd("m", Number, Startdate)**
[Earliest Upgrade] = DateAdd("d", -180, [End Date])
End If
End If
Tariff = InputBox("Enter Tarrif", "Inputbox")
If StrPtr(Tarrif) = 0 Then
MsgBox "You pressed Cancel" 'Option 1
Else
If Tariff = "" Then
MsgBox "Input Left Empty" ' Option 2
Else
[Current Talk Plan] = Tariff
End If
End If
Forms!frmForecast.Form.Requery
DoCmd.SearchForRecord , , acFirst, "Year([Earliest Upgrade]) = Year(Date())
And Month([Earliest Upgrade]) = Month(Date())"
End Sub
Always handle dates as dates, not strings. Also, it doesn't matter if the user presses Cancel or OK for an empty input:
Dim Startdate As Date
Dim Enddate As Date
Dim TestDate As String
Dim Number As String
Dim Tariff As String
TextDate = InputBox("Enter Start Date (00/00/0000):", "Inputbox")
If TextDate = "" Then
MsgBox "No date entered" 'Option 1
ElseIf Not IsDate(TextDate) Then
MsgBox "Please enter a valid date" ' Option 2
Else
Startdate = DateValue(TextDate)
[Date Connected] = Startdate
End If
<snip>
Enddate = DateAdd("m", Number, Startdate)
[End Date] = Enddate
[Earliest Upgrade] = DateAdd("d", -180, Enddate)
I'm attempting to create a function to add n workdays to a date. This obviously needs to take into account weekends and holidays. The weekend part works fine, but I used a table to list all of the holidays and I can't get my query to pull the accurate recordCount. It insists the count is always zero. Here is my code:
Public Function dateAddNoWeekends(dt1 As Date, genDate As Integer)
Dim i As Integer
Dim date1 As Date
Dim date2 As Date
Dim holidays As Boolean
Dim isWeekday As Boolean
Dim dayOfWeek As Integer
Dim numDays As Integer
Dim db As Database
Dim rs As DAO.Recordset
Dim sql As String
Set db = CurrentDb
date1 = dt1
date2 = date1
i = 0
numDays = genDate
sql = "SELECT * FROM Holidays WHERE holidayDate = " & date2
Set rs = db.OpenRecordset(sql)
Debug.Print date2
Do While i < numDays
If Not isWeekday Then date2 = date2 + 1
dayOfWeek = Weekday(date2)
If rs.RecordCount > 0 Then
Debug.Print rs.RecordCount
holidays = True
Else
holidays = False
Debug.Print rs.RecordCount
End If
If dayOfWeek > 1 And dayOfWeek < 7 And holidays = False Then
If i < numDays - 1 Then date2 = (date2 + 1)
isWeekday = True
i = i + 1
Else: isWeekday = False
End If
Loop
rs.Close
Debug.Print date2
To solve this, I declared a variable at the top of the loop (so that it would update correctly each time) and assigned a Dcount function to count from date2 to date2.
I have tried every suggestion I can find on forums but my code is getting messy!
I am using Microsoft Access (Office 365) my computer is in French, I have English installed as a secondary language.
I want to find all sales records from tblEtsySales for the second quarter of the year. I am summing the records from the field TotalReceived using DSUM and want to filter by date. I get an unexpected amount returned when it should be zero.
Here is my code:
Dim Q2 As Currency
Dim Quarter2 As String
Quarter2 = "[Sale Date] BETWEEN #01/04/2017# And #30/06/2017#"
If IsNull(Q2 = DSum("[TotalReceived]", "tblEtsySales", Quarter2)) = True Then
MsgBox "No data for the second quarter."
txtQ2.Value = ""
Else
Q2 = DSum("[TotalReceived]", "tblEtsySales", Quarter2)
txtQ2.Value = Q2
End If
Your expression #01/04/2017# is read as 2017-01-04.
So do this:
Dim Q2 As Currency
Dim Quarter2 As String
Dim Date1 As Date
Dim Date2 As Date
Date1 = #04/01/2017#
Date2 = #06/30/2017# ' You can type it differently, but VBA will correct it.
Quarter2 = "[Sale Date] BETWEEN #" & Format(Date1, "yyyy\/mm\/dd") & "# And #" & Format(Date2, "yyyy\/mm\/dd") & "#"
Q2 = DSum("[TotalReceived]", "tblEtsySales", Quarter2)
If Q2 = 0 Then
MsgBox "No data for the second quarter."
txtQ2.Value = Null
Else
txtQ2.Value = Q2
End If
You'd probably be better off passing data parameters to a procedure to make this more flexible, but if you only need it for Q2 into perpetuity...
Dim Q2 As Double
Dim Quarter2 As String
Quarter2 = "[Sale Date] BETWEEN #01/04/2017# And #30/06/2017#"
Q2=DSum("[TotalReceived]", "tblEtsySales", Quarter2)
if Q2=0 then
MsgBox "No data for the second quarter."
txtQ2.Value = ""
else
txtQ2.Value=Q2
end if
I want to run a for-loop from sometime to some specific time. Let's say from the first day of the year to the last day:
I am given the year and I need to add the month and the day to it:
I am trying to concatenate into a full date string and the compiler keeps throwing errors:
dim dt as Date
dim rs as recordset
set rs = currentdb.openRecordset("select max(date) as dan from Atbl")
for dt = #1/1/year(now)# to #2/2/Year(rs!dan)#
msgbox dt
Next
any help is appreciated. or any hint is welcome
DateSerial should make this easier.
Give it values for year, month, and day in that order, and it will give you back the corresponding date as a Date/Time value.
for dt = DateSerial(Year(Date), 1, 1) to DateSerial(rs!dan, 2, 2)
msgbox dt
Next
Nevermind, I found a solution quite fast: This works fine
dim dt as Date
dim rs as recordset
set rs = currentdb.openRecordset("select max(date) as dan from Atbl")
'save the first day as string first
Dim firstDay As String
firstDay = "1/1/" & Year(Now)
'convert to date
Dim firstDate As Date
firstDate = CDate(firstDay)
'save as string
Dim lastDay as string
lastDay = "2/2/" & Year(rs!dan)
'convert to date
Dim lastDate As Date
lastDate = CDate(lastDay)
'works fine in here
for dt = firstDate to lastDate
msgbox dt
next