Using Queries within Microsoft Access VBA
-
Upload
antonio-soto -
Category
Technology
-
view
745 -
download
7
description
Transcript of Using Queries within Microsoft Access VBA
![Page 1: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/1.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title Using Queries within Microsoft Access VBA
Antonio Soto http://www.ithelp4u.ca/en/ [email protected]
SOFTWARE ENGINEER, MCT, MCP, MCDBA, MCAD
![Page 2: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/2.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title Create Queries in Access with VBA
SQL queries in Access are an essential tool for allowing the user to view and update data. In a relational database, it is very rare to obtain meaningful data from a single table. Queries join tables together, or they can be used to update, delete, or append records.
When a form is being used, it is sometimes useful to either create a new query to do something specific or to change the SQL to adapt to changes that the user is making.
Create Query
2 www.ithelp4u.ca/en
![Page 3: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/3.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title Create Queries in Access with VBA
Create Query
3 www.ithelp4u.ca/en
![Page 4: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/4.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title Create Queries in Access with VBA
Create Query
Inside the Database object, you will find the QueryDefs collection. This collection holds all the queries in your application.
4 www.ithelp4u.ca/en
![Page 5: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/5.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title
Delete Query
We can also use VBA to delete queries from the QueryDefs collection.
We can see what is in this collection by looking at your queries within the Navigation pane of Access.
The QueryDefs collection reflects exactly what appears in the pane.
Deleting a Query in VBA Access
5 www.ithelp4u.ca/en
![Page 6: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/6.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title
Delete Query
Deleting a Query in VBA Access
√ Objects created in code are not automatically refreshed in the Navigation window until you execute the RefreshDatabaseWindow command.
6 www.ithelp4u.ca/en
![Page 7: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/7.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title
Modify Query
Modify a Query in VBA Access
We can change the SQL in an Access query by using the SQL property of the query definition.
7 www.ithelp4u.ca/en
![Page 8: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/8.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title
Modify Query
Modify a Query in VBA Access - Execute
We can construct the SQL in program code and then execute the code by using either CurrentDB.Execute strSQL or DoCmd.RunSQL strSQL. The CurrentDb.Execute() method offers many advantages over RunSQL. Trappable Errors, a Count of affected records, as well as no warning dialogs regardless of the SetWarnings setting. The best advantage is the ability to trap and handle errors in the SQL processing: something that you can't do with DoCmd.RunSQL. To enable this, use the dbFailOnError option when executing the statement: CurrentDb.Execute "DELETE * FROM MyTempTable", dbFailOnError This redirects any errors encountered by Jet to your procedures error handler.
8 www.ithelp4u.ca/en
![Page 9: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/9.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title
Parameters
Creating parameters in a QueryDef by using code
If we want to create parameters in program code, we do this in the SQL
9 www.ithelp4u.ca/en
![Page 10: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/10.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title
Create Table
Create Table Structures
As your Access application is running, there may be a need to create a table on-the-fly or amend a table structure. For example, you may be importing data into a table from a text file. If for any reason the data in the field in the text file is too large for the field in your table, it will be truncated and data will be lost.
You may also wish to create a table for a specific operation, do that operation, and then delete the table.
You can also change the properties of each individual field, such as whether it is a required field or whether it is indexed.
10 www.ithelp4u.ca/en
![Page 11: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/11.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title
Create Table
Create Table Structures
11 www.ithelp4u.ca/en
![Page 12: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/12.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title
Create Table
Create Table - Data Definition DDL Access supports Data Definition Language (DDL) instructions, which is part of the SQL Standard.
12 www.ithelp4u.ca/en
![Page 13: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/13.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title
Create Table
Create Table – List Tables The TableDefs collection contains Table Definition objects; these are all the physical tables, linked tables, and system tables in the Access database. The following code lists all of the tables and linked tables in the database:
13 www.ithelp4u.ca/en
![Page 14: Using Queries within Microsoft Access VBA](https://reader031.fdocuments.us/reader031/viewer/2022013111/55630d3fd8b42a51498b472b/html5/thumbnails/14.jpg)
Delete Query
Modify Query
Parameters
Create Query
Create Table
Course Title |This is the slide title Thank you / Merci / Gracias
14