Saturday, October 24, 2015

Userform

10/24/2015

- Delete records and extract records using the forms created in MSAccess(User forms)???

11/6/2015
'Delete record is working. And similarly Insert query will also work. Looks like only Select query has a problem.

'sql1 = "delete from IssueTracker where SNo=129"
'CurrentDb.Execute sql1

- Table already exist, but will need a new column created based on two columns. How to create???

Tuesday, September 15, 2015

Count Function

Source: http://www.techonthenet.com/access/functions/numeric/count.php


MS ACCESS: COUNT FUNCTION

This MSAccess tutorial explains how to use the Access Count function with syntax and examples.

DESCRIPTION

The Microsoft Access Count function returns the number of records in a select query.

SYNTAX

The syntax for the Count function in MS Access is:
Count( expression )

Parameters or Arguments

expression
A field or any string expression.

APPLIES TO

The Count function can be used in the following versions of Microsoft Access:
  • Access 2013, Access 2010, Access 2007, Access 2003, Access XP, Access 2000

EXAMPLE IN SQL/QUERIES

You can use the Count function in a query by clicking on the Totals button in the toolbar (This is the button with the summation symbol). The Count function is used in conjunction with the Group By clause.
For example:
Microsoft Access
This query would return the number of records for each ProductName. You can actually substitute any field with the Count function and it will return the same count result. In this case, we've used the SupplierID field to determine the number of records for each ProductName.

Sunday, March 29, 2015

MSAccess Features

3/29/15..

- Its a DB, so should be able to create Tables, Queries

- Design view, will give clear idea on how query could be designed, if you add columns, criteria to each column that will automatically create query for you(Check SQL view for that).

Other than DB:

- Create user form, write macros. Assign Queries to the bottons in the form. You could customize the vbscrip methods. Go to the properties of the botton->action-> modify vbscript

- And using the form you could create any kind of user interface

- Other feature is, if you are professional in vbscripting, you could make any kind of functionality to work. That means MSAccess is VBscrip friendly.

- Well once you create different reports playing on queries and tables using vbscript or directly from the interface, you could export the reports to excel or other Microsoft  formats.

- Every excel reports(other compatable formats) could be converted to tables in MSaccess, that way you just import them whenever needed a table of that kind in your DB.

-
 

Friday, March 20, 2015

IN vs LIKE operator in Queries

Like:

SELECT AMEX_FEED_MANAGEMENT_03082015.[QC Project Name], AMEX_FEED_MANAGEMENT_03082015.[Release Name], AMEX_FEED_MANAGEMENT_03082015.[Start Date], AMEX_FEED_MANAGEMENT_03082015.[End Date], AMEX_FEED_REQUIREMENT_03082015.[Target Release] AS [Req mapped to Rlease], AMEX_FEED_REQUIREMENT_03082015.[Target Cycle] AS [Req mapped to Cycle], AMEX_FEED_REQUIREMENT_03082015.[Requirement Type], AMEX_FEED_REQUIREMENT_03082015.[Direct Cover Status], AMEX_FEED_REQUIREMENT_03082015.[RRA Analyze Score], AMEX_FEED_TESTPLAN_03082015.[Linked Defects] AS [Test Plan Linked Defects], AMEX_FEED_TESTPLAN_03082015.Attachment AS [Test Plan Attachments], AMEX_FEED_TESTPLAN_03082015.[Test Name] AS [Test Plan Test Name], AMEX_FEED_TESTLAB_03082015.[Target Release] AS [TC mapped to Release], AMEX_FEED_TESTLAB_03082015.[Assigned to Cycle] AS [TC mapped to Cycle], AMEX_FEED_TESTLAB_03082015.[Test Set Name]
FROM AMEX_FEED_TESTPLAN_03082015, AMEX_FEED_TESTLAB_03082015, AMEX_FEED_MANAGEMENT_03082015 INNER JOIN AMEX_FEED_REQUIREMENT_03082015 ON AMEX_FEED_MANAGEMENT_03082015.[QC Project Name]=AMEX_FEED_REQUIREMENT_03082015.[QC Project Name]
WHERE (((AMEX_FEED_REQUIREMENT_03082015.[QC Project Name]) Like ([Forms]![Check QC Project set up]![Text3] & "*")));

or

Select * from Projectstable where project_id like ("PRJ");  -- here it will extract all projects that has "PRJ" in there Project ids.



IN:

SELECT Projects_list.Name
FROM Projects_list
WHERE Projects_list.Name IN (Select Project_ids.Project_Name from Project_ids );
--- IN - operator helps you to look into the list  of values, here it will display list of project names that are available in Project_ids table.

Thursday, March 19, 2015

Sub queries in MSAccess

Microsoft Access Tips for Serious Users

Provided by Allen Browne, March 2007. Updated February 2009.

Subquery basics

Discovering subqueries is one of those "Eureka!" moments. A new landscape opens in front of you, and you can do really useful things such as:
  • Read a value from the previous or next record in a table.
  • Select just the TOP (or most recent) 5 scores per client.
  • Choose the people who have not paid/ordered/enrolled in a period.
  • Express a value as a percentage of the total.
  • Avoid inflated totals where a record is repeated (due to multiple related records.)
  • Filter or calculate values from other tables that are not even in the query.

What is a subquery?

The SELECT query statement


This example shows basic SQL syntax.
It returns 3 fields from 1 table, applies criteria, and sorts the results:
SELECT CompanyID, Company, City
FROM Table1
WHERE (City = "Springfield")
ORDER BY Company;
The clauses must be in the right order. Line endings and brackets are optional.
A subquery is a SELECT query statement inside another query.
As you drag fields and type expressions in query design, Access writes a sentence describing what you asked for. The statement is in SQL (see'quell) - Structured Query Language - the most common relational database language, also used by MySQL, SQL Server, Oracle, DB2, FoxPro, dBase, and others.
If SQL is a foreign language, you can mock up a query like the subquery you need, switch it to SQL View, copy, and paste into SQL View in your main query. There will be some tidying up to do, but that's the simplest way to create a subquery.

Subquery examples

The best way to grasp subqueries is to look at examples of how to use them.

Identifying what is NOT there

A sales rep. wants to hound customers who have not placed any orders in the last 90 days:
SELECT Customers.ID, Customers.Company
FROM Customers
WHERE NOT EXISTS
(SELECT Orders.OrderID
FROM Orders
WHERE Orders.CustomerID = Customers.CustomerID
AND Orders.OrderDate > Date() - 90) ;
The main query selects two fields (ID and Company) from the Customers table. It is limited by the WHERE clause, which contains the subquery.
The subquery (everything inside the brackets) selects Order ID from the Orders table, limited by two criteria: it has to be the same customer as the one being considered in the main query, and the Order Date has to be in the last 90 days.
When the main query runs, Access examines each record in the Customers table. To decide whether to include the customer, it runs the subquery. The subquery finds any orders for that customer in the period. If it finds any, the customer is excluded by the NOT EXISTS.

Points to note:

  • The subquery goes in brackets, without a semicolon of its own.
  • The Orders table is not even in the main query. Subqueries are ideal for querying about data in other tables.
  • The subquery does not have the Customers table in its FROM clause, yet it can refer to values in the main query.
  • Subqueries are useful for answering questions about what data exists or does not exist in a related table.

Get the value in another record

Periodically, they read the meter at your house, and send a bill for the number of units used since the previous reading. The previous reading is a different record in the same table. How can they query that?
A subquery can read another record in the same table, like this:
SELECT MeterReading.ID,
MeterReading.ReadDate,
MeterReading.MeterValue,
(SELECT TOP 1 Dupe.MeterValue
FROM MeterReading AS Dupe
WHERE Dupe.AddressID = MeterReading.AddressID
AND Dupe.ReadDate < MeterReading.ReadDate
ORDER BY Dupe.ReadDate DESC, Dupe.ID) AS PriorValue
FROM MeterReading;
The main query here contains 4 fields: the primary key, the reading date, the meter value at that date, and a fourth field that is the value returned from the subquery.
The subquery returns just one meter reading (TOP 1.) The WHERE clause limits it to the same address, and a previous date. The ORDER BY clause sorts by descending date, so the most recent record will be the first one.

Points to note:

  • Since there are two copies of the same table, you must alias one of them. The example uses Dupe for the duplicate table, but any name will do.
  • If the main query displays the result, the subquery must return a single value only. You get this error if it returns multiple values:
    At most one record can be returned by this subquery.
  • Even though we asked for TOP 1, Access will return multiple records if there is a tie, e.g. if there were two meter readings on the same date. Include the primary key in the ORDER BY clause to ensure it can decide which one to return if there are equal values.
  • The main query will be read-only (not editable.) That is always the case when the subquery shows a value in the main query (i.e. when the subquery is in the SELECT clause of the main query.)

TOP n records per group

You want the three most recent orders for each client. Use a subquery to select the 3 top orders per client, and use it to limit which orders are selected in the main query:
SELECT Orders.CustomerID, Orders.OrderDate, Orders.OrderID
FROM Orders
WHERE Orders.OrderID IN
(SELECT TOP 3 OrderID
FROM Orders AS Dupe
WHERE Dupe.CustomerID = Orders.CustomerID
ORDER BY Dupe.OrderDate DESC, Dupe.OrderID DESC)
ORDER BY Orders.CustomerID, Orders.OrderDate, Orders.OrderID;

Points to note:

  • Since we have two copies of the same table, we need the alias.
  • Like EXISTS in the first example above, there is no problem with the subquery returning multiple records. The main query does not have to show any value from the subquery.
  • Adding the primary key field to the ORDER BY clause differentiates between tied values.

Year to date

A Totals query easily gives you a total for the current month, but to get a year-to-date total or a total from the same month last year means another calculation from the same table but for a different period. A subquery is ideal for this purpose.
SELECT Year([Orders].[OrderDate]) AS TheYear,
Month([Orders].[OrderDate]) AS TheMonth,
Sum([Order Details].[Quantity]*[Order Details].[UnitPrice]) AS MonthAmount,
(SELECT Sum(OD.Quantity * OD.UnitPrice) AS YTD
FROM Orders AS A INNER JOIN [Order Details] AS OD ON A.OrderID = OD.OrderID
WHERE A.OrderDate >= DateSerial(Year([Orders].[OrderDate]),1,1)
AND A.OrderDate < DateSerial(Year([Orders].[OrderDate]), Month([Orders].[OrderDate]) + 1, 1)) AS YTDAmount
FROM Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID
GROUP BY Year([Orders].[OrderDate]), Month([Orders].[OrderDate]);

Points to note:

  • The subquery uses the same tables, so aliases them as A (for Orders) and OD (for Order Details.)
  • The date criteria are designed so you can easily modify them for financial years rather than calendar years.
  • Even with several thousand records in Order Details, the query runs instantaneously.

Delete unmatched records

The Unmatched Query Wizard (first dialog when you create a new query) can help you identify records in one table that have no records in another. But if you try to delete the unmatched records, Access may respond with, Could not delete from specified tables.
An alternative approach is to use a subquery to identify the records in the related table that have no match in the main table. This example deletes any records in tblInvoice that have no matching record in the tblInvoiceDetail table:
DELETE FROM tblInvoice
WHERE NOT EXISTS
(SELECT InvoiceID
FROM tblInvoiceDetail
WHERE tblInvoiceDetail.InvoiceID = tblInvoice.InvoiceID);

Delete duplicate records

This example uses a subquery to de-duplicate a table. "Duplicate" is defined as records that have the same values in Surname and FirstName. We keep the one that has the lowest primary key value (field ID.)
DELETE FROM Table1
WHERE ID <> (SELECT Min(ID) AS MinOfID FROM Table1 AS Dupe
WHERE (Dupe.Surname = Table1.Surname)
AND (Dupe.FirstName = Table1.FirstName));
Nulls don't match each other, so if you want to treat pairs of Nulls as duplicates, use this approach:
DELETE FROM Table1
WHERE ID <> (SELECT Min(ID) AS MinOfID FROM Table1 AS Dupe
WHERE ((Dupe.Surname = Table1.Surname)
OR (Dupe.Surname Is Null AND Table1.Surname Is Null))
AND ((Dupe.FirstName = Table1.FirstName)
OR (Dupe.FirstName Is Null AND Table1.FirstName Is Null)));

Aggregation: Counts and totals

Instead of creating a query into another query, you can summarize data with a subquery.
This example works with Northwind, to show how many distinct clients bought each product:
SELECT Products.ProductID, Products.ProductName, Count(Q.CustomerID) AS HowManyCustomers
FROM
(SELECT DISTINCT ProductID, CustomerID
FROM Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID) AS Q
INNER JOIN Products ON Q.ProductID = Products.ProductID
GROUP BY Products.ProductID, Products.ProductName;

Points to note:

  • The subquery is in the FROM clause, where it easily replaces another saved query.
  • The subquery in the FROM clause can return multiple fields.
  • The entire subquery is aliased (as Q in this example), so the main query can refer to (and aggregate) its fields.
  • Requires Access 2000 or later.

Filters and searches

Since subqueries can look up tables that are not in the main query, they are very useful for filtering forms and reports.
A Filter or WhereCondition is just a WHERE clause. A WHERE clause can contain a subquery. So, you can use a subquery to filter a form or report. You now have a way to filter a form or report on fields in tables that are not even in the RecordSource!
In our first example above, the main query used only the Customers table, and the subquery filtered it to those who had no orders in the last 90 days. You could filter the Customers form in exactly the same way:
'Create a subquery as a filter string.
strWhere = "NOT EXISTS (SELECT Orders.OrderID FROM Orders " & _
    "WHERE (Orders.CustomerID = Customers.CustomerID) AND (Orders.OrderDate > Date() - 90))"
'Apply the string as the filter of the form that has only the Customers table.
Forms!Customers.Filter = strWhere
Forms!Cusomters.FilterOn = True
'Or, use the string to filter a report that has only the Customers table.
DoCmd.OpenReport "Customers", acViewPreview, , strWhere
This technique opens the door for writing incredibly powerful searches. Add subqueries to the basic techniques explained in the Search form article, and you can offer a search where the user can select criteria based on any related table in the whole database.
The screenshot below is to whet your appetite for how you can use subqueries. The form is unbound, with each tab collecting criteria that will be applied against related tables. The final RESULTS tab offers to launch several reports which don't even have those tables. It does this by dynamically generating a huge WhereCondition string that consists of several subqueries. The reports are filtered by the subqueries in the string.
Subquery search screenshot

Other examples

These articles also illustrate the use of subqueries:

Conclusion

Now you know how powerful subqueries are, you will also want to know their limitations. Surviving subqueries gives insight to help you trouble-shoot them.

HomeIndex of tipsTop