"Data type mismatch in criteria expression" - sql

I'm trying to get data that are equals to and between 2 dates that i type into my textbox! The column of PO_Date is of DateTime. It works perfectly when i hard code the dates into the select statement, for example PO_Date >= #12/6/2013 3:54:15 PM# AND PO_Date <= #1/1/2015 3:54:15 PM# inclusive of the DropDownList2. The error only comes once i try to implement the txtStartDate.Text and txtEndDate.Text! Help is very much appreciated!
oRs.Open("SELECT PO_Date, PO.Vendor FROM PO
WHERE PO_Date >= '#" & TxtStartDate.Text & "00:00:00 AM""#' AND
PO_Date <= '#" & TxtEndDate.Text & "00:00:00 AM""#'
AND Vendor Like '%" & DropDownList2.SelectedValue & "%'", oCnn)

May be you have typo, but you can check:
oRs.Open("SELECT PO_Date, PO.Vendor FROM PO
WHERE PO_Date >= #" & TxtStartDate.Text & " 00:00:00 AM# AND
PO_Date <= #" & TxtEndDate.Text & " 00:00:00 AM#
AND Vendor Like '%" & DropDownList2.SelectedValue & "%'", oCnn)
AM""#' => AM#
"00:00:00 => " 00:00:00

Related

MS-Access date comparison Datatype conflict

In my Project I have Orders which contain an amount of Hours which have to be invoiced by a Rate, both of them are stored in tables. The Orders have a ValidFrom date. All Orders have to be invoiced by the last Rate where the ValidFrom is before the Order.TimeStamp.
The solution that I've tried is:
OrderDate = DLookup("TimeStamp", "Order", "OrderID=" & ThisOrderID & ")
LastRateDate = DMAX ("ValidFrom", "CompanyRate", "CompanyID=" & ThisCompanyID & " AND ValidFrom <=" & OrderDate)
CompanyRate = DLookup ("Rate","CompanyRate", "CompanyID=" & ThisCompanyID & " AND "ValidFrom =" & LastRateDate)
This gives several different errors:
without semicolons (as written): Syntax error missing Operator.
with single semicolon ' Datatype Conflict
with double semicolon " (chr(34)) Datatype Conflict
I use a german Computer, with german as Windows and MS-Access language. I run the MS-Access 2013
How should I solve this?
Try this using the proper syntax:
OrderDate = DLookup("TimeStamp", "Order", "OrderID = " & ThisOrderID & "")
LastRateDate = DMax("ValidFrom", "CompanyRate", "CompanyID = " & ThisCompanyID & " AND ValidFrom <= #" & Format(OrderDate, "yyyy\/mm\/dd") & "#")
CompanyRate = DLookup("Rate", "CompanyRate", "CompanyID = " & ThisCompanyID & " AND ValidFrom = #" & Format(LastRateDate, "yyyy\/mm\/dd") & "#")

access vba docmd.applyfilter for dates

I am running a code based on a week number to filter for work order dates that match the week column and are greater than the WODate Column. My code gives me a "Syntax" Error for the second applyfilter command. Is there a way to have both filters apply at once?
my code:
Private Sub Weeks_AfterUpdate()
DoCmd.ApplyFilter , "[Week] = '" & Me.Weeks & "'"
DoCmd.ApplyFilter , "[WODate] >= #" & Format(wodate, "mm/dd/yyy") & "# and [FYDate] <= #" & Format(todate, "mm/dd/yyyy") & "#"
Me.FilterOn = True
End Sub
You miss a y, and the date separators should be escaped:
DoCmd.ApplyFilter , "[WODate] >= #" & Format(wodate, "mm\/dd\/yyyy") & "# and [FYDate] <= #" & Format(todate, "mm\/dd\/yyyy") & "#"

Get Min and Max time in date and include null dates within a single column in MS Access

I have a column named TimeLog
I want to get the min and max time in a day in a date range including null date values. Note this is Oledb/MS Access.
So far this is my query:
commstring = "
SELECT FORMAT([LogTime],'MM/dd/yyyy') AS LTime, MIN(LogTime) AS MinTime, MAX(LogTime) AS MaxTime
FROM TimeLog
WHERE (#" & fromdate & "# <= LogTime OR LogTime IS NULL)
AND (#" & todate & "# >= LogTime OR LogTime IS NULL)
AND UserID = '" & NumCmbBox.Text & "'
GROUP BY FORMAT([LogTime],'MM/dd/yyyy')
ORDER BY MAX(TimeLog.LogTime)
"
Try this old trick with Nz:
WHERE (#" & Format(fromdate, "yyyy\/mm\/dd") & "# <= Nz(LogTime, Now()))
AND (#" & Format(todate, "yyyy\/mm\/dd") & "# >= Nz(LogTime, Now()))
AND UserID = '" & NumCmbBox.Text & "'
or without:
WHERE ((#" & Format(fromdate, "yyyy\/mm\/dd") & "# <= IIf(LogTime Is Null, Now(), LogTime)
AND #" & Format(todate, "yyyy\/mm\/dd") & "# >= IIf(LogTime Is Null, Now(), LogTime))
OR LogTime Is Null)
AND UserID = '" & NumCmbBox.Text & "'
or using Between - And (tested and works here):
WHERE ((LogTime BETWEEN #" & Format(fromdate, "yyyy\/mm\/dd") & "# AND #" & Format(todate, "yyyy\/mm\/dd") & "#) OR (LogTime Is Null))
AND (UserID = '" & NumCmbBox.Text & "')
You may also try:
WHERE ((Nz(LogTime, Date()) BETWEEN #" & Format(fromdate, "yyyy\/mm\/dd") & "# AND #" & Format(todate, "yyyy\/mm\/dd") & "#) OR (LogTime Is Null))
AND (UserID = '" & Me!NumCmbBox.Value & "')

Syntax error in Microsoft Access SQL Select query in VBA procedure

I am having following VBA Code that has been giving a syntax error. Can someone please help me figure out what is causing the error?
Private Sub Command11_Click()
Dim EndingDate As Date
'Getting ending date from Label named endDate
EndingDate = endDate
StartingDateTxt = DateSerial(Year(EndingDate), Month(EndingDate) - 15, Day(EndingDate))
Dim customerRecords As New ADODB.Recordset
customerRecords.Open "SELECT COUNT(*) AS N FROM (SELECT DISTINCT E.Date,"&_
"E.[Inv Num], E.CusName, E.[Name Street1], E.[Name Street2], "&_
"E.[Name City], E.[Name State], E.[Name Zip], E.[Account #], E.Amount FROM TempFromExcel "&_
"AS E INNER JOIN TempFromExcel AS X ON E.CusName = X.CusName "&_
"WHERE (((DateDiff("d",X.Date,E.Date))>=30)) AND E.Date >= '" & StartingDateTxt & "' and"&_
"E.Date <= '" & endDate & "') AS T ;", _
CodeProject.Connection , _
adOpenStatic, _
adLockOptimistic, _
adCmdText
MsgBox customerRecords("N")
End Sub
My Query is taking both dates and finding the results that are between the two dates.
I think I may be missing at that part only. The rest seems fine as I had explicitly check the query and it runs fine. So is this right ?
E.Date >= '" & StartingDateTxt & "' and E.Date <= '" & endDate & "'
This has been corrected, in the answer but still am getting syntax error in Select statement first line. Am missing something?
In Microsoft Access SQL query you have to encapsulate Date value into ##, like for example, #06/01/2015#. Pertinent to your case it should look like:
E.Date >= #" & StartingDateTxt & "# AND E.Date <=#" & endDate & "#"
Hope this may help.
Try changing these lines:
"WHERE (DateDiff('d', X.Date, E.Date) >= 30 AND E.Date >= #" & Format(StartingDateTxt, "yyyy\/mm\/dd") & "# and " & _
"E.Date <= #" & Format(endDate, "yyyy\/mm\/dd") & "#) AS T ;", _

SQL Query Date Related SELECT Statement

This is my query which runs perfectly
"SELECT RemBal FROM Sales WHERE CustomerName='" & CustomerName.Text & "'"
now i am trying to get the balance on two basis customer name & current date
"SELECT RemBal FROM Sales WHERE CustomerName='" & CustomerName.Text & "' AND SaleDate=#" & SaleDate.Value & "#"
now this query not giving me any error but not returning any value too
please help
Try this
SaleDate.Format = DateTimePickerFormat.Custom
SaleDate.CustomFormat = "yyyy-mm-dd"
"SELECT RemBal FROM Sales WHERE CustomerName='" & CustomerName.Text & "' AND SaleDate=#" & SaleDate.Text() &"# "
SELECT rembal
FROM sales
WHERE customername = '" & CustomerName.Text & "'
AND Format(saledate, 'dd/MM/yyyy') = '" & SaleDate.Value & "'