I have a Access 2010 Form that is linked to Sql Server and I am using a select query that works correctly until I add an Inner Join and then the field becomes unable to update (Recordset is not Updateable) This is the query I am using but cant check the field Yes(this is a checkbox), if I remove the inner join it works. column nm has can have more than one name in it so to get the latest name I am using the inner join with the max to give me the latest name otherwise it will show both names in the nm column.
SELECT tblA.Reference
TblA.Yes,
TblA.Nm,
TblA.PKID
FROM TblA
INNER JOIN (
SELECT
Reference,
max(PKID) AS TRANID
FROM TblA
GROUP BY Reference) AS TblB ON TblA.Reference = TblB.Reference AND
TblA.PKID = TblB.TRANID)
WHERE TblA.Reference Like "*" & [forms]![NewPayments]![Combo1] & "*" AND
TblA.Nm Like "*" & [forms]![NewPayments]![ClientName] & "*" AND
TblA.Account Like "*" & [forms]![NewPayments]![ACN] & "*" AND
TblA.Query Is Null ;
If you want to use aggregates only to select specific entries, you can use EXISTS instead of INNER JOIN to keep the query updateable.
SELECT tblA.Reference
TblA.Yes,
TblA.Nm,
TblA.PKID
FROM TblA
WHERE EXISTS (
SELECT 1
FROM TblA As TblB
WHERE TblA.Reference = TblB.Reference
HAVING TblA.PKID = max(TblB.PKID)
) AND TblA.Reference Like "*" & [forms]![NewPayments]![Combo1] & "*" AND
TblA.Nm Like "*" & [forms]![NewPayments]![ClientName] & "*" AND
TblA.Account Like "*" & [forms]![NewPayments]![ACN] & "*" AND
TblA.Query Is Null ;
Related
So I'm using MS Access to have users access a mysql database. I don't use linked tables rather connect to the database using ODBC DAO connections.
I'm generating a report which at the bottom gives the balance of the account. So far in my query I have been able to get all the data I need from the database except for the balance. Reason is that all the data I pull from database is filtered by a where statement between two dates and a company id. For the balance however I need another set of results giving me the sum of a whole column.
Reason I need to put this on a single sql query is because I will then pass this query to the recordsource of the report thus I cant use two queries.
I tried to use UNION but it doesnt work since I'm using a continuous form to show all results it creates more rows to fit for the second select statement.
This works as intended.
SELECT tbl10prepago.NumeroFactura, tbl10prepago.Fecha, tbl12vehiculosprepago.Tablilla, tbl11empleadosprepago.NombreEmpleado, tbl10prepago.Litros, tbl10prepago.CostoLitro, tbl10prepago.CantidadDinero, tbl13companiasprepago.NombreCompania, tbl13companiasprepago.Concepto, tbl5localidades.NombreLocalidad, tbl5localidades.DireccionPostal, tbl5localidades.Telefono
FROM (tbl10prepago INNER JOIN tbl12vehiculosprepago ON tbl10prepago.IdVehiculo = tbl12vehiculosprepago.ID)
INNER JOIN tbl11empleadosprepago ON tbl10prepago.IdEmpleado = tbl11empleadosprepago.ID
INNER JOIN tbl13companiasprepago ON tbl10prepago.CompaniaID = tbl13companiasprepago.ID
INNER JOIN tbl5localidades ON tbl13companiasprepago.LocalidadServicio = tbl5localidades.ID
WHERE tbl10prepago.Fecha BETWEEN '" & Format(Me.Text40, "yyyy-MM-dd HH:mm:ss") & "' AND '" & Format(Me.Text42, "yyyy-MM-dd HH:mm:ss") & "'
AND tbl10prepago.CompaniaID = " & Me.Combo34.Column(0) & "
ORDER BY tbl10prepago.Fecha;
What I need to add is this:
SELECT SUM(tbl14pagoprepago.pago) as [BALANCE]
FROM tbl14pagoprepago
WHERE tbl14pagoprepago.IDCompania = " & Me.Combo34.Column(0) & "
And not add it to the first Select results or filter by date also.
Thank you all in advance!
union will only work if you have same number of columns. What I can think of is you can add this balance field as new column.
SELECT tbl10prepago.NumeroFactura, tbl10prepago.Fecha, tbl12vehiculosprepago.Tablilla, tbl11empleadosprepago.NombreEmpleado, tbl10prepago.Litros, tbl10prepago.CostoLitro, tbl10prepago.CantidadDinero, tbl13companiasprepago.NombreCompania, tbl13companiasprepago.Concepto, tbl5localidades.NombreLocalidad, tbl5localidades.DireccionPostal, tbl5localidades.Telefono,
(SELECT SUM(tbl14pagoprepago.pago) FROM tbl14pagoprepago
WHERE tbl14pagoprepago.IDCompania = " & Me.Combo34.Column(0) & ") as [Balance]
FROM (tbl10prepago INNER JOIN tbl12vehiculosprepago ON tbl10prepago.IdVehiculo = tbl12vehiculosprepago.ID)
INNER JOIN tbl11empleadosprepago ON tbl10prepago.IdEmpleado = tbl11empleadosprepago.ID
INNER JOIN tbl13companiasprepago ON tbl10prepago.CompaniaID = tbl13companiasprepago.ID
INNER JOIN tbl5localidades ON tbl13companiasprepago.LocalidadServicio = tbl5localidades.ID
WHERE tbl10prepago.Fecha BETWEEN '" & Format(Me.Text40, "yyyy-MM-dd HH:mm:ss") & "' AND '" & Format(Me.Text42, "yyyy-MM-dd HH:mm:ss") & "'
AND tbl10prepago.CompaniaID = " & Me.Combo34.Column(0) & "
ORDER BY tbl10prepago.Fecha;
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');
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:
My Access database has a query which, I suspect, is called by macros or other queries. Is there any way to run a Find on the "code" of all the macros and/or queries, to look for a text string (in this case, the query name)?
this lists all the tables & queries:
SELECT IIf([type] = 5, "Query", "Table") AS [Object type]
,MSysQueries.Flag AS [Query type]
,MSysObjects.NAME
,MSysObjects.Id
,MSysObjects.Type
FROM MSysObjects
LEFT JOIN MSysQueries ON MSysObjects.Id = MSysQueries.ObjectId
GROUP BY IIf([type] = 5, "Query", "Table")
,MSysQueries.Flag
,MSysObjects.NAME
,MSysObjects.Id
,MSysObjects.Type
HAVING (
(
(MSysObjects.NAME) NOT LIKE "~*"
AND (MSysObjects.NAME) NOT LIKE "Msys*"
)
AND (
(MSysObjects.Type) = 1
OR (MSysObjects.Type) = 4
OR (MSysObjects.Type) = 6
OR (MSysObjects.Type) = 5
)
)
ORDER BY IIf([type] = 5, "Query", "Table") DESC
,MSysQueries.Flag
,MSysObjects.NAME;
and this one lists each object and the queries that reference it:
SELECT [~MSys Tables & Queries].NAME AS [Object]
,MSysObjects.NAME AS [Used in query]
FROM [~MSys Tables & Queries]
LEFT JOIN (
MSysQueries LEFT JOIN MSysObjects ON MSysQueries.ObjectId = MSysObjects.Id
) ON [~MSys Tables & Queries].NAME = MSysQueries.Name1
GROUP BY [~MSys Tables & Queries].NAME
,MSysObjects.NAME
ORDER BY [~MSys Tables & Queries].NAME
,MSysObjects.NAME;
I don't know how to search through Macros the same way. Also, I'm not sure this always picks up objects used in Union queries.
I hope it gives you a place tro start.
You can install a free Access Add-in Access Dependency Checker, it can search strings thru all objects.
I am trying to insert mulivalues of swab locations into a table that has the same tracking number. Here is the example:
tblMainSwapLocation (Table where i get my information form)
Asset_ID MAterial Swap_Location
MFG111 Brick Top left
MFG111 Plastic Top right
MFG113 Wood Center
tblCVLocation (Table where i want to insert information into)
TrackingID Asset_ID Swap_Location
99801 MFG111 Top left
99801 MFG111 Top right
I am using the following sql query to do the job, but i am having troubles with how the query should be.
strSQL = "Insert Into tblCVProject " & vbCrLf
strSQL = strSQL & "Values ( [pTrackNum], (SELECT MEQ.Asset_ID ,MEQ.SwabLocation" & vbCrLf
strSQL = strSQL & "FROM tblMainSwapLocation as MEQ " & vbCrLf
strSQL = strSQL & "WHERE MEQ.Asset_ID = [pAsset_ID] ))"
Debug.Print strSQL
Set qdf = dbs.CreateQueryDef(vbNullString, strSQL)
qdf.Parameters("pAssetID").Value = Me.cboAsset_Id
qdf.Parameters("pTrackNum").Value = TrackNum
The query in simplier form, It runs when the user clicks the save button.
Insert Into tblCVProject
Values ( [pTrackNum], (SELECT MEQ.Asset_ID ,MEQ.SwabLocation, MEQ.Equipment_Name
FROM tblMainSwapLocation as MEQ
WHERE MEQ.Asset_ID = [pAsset_ID] ))
This is the error I am getting:
The syntax of the INSERT statement is well explained at:
https://msdn.microsoft.com/en-us/library/bb208861(v=office.12).aspx
Either use VALUES (for single-record append query) or SELECT for multi-record append query.
Not enough info to give a clear answer.
I have guessed that your TrackingID column is defined as auto-number because I see nowhere a source for it
If you want to generate a record in tblCVLocation for each tblMainSwapLocation then you just have to do this :
INSERT INTO tblCVProject ([Asset_ID],[Swap_Location])
SELECT Asset_ID, Swap_Location FROM tblMainSwapLocation
If you want to insert only for some Asset_ID or material, then add a WHERE clause after the SELECT
If you want only the distinct pairs of Asset_ID + Swap_location then add a DISTINCT clause in the SELECT
I really can be more accurate with the information specified