Can any body help me with the following SQL statement?
What am I doing wrong here?
It is giving me no result however there should be some...
The desired result should be a list or errorsdetails with the total number of times it is in the database (mdb) based on the selected date-period.
PS sorry for my badly written English..
Many thanks
Koen
sSQL = "SELECT ErrorDetail, count(*) AS totalcount FROM tblErrors WHERE DisplayName = " & cbox_modules.Text & " AND ErrorDate BETWEEN #" & startdate & "# AND #" & enddate & "# GROUP BY ErrorDetail"
Assuming the "DisplayName" column is a string, ensure you surround it with apostrophes:
SELECT
ErrorDetail,
COUNT(*) AS totalcount
FROM tblErrors
WHERE DisplayName = '" & cbox_modules.Text & "' AND ErrorDate BETWEEN #" & startdate & "# AND #" & enddate & "#
GROUP BY ErrorDetail
Related
I'm making ledger where I want to fetch data from date filter but it returns everything here is my query
str = "Select * from [ledger] where [client_name]='" & client_name.Text & "' OR [company_name]='" & company_name.Text.ToString & "' OR [registration_no]='" & reg_no.Text.ToString & "' And [date] Between '" & from_date.Value.ToShortDateString & "' And '" & To_Date.Value.ToShortDateString & "'"
In Access there is already date has datetime data type even I tried using access query builder it also returns all data instead of that particular date range.
Edit: after doing some troubleshoot query working perfectly like this
str = "SELECT * FROM [ledger] WHERE [date] Between # 10/03/2022 # And # 10/05/2022 # AND [client_name]='" & client_name.Text & "' AND [company_name]='" & company_name.Text.ToString & "' AND [registration_no]='" & reg_no.Text.ToString & "'"
but when I integrate with my date time picker to get that particular format, I get an error
System.InvalidCastException: 'Conversion from string "MM/dd/yyyy" to type 'Integer' is not valid
Any solution for this?
After some troubleshoot this is the final query which is woring properly
str = "SELECT * FROM [ledger] WHERE [date] Between #" & from_date.Value.ToString("MM/dd/yyyy") & "# And #" & To_Date.Value.ToString("MM/dd/yyyy") & "# AND [client_name]='" & client_name.Text & "' AND [company_name]='" & company_name.Text.ToString & "' AND [registration_no]='" & reg_no.Text.ToString & "'"
Thanks everyone for commenting.
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") & "#")
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 ;", _
Is there anyway I can include the Max or Top1 function into the below.
I'm trying to return the latest date value however due to the database returning date & time format I need to use the FORMAT syntax to correct this.
I've tried in both the SELECT and WHERE clauses but can't get it to work
sSql = "SELECT FORMAT(Total.StockDate, 'dd/mm/yy' ) AS DateRev FROM " & Mailbox & Totals & " WHERE(Total.UnitCode=" & Sheets("Pipe Cleaning").Range("C4") & " )"
Well you can do it as a subquery:
sSql = "SELECT FORMAT(StockDate, 'dd/mm/yy' ) AS DateRev FROM " & _
" (SELECT MAX(StockDate) StockDate FROM " & Mailbox & Totals & _
" WHERE(Total.UnitCode=" & Sheets("Pipe Cleaning").Range("C4") & " ) )"
But it may be cleaner to just get the date value and then format in VBA:
sSql = " (SELECT MAX(StockDate) StockDate FROM " & Mailbox & Totals & _
" WHERE(Total.UnitCode=" & Sheets("Pipe Cleaning").Range("C4") & " ) "
.. execute SQL, store result in dtDate
sDate = Format(dtDate, "dd/mm/yy")
Just wrap the format in a MAX statement. You have to specify the months as caps MM though.
Sample:
Select Max(Format(mydatefield, 'dd/MM/yy')) from mytable
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 & "'