SQL Query in Access

SELECT QUERY

In order to create programs in Access we have to be very familiar with the language for queries on databases SQL (Structured Query Language). This language was introduced by IBM in order to question its relational database, and since then has always been used for this purpose also by Microsoft and others, in practice it has become a standard for relational databases. Each time, however, that it was appropriate for their own purposes, some software companies have created the dialects of this real programming language, so Microsoft has created its own dialect, what today are in the database tool for Office. Access is a relational database to a file within which we have the tables, the main part of a store in the tables have fields that form the structure of the table, while the data are called records. Each field can be identified with a name and a data type that contains text, numbers, currencies, dates, etc. .. To select data in a table we need to create a select query:

SELECT a field name, field name, 2, …. * FROM table name

SELECT command makes sure to select specific fields or all (*) from a table. Take for example the table “communication containing the fields” IDTown, City, Zip Code, State “(the first of these will be of type long integer and autoincrementante with primary index, which is used for joins that we will see in future lessons). To select only the name of the town and started with “C” we write:

SELECT City FROM towns WHERE City LIKE ‘C *’ ORDER BY City

with this command we have isolated only the towns beginning with “C” and in ascending alphabetical order. We see that we have written the SQL commands in uppercase, for this differenaziarli from the field names and tables. Instead of “LIKE” could also write “=”, but in this case we could not use the asterisk, and then we could only find a common time and that if the argument of “LIKE” or “=” is a text must enclose it in quotes to differentiate them from the numbers. Access everything can be done graphically, but to also write the code by hand selecting the type of SQL view. In the tree view, a query is much easier to understand. To create a new query just go to the tab and click on new query, select frame and choose the table on which to run the query, in our case the table “common” field then select “Common”, let us appears that having the check box is checked, select ascending or descending order and then criteria ‘LIKE’ C * “‘. What we have written before now we have made graphically, much more comfortable, but not a habit, at least initially, to create queries in this way to learn programming in SQL you need to experience it, you get only doing the job manually. If you try to display the SQL query to see the code created.
If you need examples of useful database to see the query in VBA code, you can download for example Calus 2012 that has a lot of search queries in the source code can be found among the free software on our website.

Next Lesson >>

This entry was posted in Database and tagged , . Bookmark the permalink.