I have a user inputed date format of mm/dd/yyyy. I want to use this input in a query to act as a filter (start and end time). The actual date stored in the datebase was set with now(), so it has the format mm/dd/yyyy XX:XX:XX AM/PM.
How can I use these inputed dates in my filter? (When I tried out an mm/dd/yyyy input I was returned a Report with no values, but if I added the time, it worked, but I don't want the user to have to enter the time.)
Right now I am using simple input boxes for the input. I really would like to create a calendar popup (and I see tutorials online). Would the solution change if I changed over to a Calendar?
EDIT:
Here is my code, to make it eaiser to understand the issue.
Dim startDate As Date
Dim endDate As Date
startDateString = InputBox("Enter a start date.")
endDateString = InputBox("Please enter and end date.")
Reports![rpt_Inventory_Update].RecordSource = "MyQuery"
Reports![rpt_Inventory_Update].Filter = "Modification_Date BETWEEN #" & startDate & "# AND #" & endDate & "#"
Reports![rpt_Inventory_Update].FilterOn = True
When you compare to the field with Now(), you could change it to be compared to DateValue(Now())
Now() = Date() returns False
DateValue(Now()) = Date() returns True
I figured it out. This is my new code:
startTime = TimeValue("00:00:00")
endTime = TimeValue("23:59:59")
startDate = startDate + startTime
endDate = endDate + endTime
Reports![rpt_Inventory_Update].RecordSource = "MyQuery"
Reports![rpt_Inventory_Update].Filter = "Modification_Date BETWEEN #" & startDate & "# AND #" & endDate & "#"
Reports![rpt_Inventory_Update].FilterOn = True
This makes sure that the start and end dates in the query have a full time stamp, and thus the query works!
Related
I am having trouble comparing dates. The if statement in my code is saying the endDate is not less even though it is. For example, if the endDate is 2/20/2017 and startDate 2/22/2017 the if statement says endDate is not lest but it is.
Dim startDate As Date
Dim endDate As Date
startDate = DateValue(Me.dueDateTxt)
endDate = DateValue(Me.shippedDate)
If (endDate < startDate) Then
Debug.Print "It is less"
Else
Debug.Print "not less"
End If
I have also tried
If Me.dueDateTxt < Me.ShippedDate Then
If CDate(startDate) < CDate(endDate) Then
If Format(startDate, "mm/dd/yyyy") < Format(endDate, "mm/dd/yyyy") Then
If DateDiff(d, startDate, endDate) > 0 Then
I am missing something somewhere thank you in advance for the help!
EDIT: I figured out what the problem was. I have a function to eliminate holidays and weekends. That function was swaping my startDate and endDate. Thank you everyone for your help and suggestions.
Your first block of code works fine for me.
Have you set the format of the text boxes(?) as 'Short Date' for example?
I have this few line of codes where I save the date from datetimepicker to my database column REMINDER.
myCommand.CommandText = "Update kup_table SET REMINDER = #reminder Where ID = #theIDD"
myCommand.Parameters.AddWithValue("#reminder", DateTimePicker1.Value.Date)
myCommand.Parameters.AddWithValue("#theIDD", theID)
myCommand.ExecuteNonQuery()
In my database, the date is saved in this format 2015-12-14 00:00:00 since the datatype is DATETIME.
How do I compare it with the date now? If the saved date and today date are a match, then a reminder will go off.
I have tried using this sql command but having still having trouble where the reminder is always zero. Thanks in advance.
myCommand.CommandText = "Select COUNT(*) from kup_table Where REMINDER BETWEEN DATE() AND DATEADD('d', 1, DATE())"
mySqlConn.Open()
Console.WriteLine("Connected.")
count = myCommand.ExecuteScalar()
MsgBox("You have " + count.ToString + " reminder(s).")
Select COUNT(*) from kup_table Where DATE(REMINDER) = DATE(NOW())
The DATE() function returns the date part of the datetime only. (e.g. 2015-12-14)
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date
There is a table in MS Access having 3 columns:
ID (Primary key)
Date (Date/Time)
Train no (short text)
I have designed a form where date can be selected from a combo box and according to the date selected, respective trains should be displayed.
Problem is, the query works fine on dates above 10 (like 11/1/2015) but below 10 (like 9/1/2015) it gives error : "No current record" . The record is there in the table but it doesn't display.
The query is : SELECT DISTINCT [Train No] FROM Issue WHERE [Date] = #" & dt & "#"
dt is the date selected from the combo box.
Try this:
Dim DateSelected As Date
Dim DateString As String
DateSelected = DateValue(Me!YourComboBox.Value)
DateString = Format(DateSelected, "yyyy\/mm\/dd")
SELECT DISTINCT [Train No] FROM Issue WHERE [Date] = #" & DateString & "#"
I have a vb.net program that queries a mysql database. i can search and get records between to dates with this code:
sqlQRY12 = "SELECT * from mating WHERE date BETWEEN '" & export_daily_date_DateTimePicker1.Text & "' AND '" & export_daily_date_DateTimePicker2.Text & "' AND chkbox = '0' ORDER BY lot_id ASC"
my format for my date is:
export_daily_date_DateTimePicker1.Format = DateTimePickerFormat.Custom
export_daily_date_DateTimePicker1.CustomFormat = "MM/dd/yy"
export_daily_date_DateTimePicker2.Format = DateTimePickerFormat.Custom
export_daily_date_DateTimePicker2.CustomFormat = "MM/dd/yy"
however if i try to search between two years like 12/20/13 - 02/20/14 I return no records when i know they exist? Any help would be great ty
Generally dates expressed as strings in database queries should be in the format "YYYY-MM-DD".
You essentially want your executed query to be this:
SELECT * from mating
WHERE date BETWEEN '2013-12-20' AND '2014-02-20'
So change the format of your dtpickers to be yyyy-mm-dd like this:
export_daily_date_DateTimePicker1.Format = DateTimePickerFormat.Custom
export_daily_date_DateTimePicker1.CustomFormat = "yyyy-MM-dd"
export_daily_date_DateTimePicker2.Format = DateTimePickerFormat.Custom
export_daily_date_DateTimePicker2.CustomFormat = "yyyy-MM-dd"
You should never concatenate values into your SQL commands. If at all possible, you should use parameters. With parameters, you can specify the value as it's actual type (Date) rather than as the string representation. The ADO Provider will handle converting the value correctly for you.
cmd.CommandText = "SELECT * from mating WHERE date BETWEEN #date1 AND #date2 AND chkbox = '0' ORDER BY lot_id ASC"
cmd.Parameters.AddWithValue("#date1", export_daily_date_DateTimePicker2.Value)
cmd.Parameters.AddWithValue("#date2", export_daily_date_DateTimePicker2.Value)
I have an Access Form - lets call it "Add Labor" (Access 2007) that saves data into a table.
The table has two columns in particular called "Start Date" and "End Date" (This table stores tasks)
There is also another table called FiscalYears which includes Start and End Dates for Fiscal Years, which is structured as follows
FyID
FYear
StartDate
EndDate
Example Data:
FYId FYear StartDate EndDate
-----------------------------
1 2010 10/1/2009 9/30/2010
2 2011 10/1/2010 9/30/2011
So in My Add Labor Form if someone enters labor that span across two fiscal years I need to enter two labor entries. Here is an example
If a user selects Labor Start Date = 6/30/2009
And End Date 10/2/2010 , it spans two fiscal years
So in my Labor Table I should enter two things
LaborID StartDate EndDate
-----------------------------
1 6/30/2009 9/30/2010
2 10/1/2010 10/2/2010
Basically I need to do a check before I save the record and add two records if they span Fiscal years, right now I'm just blindly doing Save Record on the form (inbuilt), but I guess I need to add some VBA. I've hardly ever used Access so this may be simple(hopefully). I am thinking instead of the event which just calls Save Record, I need it to add custom VBA.
Say you have an unbound form for adding the dates, you can say:
Dim rsFY As DAO.Recordset
Dim rsAL As DAO.Recordset
Dim db As Database
Dim sSQL As String
Set db = CurrentDb
''Select all years from the fiscal years table
sSQL = "SELECT FYear, StartDate, EndDate " _
& "FROM FiscalYears WHERE StartDate>=#" & Format(Me.StartDate, "yyyy/mm/dd") _
& "# Or EndDate <=#" & Format(Me.Enddate, "yyyy/mm/dd") _
& "# ORDER BY FYear"
Set rsFY = db.OpenRecordset(sSQL)
Set rsAL = db.OpenRecordset("AddLabor") ''table
''Populate recordset
rsFY.MoveLast
rsFY.MoveFirst
Do While Not rsFY.EOF
''Add records for each year selected
rsAL.AddNew
If rsFY.AbsolutePosition = 0 Then
rsAL!StartDate = Format(Me.StartDate, "yyyy/mm/dd")
Else
rsAL!StartDate = rsFY!StartDate
End If
If rsFY.AbsolutePosition + 1 = rsFY.RecordCount Then
rsAL!Enddate = Format(Me.Enddate, "yyyy/mm/dd")
Else
rsAL!Enddate = rsFY!Enddate
End If
rsAL.Update
rsFY.MoveNext
Loop
If the code was running in a main form with a subform showing the Addlabor table, you could update the subform to show the new records like so:
Me.Addlabor_subform.Requery
Why do you need a FiscalYears table? If your organization's fiscal years always start on Oct. 1 and end on Sept. 30, you can use a function to determine the fiscal year for a given date.
Public Function Fy(ByVal pDate As Date) As Integer
Dim intYear As Integer
Dim intReturn As Integer
intYear = Year(pDate)
If pDate > DateSerial(intYear, 9, 30) Then
intReturn = intYear + 1
Else
intReturn = intYear
End If
Fy = intReturn
End Function
And simple functions to return the Start and End dates for a given year.
Public Function FyStart(ByVal pYear As Integer) As Date
FyStart = DateSerial(pYear - 1, 10, 1)
End Function
Public Function FyEnd(ByVal pYear As Integer) As Date
FyEnd = DateSerial(pYear, 9, 30)
End Function
You can then determine how many fiscal years are included in a given date range by:
Fy(EndDate) - Fy(StartDate)
But I may be totally off base because you said "Start Date = 6/30/2009 And End Date 10/2/2010" spans two years. However, this expression returns 2 (3 years):
Fy(#10/2/2010#) - Fy(#6/30/2009#)