Brackets in Access Query Using ConcatRelated Function - sql

I'm attempting to concatenate multiple records of field "dbo_race.racedesc" into one record based on username. I'm attempting to use the ConcatRelated function (http://allenbrowne.com/func-concat.html). As the username is text, I followed the article's example to include extra quotes.
The error I get is "Error 3126: Invalid bracketing of name '[dbo_indrace.username]'."
The error seems to be referring to the period within the brackets. However, if I only list the field without the table name, I am prompted that it could refer to more than one table listed in my FROM clause.
What would be the correct syntax for the ConcatRelated function?
SELECT DISTINCT dbo_ind.username,
ConcatRelated("[racedesc]", "[dbo_race]", "[dbo_indrace.username] = """ & [dbo_ind.username] & """") AS racedescription
FROM dbo_race INNER JOIN (dbo_ind INNER JOIN dbo_indrace ON dbo_ind.username = dbo_indrace.username) ON dbo_race.race = dbo_indrace.race
WHERE dbo_race.lang='E';

Try changing [dbo_indrace.username] to [dbo_indrace].[username]
Same with [dbo_ind.username]

Related

Filter Access table with keywords stored in another table

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.

Allen Browne's ConcatRelated() Error 3061: Too few parameters

I am trying to create a list of products at a given warehouse.
Allen Browne's ConcatRelated() function seems to be the tried and true method to create lists using when a linked variable is the same, but I can't get it to work.
I have broken my information down into a single query... "qry_Products"
SELECT qry_AX_LineItems_LINES.Warehouse, tblREF_Chemical.[Sales Name]
FROM qry_AX_LineItems_LINES INNER JOIN tblREF_Chemical ON
qry_AX_LineItems_LINES.ItemId = tblREF_Chemical.[Item Number]
GROUP BY qry_AX_LineItems_LINES.Warehouse, tblREF_Chemical.[Sales Name];
It produces a table with the Sales Name and Warehouse(s).
What I need to see happen is a list of the Sales Names when their warehouse matches.
I have tried using the function in a textbox of my form...
=ConcatRelated("[Sales Name]","[qry_Products]"," Warehouse ='" & [Warehouse]
& "'")
It causes an Error 3061 and leaves the cell blank.
I double checked my syntax within the quotes by using Dlookup(), and it produced the first result of the list.
I have also tried altering my query...
SELECT qry_AX_LineItems_LINES.Warehouse, ConcatRelated("[Sales Name]","
[tblREF_Chemical]") AS Expr1
FROM qry_AX_LineItems_LINES INNER JOIN tblREF_Chemical ON
qry_AX_LineItems_LINES.ItemId = tblREF_Chemical.[Item Number];
Unfortunately it then lists every product in my database as a list.
I also tried creating a new query to reference the one producing minimal information.
SELECT ConcatRelated("[Sales Name]","qry_Products") AS Expr1
FROM qry_Products;
I know that the initial query is correct but when I go to run the new query I get multiple pop-ups of Error 3061 and empty cells for results.
I double checked that I am copying the module exactly. http://allenbrowne.com/func-concat.html
Module is named "Concat".
I'm reading every help guide out there but I just can't see what I should try next.
Thank you so much for time and any advice!
SubForm frm_LineItems
Query qry_Products
I found another thread that gives an alternative to the Allen Browne method.
https://bytes.com/topic/access/answers/569535-combining-rows-opposite-union
This seems to be working.
'Concat Returns lists of items which are within a grouped field
Public Function Concat(strGroup As String, strItem As String) As String
Static strLastGroup As String
Static strItems As String
If strGroup = strLastGroup Then
strItems = strItems & ", " & strItem
Else
strLastGroup = strGroup
strItems = strItem
End If
Concat = strItems
End Function
with Query SQL
SELECT WH,
Max(Concat(WH, [Sales Name])) AS Products
FROM [qry_Products]
GROUP BY WH
I wanted to leave this here in case anyone else was having a similar issue.

MS Access - Ambiguous outer joins?

I am not sure why my query is netting me the following error:
The SQL statement could not be executed because it contains ambiguous outer joins. To force one of the joins to be performed first, create a separate query that performs the firm join and then include that query in your SQL statement.
This is my query, I only see one join:
SELECT PC.[Mother_Board_Name] & ',' & PC.[Mother_Board_Manufacturer]
FROM PersonalComputerHardware PC, Registers R
WHERE ',' & R.[Names].Value & ',' LIKE '*,' & PC.[Computer_ID] & ',*';
R.Names.Value is a reference to a multi-value field, by the way. I don't understand this error message because I am only using one Cross JOIN
Try this:
SELECT PC.Mother_Board_Name & ',' & PC.Mother_Board_Manufacturer
FROM PersonalComputerHardware PC
INNER JOIN Registers R ON R.Names LIKE '*,' & PC.Computer_ID & ',*'
Assuming your actual logic is correct, this should work.
However your logic is probably not correct. At best it will only match Computer_ID that is in the middle (not first or last) unless your Names has commas at the beginning and end as well.

Why doesn't my query use my criteria?

I have a db in Access and I'm trying to get a textbox to run my query and pass an other bounded textbox's value in as the criteria in DLookUp. I have the query running in design view and when I enter the criteria directly it returns the correct results. When I open the report it gives me the sum of all the possible rows. In other words it doesn't filter the rows.
I haven't used Access in about twelve years, thankfully, and everything I've done up to this point has been tutorial/example patchwork, but here it is...
SQL Query:
SELECT Sum(IIf(Attended=-1,1,0)) AS attendance
FROM Students_Classes_Attendance
WHERE (((CStr([Students_Classes_Attendance].[Class_Id]))=[classId]));
DLookUp as Control Source:
=DLookUp("[Total Attendance by Class]![attendance]",
"[Total Attendance by Class]",
"[Class_Id] =" & [Class_Id])
I'm lost at the moment. I'm guessing that the value isn't there before the query fires and since the criteria is an optional parameter that it's being passed null, but I would hope you'd get an error from that. Not that #Error is very meaningful anyway.
Does anyone know for certain the problem and the best way to correct it? Thanks.
Edit:
I did the changes recommended in the answer so now my DLookUp looks like...
=DLookUp("[attendance]",
"[Total Attendance by Class]",
"[Class_Id] =" & [Class_Id])
...still returns the total for all rows. Removing the criteria completely makes no difference either, which returns me to thinking it has something to do with the bound textbox not having a value.
DLookup uses the following syntax:
Syntax for numerical values:
DLookup("FieldName" , "TableName" , "Criteria = n")
Syntax for strings: (note the single apostrophe before and after the string value)
DLookup("FieldName" , "TableName" , "Criteria= 'string'")
Syntax for dates: (note the # before and after the date value)
DLookup("FieldName" , "TableName" , "Criteria= #date#")
I believe you just need to remove the table name from the first parameter. Try this:
=DLookUp("[attendance]", "[Total Attendance by Class]", "[Class_Id] = " & [Class_Id])
Keep in mind that if Class_Id is a Text Field, you need to surround it by single quotes:
=DLookUp("[attendance]", "[Total Attendance by Class]", "[Class_Id] = '" & [Class_Id] & "'")

How do I save the result of an SQL COUNT query with VBA in Access 2007?

I'm trying to count the number of records in a table that meet a certain criteria. My preference is to use SQL, not Dcount, as I want to get better at SQL. Here's my current code below:
Dim countString As String
Dim count
countString = "SELECT COUNT(*) FROM `Engagement Letters` WHERE 'Client ID' = " & Me.cboSelectClient
count = CurrentDb.OpenRecordset(countString).Fields(0).Value
Yeah I know, I've used spaces in my tables and field names - I will change that. Though I think I should still be able to run this query as is, so I will leave it as is for now.
When I run the above, I get runtime error 3464 - data type mismatch in criteria expression. I've had the below dcount function work fine:
count = DCount("[Engagement Letter ID]", "Engagement Letters", "[Client ID] = " & Me.cboSelectClient)
And also the below COUNT query without the WHERE works fine:
"SELECT COUNT(*) FROM `Engagement Letters`"
My knowledge of SQL is very minimal, and my knowledge of more advanced VBA is also quite minimal, so I'm not sure where I'm going wrong. Can anyone help me with this?
Try building your string like this.
countString = "SELECT COUNT(*) FROM [Engagement Letters]" & vbCrLf & _
"WHERE [Client ID] = " & Me.cboSelectClient
Debug.Print countString
Use square brackets around object (table and field) names which include spaces or any characters other than letters, digits, and the underscore character.
For the table name, you used `Engagement Letters`, and the backticks appear to work the same as square brackets. Perhaps they always work equally well, but I don't know for sure because I use the brackets exclusively. And brackets instead of backticks might help you avoid this mistake ...
WHERE 'Client ID' = " & Me.cboSelectClient
... that was asking the db engine to compare the literal string, "Client ID", to the numerical value (?) you pulled from cboSelectClient.
I used vbCrLf between the 2 parts of the SELECT statement because I find that convenient when examining the completed string (via Debug.Print).