how to make query by date in access 2013? - ms-access

I made access application and i have query build on date criteria
this is my query
Set sales = CurrentDb.OpenRecordset("Select * From sales where action_date = #" & date_actions & "#")
I change the date in windows to dd/mm/yyyy
But when i try to run this query nothing happens
But when i change it to default MM/d/yyyy
It run correctly
How to solve this problem? Please, and thanks in advance

I suggest use this format: YYYY-MM-DD HH:MM:SS
You can convert your date as follows:
Format(date_actions, "yyyy-mm-dd hh:mm:ss")
Then your statement will be:
Set ftm_date = Format(date_actions, "yyyy-mm-dd hh:mm:ss")
Set sales = CurrentDb.OpenRecordset("Select * From sales where action_date = #" & ftm_date & "#")

JET engine deals date in American format, not the regular DD/MM/YYYY HH:NN:SS. So you need to format the dates accordingly.
Set sales = CurrentDb.OpenRecordset("SELECT * " & _
"FROM " & _
"sales " & _
"WHERE action_date = " & Format(date_actions, "\#mm\/dd\/yyyy\#"))
Hope this helps.

Related

MS access update query issue with date range

I'm using this update query in MS access 2016
Update sampledata set VALUE= '" & Me.value & "' where Day between '" & Me.dayfrom & "' and '" & Me.dayto & "';
The strangest problem m facing is- It is considering the form values for start and end date but however updates records only for the start date. Example. If dayfrom is 01-Nov-2021 and dayto is 30-Nov-2021, the query is updating records of only 01-Nov-2021.
When I pass the day from as 30-Nov-2021, it is updating records for the whole month.
Note: This doesn't happen when I directly pass the values in the query, it happens only when i Pick data from FORM and apply it in query.
UPDATE table3
SET table3.status = "YES"
WHERE (((table3.transactiondate)>=[FORM]![Form3]![startdate] And
(table3.transactiondate)<=[FORM]![Form3]![enddate]));
When you run the query, two pop up input box will open, in the first one, enter the value for start date, e.g 01/01/2022 , in the second one enter the end date e.g 02/28/2022. Do check the date format in use by your system so you can be sure you are entering the date parameter in the right format.
As is, the date values will be casted to strings using your Windows settings.
So, force a format on the string expressions to have true string expressions for the date values:
Sql = "Update sampledata " & _
"Set VALUE = '" & Me!value.Value & "' " & _
"Where Day Between #" & Format(Me!dayfrom.Value, "yyyy\/mm\/dd") & "# And #" & Format(Me!dayto.Value, "yyyy\/mm\/dd") & "#;"

MSAccess VBA, filtering query results via combo box (text)

I want to filter date values (text) in a query based on a comboBox text value.
Here is what I have:
ComboBoxTimePeriod (ID [num], TimePeriod [text]).
In ComboBox I select Time period, like "16.10.2017-15.11.2017".
In a query I have Date field with single date, like "20.10.2017" (text).
What I want is to write an SQL code which searches for all records with date within TimePeriod range.
So far Idea is to extract SatrtDate and EndDate from TimePeriod like this:
Dim strStartDate As String
Dim strEndDate As String
strStartDate = Mid(Me.cboTimePeriod.Text, 1, 10)
strEndDate = Mid(Me.cboTimePeriod.Text, 12, 10)
Now from TimePeriod "16.10.2017-15.11.2017" I have StartDate (16.10.2017) and EndDate (15.11.2017).
And I want to filter all records which have date within those two dates.
This is where I need you guys.
Note that this is different issue from my last question, where I searched for a records with TimePeriodID (Subform filtering based off multiple parameters (Combobox AND Textbox)).
Always handle dates as Date, not text:
Dim StartDate As Date
Dim EndDate As Date
StartDate = DateValue(Split(Me.cboTimePeriod.Value, "-")(0))
EndDate = DateValue(Split(Me.cboTimePeriod.Value, "-")(1))
The build your filter:
... " Between #" & Format(StartDate, "yyyy\/mm\/dd") & "# And #" & Format(EbdDate, "yyyy\/mm\/dd") & "#"
To filter a form:
Me.Filter = "[Date Field] Between #" & Format(StartDate, "yyyy\/mm\/dd") & "# And #" & Format(EbdDate, "yyyy\/mm\/dd") & "#"
Me.FilterOn = True
And do make sure that your Date Field is of data type Date, not Text.

VB mysql date search is not working

Hello I am using VB6 and mysql and I am facing a problem in searching through dates its not fetching records
Here is my code
Dim GetDateNow As Date
Dim GetDateTen As String
Dim SetDateTen As Date
GetDateNow = Format$(Now, "yyyy-mm-dd")
GetDateTen = Now - 15
SetDateTen = Format$(GetDateTen, "yyyy-mm-dd")
Dim rs As New Recordset
SQL = "SELECT * FROM CreditPayLog WHERE payment_user='" & Label5.Caption & "' AND payment_date BETWEEN '" & Format(SetDateTen, "yyyy-MM-dd") & "' AND '" & Format(GetDateNow, "yyyy-MM-dd") & "'"
Also i have used debug.print function to see the query all looks good but its not fetching data here is the query
SELECT * FROM CreditPayLog WHERE payment_user='1222150322' AND payment_date BETWEEN '2016-04-06' AND '2016-04-21'
payment_date is DATETIME column in mysql
Your sql is creating a text/string value for date... you need to cast as a date. Not sure how in mySQL but in MSSql you would use Convert(datetime, 'yourDateAsString', 103)

vb.net get database data between two date

I have two datetimepicker, startDate is stored datetimepicker1 value and endDate is stored datetimepicker2 value.
I want to to get the data between startDate and endDate from database.
Dim bSql As String = "select date, sum(total_price) from bill where Date = '" & Format(startDate, "yyyy/MM/dd") & " and Date='" & Format(endDate, "yyyy/MM/dd") & "'"
i tried the code above but it can't work. Anyone can help me?
If you're trying to find a string format for a date at all, you've already lost. Try this:
Dim bSql As String = "select date, sum(total_price) from bill where Date >= #startDate and Date < #endDate;"
Using cn As New MySqlConnection("connection string here"), _
cmd As New MySqlCommand(bSql, cn)
cmd.Parameters.Add("#startDate", SqlDbType.DateTime).Value = startDate
cmd.Parameters.Add("#endDate", SqlDbType.DateTime).Value = endDate.AddDays(1)
cn.Open()
'...
End Using
No formatting required or wanted.
Try this, using the SQL BETWEEN operator, which allows you to specify the lower and upper bounds of a range.
Dim bSql As String = "select date, sum(total_price) from bill where Date BETWEEN '" & startDate.ToString("yyyy/MM/dd") & "' AND '" & endDate.ToString("yyyy/MM/dd") & "' GROUP BY date;"
You will also need to apply a grouping to use the aggregate function "SUM":
-- find all dates with sales and the total prices on each date
SELECT [date], SUM(total_price) AS [TotalPrice]
FROM bill
WHERE [date] BETWEEN '2013-01-01' AND '2013-12-31' -- use appropriate date format here
GROUP BY [date];
in data base i have DD/MM/YYYY
solution is to make in VB MM/DD/YYYY
this is a CODE
oldbc.CommandText = "select * from recette where Date between #10/09/2015# and #10/011/2015#"
Try formatting your dates like this (you will need to use the Value of the DateTimePicker as well):
Format(startDate.Value, "yyyy-MM-dd")
A better option is to use a parameterised query and then you don't have to format the date into any particular format. More info here: How do I create a parameterized SQL query? Why Should I?
Dim bSql As String = "select date, sum(total_price) from bill where Date = " & DateTimePicker1.Text & " and Date=" & DateTimePicker1.Text & ""
Set the datetime picker date format.
I hope it will helpful for you...
Another possibility is to make a little function to format DATE variable and return correct date string syntax for MySQL.
Public Function MySQLDate(inDate As Date) As String
Return "'" & inDate.ToString(format:="yyyy'/'MM'/'dd") & "'"
End Function
Now your query is more readable, easier to create, and looks like this:
Dim bSql As String = "select date, sum(total_price) from bill where Date BETWEEN " & MySQLDate(startDate) & " and " & MySQLDate(endDate)

Dlookup retrieves a date wrong

I'm trying to get a date in my database but when the day is less than 12, the month and the day are switched
Example: In the database 2012-02-10 (2 october 2012), the value I get when I do that:
lastDateMill = Nz(DLookup("LastContactDate", "Mills", "MillID = " & lstMills.Column(0, i)), 0)
is
lastDateMill = "10/02/2012"
So I thought that was only a format thing but when I do
Format(lastDateMill, "Long Date")
it equals "February-10-12"
This is how I update the date
DoCmd.RunSQL "UPDATE Mills SET LastContactDate = #" & SalesCallDate & "# WHERE MillID = " & lstMills.Column(0, i)
And the SalesCallDate = "2/10/2012" so the good date
So why the day and the month are switched?
The front end is ms-access-2010 and the back end is on SQL SERVER 2012
Your SalesCallDate variable contains a date as text:
SalesCallDate = "2/10/2012"
Apparently you intend the date format of that string to be m/d/yyyy, but it gets interpreted as d/m/yyyy format.
Store the string value in yyyy/mm/dd format to eliminate confusion due to locale issues ... 2012/02/10
Since it turns out that SalesCallDate is actually a text box containing a date value, change your UPDATE approach to avoid date problems due to locale.
Dim strUpdate As String
Dim db As DAO.Database
strUpdate = "UPDATE Mills SET LastContactDate = " & _
Format(Me.SalesCallDate, "\#yyyy-m-d\#") & vbCrLf & _
"WHERE MillID = " & Me.lstMills.Column(0, i)
Debug.Print strUpdate
Set db = CurrentDb
db.Execute strUpdate, dbFailonerror
Set db = Nothing
Try this, by explicitly specifying a format
DoCmd.RunSQL "UPDATE Mills SET LastContactDate = #" & _
Format$(SalesCallDate,"yyyy/mm/dd") & "# WHERE MillID = " & _
lstMills.Column(0, i)
UPDATE:
Maybe there is better way to do it, that is independent of any formattings. The idea is to tranfer the date from table to table, without any combo-, list- or text in between. Therefore any conversion from a date type to string and then back to a date field is avoided.
If the tables can be joined (assuming that MillID is the bound field of the listbox):
DoCmd.RunSQL "UPDATE Mills " & _
"INNER JOIN sourceTable ON Mills.MillID = sourceTable.MillID " & _
"SET LastContactDate = sourceTable.SalesCallDate " & _
"WHERE Mills.MillID = " & lstMills
Otherwise
DoCmd.RunSQL "UPDATE Mills SET LastContactDate = " & _
"(SELECT SalesCallDate FROM sourceTable WHERE ID = " & sourceID & ")" _
"WHERE Mills.MillID = " & lstMills