How to select top 10 in Access query?

select top 10 Name, Price from MyTable order by Price desc Updated: @Fionnuala pointed out that: “Access SQL selects matches, so it will select all items with the same highest prices, even if this includes more than 10 records. The work-around is to order by price and a unique field (column).” So, if you have … Read more

insert query with sequential primary key

Here are some notes that may help you towards your goal, however life would be a lot easier and a lot safer with autonumbers. This is VBA as you mention MS Access. Function NextTranNumber(ByRef FirstTran As Long, _ ByRef LastTran As Long, Optional BlockSize = 1) Dim cn As New ADODB.Connection Dim rs As New … Read more

Pivot Query in MS Access

Consider: TRANSFORM First(Data.Cat) AS FirstOfCat SELECT Data.ID, Data.Name FROM Data GROUP BY Data.ID, Data.Name PIVOT “Cat” & DCount(“*”,”Data”,”ID=” & [ID] & ” AND Cat<‘” & [Cat] & “‘”)+1; Or if there is a unique record identifier field – autonumber should serve: TRANSFORM First(Data.Cat) AS FirstOfCat SELECT Data.ID, Data.Name FROM Data GROUP BY Data.ID, Data.Name PIVOT … Read more

Access 2007 – Left Join to a query returns #Error instead of Null

While the query should return Null based on the join type, as Allen Browne states in his article, Bug: Outer join expressions retrieved wrongly, “Instead, it behaves as if [the JET query optimizer] is evaluating the expression after it has returned the results from the lower-level query.” Consequently, you must select the calculated field using … Read more

MS Access LIMIT X, Y

While the Access/JET TOP keyword does not directly provide an OFFSET capability, we can use a clever combination of TOP, a subquery, and a “derived table” to obtain the same result. Here is an example for getting the 10 rows starting from offset 20 in a Person table in ORDER BY Name and Id… SELECT … Read more