- Computers & Software»
- Computer Software»
- Office Software Suites
How to use Queries in Access
In my job as an accountant, I have learned to use Microsoft Access on a regular basis. It is a great compliment to Excel. Using Access, allows you to easily manage large chunks of data and manipulate it into a more useful format. When you are done, you can either create a report in Access or export the information directly into Excel. The tool that is used to manipulate data in Access is called a query.
Creating Access Queries
Click on Access’ “Create” menu and click on the “Query Design” button. A box will pop up will come up titled, “Show Table”. This box will list all of the tables that you have set up in your database. It will also contain any queries that you have set up. You can choose to close the box or to select one or more tables or queries. Any field in a query can be limited or exclude data by using the “Criteria” line in a query. If you do not want any rows that are blank in a given field, you would enter “Is Not Null”. If you wanted only the blanks, it would be “Is Null”. You can exclude any rows in a field that has a certain word.
Basic Introduction to Database Queries in Access
Linking Tables or Queries in a Query
If you wish to link a table or query, for example, click on “Customer Name” in table T001 and drag your mouse over to “Customer Name” in table T002. Right-click on the line between the tables, select “Join Properties”, which will open up a pop up window. You have three choices to choose from: Only include rows where the joined fields from both tables are equal, include all records from T001 and only those from T002 that are equal with T001, and include all records from T002 and only those from T001 that are equal. For our example, I would choose the third option because I want to link the customer information with my invoice detail.
Formulas in Queries
Access also allows formulas to be entered into queries. Some of the functions are the same as Excel; however, many of them are different. A great example of this is the “IF” statement in Excel, is actually “IFF” in Access. The other thing to keep in mind is that you need to bracket “” field names when using them in formulas. If both tables have the same exact field name, you will have to include the table name like this:
Customer: =[T001]![Customer Name]
This will make a new field in the query called “Customer” and pull the customer name from table T001.
Types of Queries in Microsoft Access
There are six different types of Access database queries. I will explain how to use each type and to give an example of how each is used. For illustration purposes, I created a simple Access database with two tables in it. The first table is called T001, which is designed to hold customer information. It has the following fields: Customer Name, Customer Address, State, Zip Code, Phone Number, and Email Address. The second table, T002, lists invoice data with the follow fields: Customer Name, Invoice Number, Invoice Date, and Invoice Amount.
Select queries are the default selection and the most commonly used type of query. They are usually built off one or more tables or queries. If you select more than one table or query, you need to have a common field, such as “Customer Name” in our example. I use them all of the time to link tables/queries, run calculations, and to limit the data that I want to return. The biggest thing to remember with select queries is that they are independent in nature. That is, you can run them repeatedly without affecting anything else in the database.
Make Table Queries
Make table queries are used to create tables based on what information that you have in the query. They are created the same way as select queries are. Once you have selected your tables/queries, on Access’ “Design” menu, click on the “Make Table” button. A Dialogue box will open up asking you to enter a table name. I enter “T003” and clicked “Ok”. Once this query is run, it will create a table with any fields that you have created and populate the table that they query returns. If it is run again, it will replace the contents of what is in T003. The few times that I have used this type of query, I have run it once and then changed it to an “Append” query, which we will talk about next.
Append queries are similar to make table queries, in that, they will take whatever the query returns and populates the selected table with that information. They are different, in that, the table already needs to be established and append queries will not replace the data already in the table. When you click on the “Append” query button, it will ask you to select a table to append. Select the appropriate table and click “Ok”. A new row will show up on the bottom called “Append To:”. Select the appropriate field from the table that you are appending. Sometimes Access will auto populate this row, but it is a good idea to check to see if all of the fields are correct. I use this type of query regularly in my Access database at work. I take several tables or queries and append them to one table. A great example of this is all of my journal entry queries that pull information from the tables that contain the monthly data files and append it to a single table that become the journal entry table.
Update queries are a lot like the find and replace dialogue boxes, but they are must more powerful. When this type of query is run, it will find all of the records that match your criteria and will update them. It is important to remember that update queries cannot add or delete rows. In my personal experience, I rarely use this type of query. I would be more likely to use find and replace inside of a table.
Cross Tab Queries
Cross tab queries look much like a spreadsheet. This type of query is great for taking large amounts of data and summarizing it. Personally, I have never used this type of query.
Delete queries are commonly used with an Append query following it. They are used to delete the contents of an entire table or just certain records depending on how the query is set up. I use this query all of the time. As discussed above, I use this as part of calculating my journal entries each month. I run a delete query and then several append queries to dump the journal entry calculations in to one table.
Microsoft Access has a lot of power if you know how to use it. Queries are a big part of harnessing that power and manipulating data into a useable format.