Filter Access table with keywords stored in another table - vba

I have 3 Access tables: tblClients, tblSalesRecords, tblKeywords. They are all linked by the ClientID key. I have some of my clients who need the sales records for SPECIFIC brands only that appear in the sales description records. Hence I created the tblKeywords which hold keywords of each client (from couple of keywords to tens).
I need to VB code that I can attach to Form/Report/Query that can fetch these records based on the keywords of each client. I was able to do it for ONE keyword only, but could not figure out how to create a full string holding all keywords.
Below is the SQL code linked to the form used to prepare the reports:
SELECT tblSalesRecords.SalesID, tblSalesRecords.ClientID, tblSalesRecords.ItemDescription, tblSalesRecords.Qty
FROM tblSalesRecords
WHERE (((tblSalesRecords.ItemDescription) Like "*" & [Forms].[KeywordsSubform].[Keyword] & "*"));
Keword table sample as below:
Client ID|Keyword
101|Samsung
101|Apple
101|Toshiba
102|Car
102|Motorbyc
102|Bus
Thank you.

One approach is a having a field with the EXACT strings to match:
SELECT * FROM tblSalesRecords
WHERE somefield IN (SELECT keyword FROM tblKeywords)
AND ClientID = Forms!formname!ClientID
Another uses DLookup().
SELECT * FROM tblSalesRecords
WHERE NOT DLookup("ClientID", "tblKeywords", "'" & [fieldname] & "' LIKE '*' & [Keyword] & '*'") IS NULL
AND ClientID = Forms!formname!ClientID;
Otherwise, most likely need a VBA custom function.

Related

ACCESS update with subquery

I have a table like this:
Now I'm trying to write in the column "SUMAMOUNT" of the table the sum of amount per "CODE" and "IBAN" but i can't reach this.
I'd want something like this:
I'm using this query but it doesn't work:
update tabella
set sumamount = (select sum(t2.amount)
from tabella as t2
where t2.code = tabella.code and t2.iban = tabella.iban
);
The precedent query gives me this result:
Can you help me? I'm using MS ACCESS.
Thank you in advance!
EDIT: Screenshot of the error:
I can't even try to run it because he ask me to save it. When I try to save, access gives me this error.
Consider domain aggregate, DSum, which allows an updateable query. Below assumes code and iban are text types and therefore requires single quote enclosures.
UPDATE tabella t
SET t.sumamount = DSUM("amount",
"tabella",
"code = '" & t.code & "' AND iban = '" & t.iban & '");
(By the way, best practice in databases is to avoid saving calculations in tables. Save resources and simply run queries on data as needed.)

Use a parameter in SSRS for table_name

I have scoured the internet for options and the only one I have found that can do it is by using a $Proc however I am trying to avoid that.
I would think it would be pretty simple to use a parameter to select a different table depending on what the user chooses from a drop down.
Here it is:
- There are two tables the report needs to use,
* some_table_CY (current year table)
* some_table_STLY (same time last year table)
So I created a parameter that gives the user the option to select "Current_Year" or "Last_Year", depending on which one the user chooses the parameter would then be used in the select statement, something like this: "SELECT * FROM :pReportVersion"
However, it is not working. I need it to do this, not using a union since unioning these two tables causes HUGE performance issues and the query takes more than 4 hours to run which is not acceptable for a report that users need on request.
(This is querying oracle)
Use the Dataset expression and set it to:
="SELECT * FROM " & Parameters!ReportVersion.Value
For longer queries you may need to wrap each line with quotes, append with an ampersand and add a line feed:
="SELECT * " & VBCLRLF &
"FROM " & Parameters!ReportVersion.Value & VBCRLF &
"WHERE FIELD1 > 10 " & VBCRLF &
"AND FIELD2 = 'YES' "
you can still use the union..
Say you have a parameter called #year
set the available values to the following (specify values)
current year for label and 1 for value
last year for label and 2 for value
Then your dataset can be something like this:
select * from some_table_CY
where #year = 1
union all
select * from some_table_LY
where #year = 2

Use an Access Forms Unbound text box as a Field filter in a table

Access 2013 - Reference an Unbound text box on a Form
I am currently trying to use an unbound text box [Text161] on a Form name [DCM_Gap_Servers] to sort information through a table. I want the query that I created to be able to take the users input from [DCM_Gap_Servers]![Text161] as the field that is being sorted from the table names 'Server'.
This is the SQL I am using right now in the query:
SELECT * FROM Servers WHERE "Forms![DCM_Gap_Servers]![Text161]" IS NULL
** I have already Tried:
"Forms![DCM_Gap_Servers]![Text161]" ; (Forms![DCM_Gap_Servers]![Text161]); Forms.[DCM_Gap_Servers]![Text161]
This will work at any time if I replace the Text Box reference with the actual Field name I am using, but since there are hundreds of combinations of fields, I need the reference to work.
I have looked all over, and I can't seem to find the correct answer. I am willing to do it in VBA if needed, whatever it takes to get the filtering done correctly.
Thank You.
It is:
SELECT * FROM Servers WHERE Forms.[DCM_Gap_Servers].[Text161] IS NULL
but that will just select all records whenever your textbox is Null.
So it rather is:
SELECT * FROM Servers WHERE SomeField = Forms.[DCM_Gap_Servers].[Text161]
To use the form value as a field name, you must use concatenated SQL:
strSQL = "SELECT * FROM Servers WHERE " & Forms![DCM_Gap_Servers]![Text161].Value & " IS NULL"
This you might pass to the SQL property of an existing query object:
MyQueryDef.SQL = strSQL
Or:
Constant SQL As String = "SELECT * FROM Servers WHERE {0} IS NULL"
FieldName = Forms![DCM_Gap_Servers]![Text161].Value
MyQueryDef.SQL = Replace(strSQL, "{0}", FieldName)
Of course, take care the the field name isn't a zero length string.

Creating a query linked to a value in a list box

The below retreive query works fine until i insert the WHERE clause which is linked to a list box taking the text value in the field 'Group' from the table 'tblTeams'.
Both the values from the table 'tblTeams' as well as that from the list box (sourcing another table) are values.
Looking at the code, the list box value is correct but for some reason does not link to the table. I am wondering if there is something wrong with the syntax?
SELECT [tblTeams].[Department], [tblTeams].[Team], [tblTeams].[Group] FROM [tblTeams]
ORDER BY [tblTeams].[Department]
WHERE [tblTeams].[group]= '" & lstGroup.Value & "'"
Where clause needs to be before order by clause. See documentation
SELECT [tblTeams].[Department], [tblTeams].[Team], [tblTeams].[Group] FROM [tblTeams]
WHERE [tblTeams].[group]= '" & lstGroup.Value & "'"
ORDER BY [tblTeams].[Department]

How do I run an SQL update query using a like statement

I am trying to update a field in a table using an SQL update query where there is a like statement referencing a value in another table. They syntax unfortunately is not working. Below is my code. In short, I am trying to put a '1' in the field 'Query07ParolaChiave' in the table 'tblSearchEngine01' when the value located in table 'tblsearchengine07' is present in the field 'tblMasterListOfEventsNotes' located in the table 'tblSearchEngine01'. I think my code is almost complete but there is a syntax issue which i cant find.
st_sql = "UPDATE tblSearchEngine01, tblSearchEngine07 SET tblSearchEngine01.Query07ParolaChiaveSelect = '1' WHERE ((([tblSearchEngine01].[tblMasterListOfEventsNotes]) Like " * " & [tblsearchengine07].[ParolaChiave] & " * "))"
Application.DoCmd.RunSQL (st_sql)
I suggest you 2 solutions :
This one is using EXISTS functions, and will check for each row in tblSearchEngine01 if there is a matching value in tblsearchengine07
UPDATE
tblSearchEngine01
SET
tblSearchEngine01.Query07ParolaChiaveSelect = '1'
WHERE
EXISTS (SELECT 1
FROM tblsearchengine07
WHERE [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*')
This one is more performant because it uses JOIN
UPDATE
tblSearchEngine01
INNER JOIN tblsearchengine07
ON [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*'
SET
tblSearchEngine01.Query07ParolaChiaveSelect = '1'
I read something like in ADO/VBA, you have to use % instead of * as the wildcard.
You can have more information on wildcard and LIKE comparator here
UPDATE
Why the '1' after select in your first solution?
EXISTS (SELECT 1 ... is better for performance because it return only the number 1 instead of fields, anyway EXISTS just stop the excecution after 1 element found.
'Performant' means more consuming in regards to space and memory?
JOIN is more performant in term of time of execution, RDBMS are far better at joining tables than using subquery, in some rare case, it's more interesting to use the 1st solution.
Also, any initial thoughts as to why my original solution (coming straight from an Access Query which works) does not function?
I cannot really know but perhaps it's because of " * ", because you are saying SPACE + * + SPACE + VALUE + SPACE + * + SPACE. For ex : 'John' LIKE ' John '
May be with "*" instead of " * " could solve it...
I have no other track, I'm not Access sql developper, I usually play around Sql server/Oracle/mySql, hope it helped. ;)
Try to change your like this way:
... Like '*" & tblsearchengine07.parolachiave & "*'))"
The like statement go into the WHERE clause.
If you do want to use LIKE without you care about caps letters, then you can use it like this:
LIKE COLUMN_NAME = '%WhatYouLike%'
My suggestion is:
Use a table variable (#Table) with a unique/primary key coming from the table to be updated.
SELECT all the data to be updated (you can add the like statement here) and then INSERT that in the created table variable.
Construct the UPDATE statement with an INNER JOIN to the table variable matching with the unique/primary key.
I know this may take a lot of steps but believe me these are more efficient than using a black list approach.