I've got a SQL query in my VB6 project that is performing a three-way INNER JOIN in an Ms-Access database.
The VB6 query is:
SQL = "SELECT popsLines.stockCode, popsLines.orderNumber, popsOrders.dateOrdered, popsReceipts.dateReceived, popsReceipts.reference" & _
" FROM (popsOrders INNER JOIN popsLines ON popsOrders.orderNumber = popsLines.orderNumber)" & _
" INNER JOIN popsReceipts ON popsOrders.orderNumber = popsReceipts.orderNumber" & _
" WHERE (([WHERE popsLines].[stockCode]=" & sqlString(m_sStockCode) & "));"
This wasn't working, it returned an error saying
No value given for one or more required parameters
So then next thing I did was copy the value in the SQL variable and paste it into an Access query, with the value of the m_sStockCode parameter.
SELECT popsLines.stockCode, popsLines.orderNumber, popsOrders.dateOrdered, popsReceipts.dateReceived, popsReceipts.reference
FROM (popsOrders INNER JOIN popsLines ON popsOrders.orderNumber = popsLines.orderNumber)
INNER JOIN popsReceipts ON popsOrders.orderNumber = popsReceipts.orderNumber WHERE (([WHERE popsLines].[stockCode]="010010003"));
When executing this, it said
Enter Parameter Value: WHERE popsLines.StockCode
Why isn't it accepting the query as it is?
I've also tried changing there WHEREclause to
(( WHERE [popsLines].[stockCode]="010010003"));
but got
Syntax error (missing operator) in query expression '((WHERE [popsLines].[stockCode]="010010003"))'
The last part - your WHERE clause - is garbled. It should read:
.. WHERE ([popsLines].[stockCode]='010010003');
Related
I have two tables (lets call them Parameters 1 and 2) which both have a many-many relationship with a third table (Options). I need to group the third table records into three groups:
Those exclusively related to [specific Parameter 1 record],
Those exclusively related to [specific Parameter 2 record] and
Those related to both [specific Parameter 1 record] and [specific Parameter
2 record].
I can ignore Option records not related to either of them.
I need to be able to specify which Parameter 1 and 2 records apply in a form (using combo boxes), and have VBA juggle the three lists in the background, updating them as the Option records they contain are "used" elsewhere in the form (with check boxes).
At the risk of asking a bad question I'll submit the code I have - even though it's not a code that fails, just the framework for one that isn't even finished enough to debug yet. I simply haven't got the tools to complete it, as I don't know what methods/properties of what things to use to do it, and can't seem to find the answers in my own research thus far. Comments directing me to other resources will be appreciated, even if you don't have an answer that you're sure is best practice.
Function SetOptions()
If IsNull(cmbParam1) Or IsNull(cmbParam2) Then
MsgBox "You must select both an Param1 and a Param2!", vbCritical, "Wait!"
Exit Function
End If
'Recordsets of allowed Options
Dim Param1Opt, Param2Opt, OverlapOpt
'create recordset of tblOption.Option(s) referenced in qryPr1Opt with Param1 from cmbParam1
Param1Opt = CurrentDb.OpenRecordset("SELECT tblPr1Opt.Option FROM tblPr1Opt " &_
"WHERE Param1 = '" & cmbParam1 & "';")
'create recordset of tblOption.Option(s) referenced in qryPr2Opt with Param2 from cmbParam2
Param2Opt = CurrentDb.OpenRecordset("SELECT tblPr2Opt.Option FROM tblPr2Opt " &_
"WHERE Param2 = '" & cmbParam2 & "';")
'create recordset of tblOption.Option(s) in qryOptOvrlp with Param2 and Param1 from form
OverlapOpt = CurrentDb.OpenRecordset("SELECT qryOptOvrlp.Option FROM qryOptOvrlp " &_
"WHERE Param1 = '" & cmbParam1 & "' AND Param2 = '" & cmbParam2 & "';")
OverlapNum = Param1Num + Param2Num
'Steps remaining:
'1. Get Param1Opt and Param2Opt to only include Options not in overlap
For Each oOpt In OverlapOpt
For Each aOpt In Param1Opt
If aOpt.Value = oOpt.Value Then
'filter this record out of Param1Opt
End If
Next aOpt
For Each gOpt In Param2Opt
If gOpt.Value = oOpt.Value Then
'filter this record out of Param2Opt
End If
Next gOpt
Next oOpt
'2. Get the data in Param1Opt, Param2Opt and OverlapOpt, as well as their
'corresponding Nums to be accessible/editable in other functions/subs
End Function
You can reference the values of controls in SQL statements run in the context of Access, using the following syntax:
Forms!FormName!ControlName
or using square brackets if needed:
Forms![Form Name]!ControlName
Therefore, instead of opening multiple recordsets, you can express each of your points as a single SQL statement with joined tables. You can then either set the RowSource of a combobox or listbox to the statement (if you are only using the statement in one place); or you can save the statement as an Access query, and use the query name as the RowSource (if you need the statement in multiple places).
Given the following schema:
and two comboboxes: cmbParam1 and cmbParam2 on a form named Form1, you can use SQL statements as follows:
1. Records from Options which match tblPr1Opt but have no match in tblPr2Opt
SELECT DISTINCTROW Options.Option
FROM (Options
INNER JOIN tblPr1Opt ON Options.Option = tblPr1Opt.Option)
LEFT JOIN tblPr2Opt ON Options.Option = tblPr2Opt.Option
WHERE tblPr2Opt.Option IS NULL
AND tblPr1Opt.Param1 = Forms!Form1!cmbParam1
Or using the query designer (note the arrow head next to tblPr2Opt; this indicates a left join):
2. Records from Options which match tblPr2Opt but have no match in tblPr1Opt:
SELECT DISTINCTROW Options.Option
FROM (Options
INNER JOIN tblPr2Opt ON Options.Option = tblPr2Opt.Option)
LEFT JOIN tblPr1Opt ON Options.Option = tblPr1Opt.Option
WHERE tblPr1Opt.Option IS NuLL
AND tblPr2Opt.Param2 = Forms!Form1!cmbParam2;
or in the query designer:
3. Records from Options which match on both:
SELECT Options.Option
FROM (Options
INNER JOIN tblPr1Opt ON Options.Option = tblPr1Opt.Option)
INNER JOIN tblPr2Opt ON Options.Option = tblPr2Opt.Option
WHERE tblPr1Opt.Param1 = Forms!Form1!cmbParam1
AND tblPr2Opt.Param2 = Forms!Form1!cmbParam2
Or in the query designer:
Trying to run this SQL query in MS Access but it keeps asking for parameter value. I am new to MS Access. Can you please help me with the query ?
UPDATE AC_E2 a
SET
a.EXPRESSION = (Select "No : " & NO & " CAT : " & CAT from AC_E1 f WHERE a.NUM = f.NUM)
WHERE
a.PROPNUM = (Select PROPNUM from AC_E2 e WHERE e.EXPRESSION LIKE "No: #M.NO CAT: #M.CAT" and e.NUM = a.NUM)
I am just trying to search a column in table AC_E2 which has this expression: No: #M.NO CAT: #M.CAT and am trying to replace the #M.NO and #M.CAT with their respective value from AC_E1 table.
In MS Access queries, the square brackets are a convention to prompt the user running the query to input a parameter. e.g. [EXPRESSION] will prompt the user to enter the value named, "EXPRESSION".
Got hint from this link and changed my query accordingly:
How do I perform update query with subquery in Access?
UPDATE AC_E2 a, AC_E1 b
SET a.EXPRESSION = "No : " & b.NO & " CAT : " & b.CAT
WHERE
a.NUM = e.NUM AND
a.EXPRESSION = 'No: #M.NO CAT: #M.CAT'
I'm trying to create a query that can run from a button in MS Access. The SQL query I've created is below:
PARAMETERS CASNUMBER Text ( 255 );
SELECT DISTINCT Chemical.Chemical_Name, Hazard.Hazard_Code, Hazard.Hazard_Text
FROM Chemical, Chemical_Hazard, Hazard
WHERE Chemical.Chemical_Id = Chemical_Hazard.Chemical_Id
and Chemical_Hazard.Hazard_Id = Hazard.Hazard_Id
and [CASNUMBER] = Chemical.CAS
;
How do I make it so that a MsgBox appears when there are no values returned?
I'd recommend join tables using JOIN instead of WHERE. Also you can avoid using parameter, which in most cases requres VBA code for query execution. Just create query without parameter, add CAS to columns list:
SELECT DISTINCT Chemical.Chemical_Name
,Hazard.Hazard_Code
,Hazard.Hazard_Text
,Chemical.CAS
FROM (
Chemical_Hazard INNER JOIN Chemical ON Chemical.Chemical_Id = Chemical_Hazard.Chemical_Id
)
INNER JOIN Hazard ON Chemical_Hazard.Hazard_Id = Hazard.Hazard_Id
And then check for CAS in code:
If DCount("*", "MyQuery", "CAS=" & lngCASNumber) = 0 then
MsgBox "CAS not found"
End If
I spent some time looking around at other questions on the board, but didn't find anything similar so here it goes:
I have a report that runs up against the limits of CRM 2011 online in regards to the max number of characters in a Fetch Query. I would like to have a drop box that has an option for "All Contacts" or "Select up to Five Contacts" to avoid that issue.
What I would like to do is remove the condition from the Fetch statement if the "All Contacts" option is selected, and include the condition only if the select up to five is chosen.
How would I go about doing this?
Let me know if you have any further questions.
Nick
You can build your SQL using custom code because the SQL itself can be a string expression that you build manually. Let's say your SQL looks like this at the moment:
SELECT ThisField, ThatField
FROM Contacts
WHERE ContactId IN #Contacts
Let's change that to a string expression that builds the SQL:
="SELECT ThisField, ThatField "
& "FROM Contacts "
& "WHERE ContactId IN #Contacts "
Now we need to pull the #Contacts parameter apart and only select by ContactId when there are five or less contacts selected:
Function ContactsSQL(ByVal parameter As Parameter) AS String
Dim Result As String
Result = ""
If parameter.IsMultiValue Then
If parameter.Count <= 5 Then
Result = "WHERE ContactId IN ("
For i As integer = 0 To parameter.Count-1
Result = Result + CStr(parameter.Value(i)) + ", "
Next
Result = Left(Result, Result.Length - 2) + ") "
End If
End If
Return Result
End Function
This function builds the SQL WHERE clause to select the contacts only if five or less have been selected, otherwise it passes back an empty string. Now we use this function in our SQL expression:
="SELECT ThisField, ThatField "
& "FROM Contacts "
& Code.ContactsSQL(Parameters!Contacts)
Note that you are passing the Contacts parameter object here, not the Value property. We access the Value property in the custom code function.
I use MySql.Data.MySqlClient.MySqlCommand and MySqlConnection
Public fillGridCmdTxt As String = "SELECT tblItems.part_num AS Part#, tblCategory.category_description AS Category, " _
& " tblItems.item_name AS 'Item Name', tblItems.item_desc AS Description, " _
& "tblItems.item_qty AS Qty, tblUnit.unit_name AS Unit, tblItems.item_price AS 'Selling Price(Php)' " _
& "FROM tblUnit INNER JOIN tblItems ON tblUnit.unit_id = tblItems.unit_id INNER JOIN tblCategory " _
& "ON tblItems.category_id = tblCategory.category_id "
and when i use executeNonQuery
on MySqlCommand, it gives me an error...
It says that "Unkown table '*tblItems in field list*" even the table is really existing on my database... a little help please?
You need to enclose Part# between quotes.
The # sign starts a comment in MySQL, so your whole statement is read as just SELECT tblItems.part_num AS Part. The error message tells you that you are naming a table in the fields list that you didn't specify in the FROM list (because the FROM list got commented out).