ASP and Acess DB - "Like" Query problem - ms-access

//ACCESS
SELECT DISTINCT products.imageUrl FROM products WHERE ((products.pcprod_ParentPrd=5573) AND (products.pcprod_Relationship LIKE '*441*'));
//ASP
SELECT DISTINCT products.imageUrl FROM products WHERE ((products.pcprod_ParentPrd="&pidProduct&") AND (products.pcprod_Relationship LIKE '*"&rsCS("idoptoptgrp")&"*'));
this query works when i'm manually running the query in Access database. but when I run it from ASP. it doesn't return and rows

#user670111: In your ASP you have to use % instead of * as the wild-card character.
So rewrite your query in ASP as
SELECT DISTINCT products.imageUrl FROM products WHERE (products.pcprod_ParentPrd = " & pidProduct & ") AND (products.pcprod_Relationship LIKE '%" & rsCS("idoptoptgrp") & "%')

Maybe the values of pidProduct & rsCS("idoptoptgrp") are not what you are expecting it to be?
Does it work when you try ...
((products.pcprod_ParentPrd=" & 5573 & ") AND (products.pcprod_Relationship LIKE '*" & 441 & "*'));

Related

Dlookup with multiple criteria return the same result

When I run the following query,
Dlookup("[Service Description]","[Roster_QC]","[ID]=" & [ID]-1 AND "'[Person]= '" & [Person]&"'")
It showed the same result every time.
What I try to achieve is to let the Dlookup return for the previous service description when the person is the same.
Appreciate your insights or solution to this. Thanks.
Have an extraneous apostrophe and incorrect concatenation.
Dlookup("[Service Description]", "[Roster_QC]", "[ID]=" & [ID]-1 & " AND [Person]= '" & [Person] & "'")
That approach assumes there are no gaps in ID sequence. If there are gaps, expect incorrect results. Alternative query where gaps are irrelevant:
SELECT Roster_QC.*, (SELECT TOP 1 Dupe.[Service Description] FROM Roster_QC AS Dupe
WHERE Dupe.Person = Roster_QC.Person AND Dupe.ID<Roster_QC.ID
ORDER BY Dupe.ID DESC) AS Prev
FROM Roster_QC;

How can I use nz on a table field in a filter

I am trying to use nz on a table field in a filter on a form, however the filter is returning 0 results.
I originally tried doing this;
DoCmd.ApplyFilter "", "UCase([DELV_ADDR]) like '%" & UCase(Nz(Me.delv_txt,"""")) & "%'"
but this only returns entries that don't have blank addresses. I also tried
DoCmd.ApplyFilter "", "UCase([DELV_ADDR]) like '%" & UCase(Me.delv_txt) & "%' and isNull(Me.delv_txt)"
but predictably it returned addresses that matched my criteria and all null addresses
This code is what I've ended up with but it's still not working.
DoCmd.ApplyFilter "", "UCase(Nz([DELV_ADDR], """")) like '%" & UCase(Nz(Me.delv_txt,"""")) & "%'"
This filter doesn't return any results at all.
Any help would be greatly appreciated!
MS Access uses the * character for wildcards with the LIKE operator.
Change the % wildcards to * and it will return the results as expected:
DoCmd.ApplyFilter "", "UCase(Nz([DELV_ADDR], """")) like '*" & UCase(Nz(Me.delv_txt,"""")) & "*'"

Query returns results in QBE, but not via VBA code

Working in Access 2010 against an Access DB, I created a query in the QBE. As part of an overly complex quarterly reporting process, I now need to be able to execute that query with different parameters via VBA code. The SQL for the query is now embedded in my VBA module, and is modified on the fly, then executed. When run in the QBE, this particular instance of the query returns 400+ rows of data, but none are returned when executed in VBA via this code:
Dim Data As New ADODB.Recordset
Dim SQLString As String
SQLString = "SELECT PatientStatSchedDataDump.PtCompID, AppType.ProviderW, " & _
"PatientStatSchedDataDump.Date, PatientStatSchedDataDump.Status " & _
"FROM (AppType INNER JOIN PatientStatSchedDataDump ON AppType.AType = " & _
"PatientStatSchedDataDump.Type) LEFT JOIN GroupType ON AppType.Group = " & _
"GroupType.Group " & _
"WHERE (((PatientStatSchedDataDump.PtCompID) Like 'ClientName*' ) " & _
"AND ((PatientStatSchedDataDump.Date) BETWEEN #1/1/2014# AND #3/31/2014#) " & _
"AND ((GroupType.[Sort Order]) IN ('A', 'B', 'C', 'D', 'E')))"
Data.Open Source:=SQLString, ActiveConnection:=CurrentProject.Connection
If Not Data.EOF And Not Data.BOF Then
'the IF is never true - EOF & BOF are always True
NewSheet.Cells(InstCountRow + InstCount + 2, 1).CopyFromRecordset Data
End If
Data.Close
Set Data = Nothing
Again, if I create a new query in Access, paste the SQL code into the SQL window and run it, I get 400+ rows of results with this exact query
A query run from ADO requires ANSI wild cards: % instead of *; and _ instead of ?.
So change this ...
"WHERE (((PatientStatSchedDataDump.PtCompID) Like 'ClientName*' ) "
to this ...
"WHERE (((PatientStatSchedDataDump.PtCompID) Like 'ClientName%' ) "
If you want one query which works the same when run from ADO as it does when run in the QBE, you can use ALike instead of Like. With ALike, the db engine always expects ANSI wildcards.
"WHERE (((PatientStatSchedDataDump.PtCompID) ALike 'ClientName%' ) "

Run-time error 3075

I got this error message "Syntax error in query expression 'Select sum([BatteryDataTable].NumberOfBatter as)'"
Original sql statement below:
sqlDateRangeSearch = "Select *, **Select sum([BatteryDataTable].NumberOfBattery as " & Me.SumOfBattery & ")** from BattryDataTable" & _
" where BatteryDateChanged BETWEEN " & _
Format(Me.FromDTPicker.Value, "\#yyyy-m-d\#") & " and " & _
Format(Me.ToDTPicker.Value, "\#yyyy-m-d\#") & ";"
Me.RecordSource = sqlDateRangeSearch
What I am trying to do is to display all selected records, sum of values of all selected records, between selected dates. A selected records will display in a form, and the sum of values of all selected records, is displayed in textbox in the same form. BTW, somebody on here has helped me with this code. I was deeply appreciated. The part of the syntax in bold has caused the error. Thank you very much in advance.
When you create a write an SQL, you are not required to repeat statements such as SELECT, WHERE etc. You simply separate your selected staements with a comma as you have done already. Removing the second instance that you have mentioned SELECT will hopefully remove the error for you.
Many issues with your SQL.
as Dane wrote, you don't need to repeat a SELECT in this SQL
statement
the alias name you give for your sum column have to be included in
your SQL (within the double quotes) & should be outside of the closing bracket of the SUM
the ** you use before & after the SUM are irrelevant.
So here is your modified SQL Statement.
sqlDateRangeSearch = "Select *, sum(NumberOfBattery) as SumOfBattery from BattryDataTable" & _
" WHERE BatteryDateChanged BETWEEN " & _
Format(Me.FromDTPicker.Value, "\#yyyy-m-d\#") & " AND " & _
Format(Me.ToDTPicker.Value, "\#yyyy-m-d\#") & ";"
Me.RecordSource = sqlDateRangeSearch
Hope you find this helpful.

SQL UPDATE based on condition

We need to update a table with the users id (NBK). The table with NBK also has the user status (0 - 1) and only one user will have 1 at a time. The challenage is to #1 capture the active user #2 update the other table with the user NBK. I hope the code below just has a simple syntex error that I cannot find?
Dim nb As String
Dim NBK As String
nb = [Employees]![NBK] & "' WHERE "
nb = nb & " " & [Employees]![Status] = '1'
NBK = " Update tbl_DateTracking SET NBK = "
NBK = NBK & "'" & nb & "' WHERE "
NBK = NBK & "CaseId = '" & CaseId & "' AND OCC_Scenario = '" & OCC_Scenario & "' ;"
DoCmd.RunSQL nb
DoCmd.RunSQL NBK
Several pointers of note here:
You should use parameterized queries instead of string concatenation. This prevents/contains SQL injections and other issues regarding malformed input.
Ideally, you should normalize the database so that "active user" is not a field on the user table. What happens when there are two users set as "active" in the database?
With this given schema however, you're going to want to use a sub-select query. Ex:
UPDATE tbl_DateTracking SET NBK=(SELECT NBK FROM Employees WHERE Status=1 LIMIT 1) WHERE CaseID=? AND OOC_Scenario=? and then pass in CaseId and OOC_Scenario as the parameters.
Note, I'm not familiar with VB or how it interacts with SQL; The above is just an example, you'll have to apply it to your application and alter it to make it work. The way you're building and running the nested queries also looks like it won't work, since your first query doesn't contain a command (You probably want SELECT, I think. Does VB do that automatically with []![] syntax?), and when you nest it inside the second query, it's not surrounded with (). Running the first query by itself also has no effect if you're including it as a sub-query in the second one.