Use Embedded SQL Query for better efficiency and security

Creating a query in Microsoft Access is simple and most people can learn to do it in a relatively short period of time. However, using query in Microsoft Access has two major drawbacks:

  1. If you want to create a parameter query and the parameters are saved in a table, it will be very slow to run such query. It can take ten times more time to run such query. If you convert the regular query into SQL query, it will avoid such problem.
  2. Your query is subject to the mercy of the users. Users may accidentally or intentionally change your query. If you want to maintain the integrity of the Access file, it is better to embed your query into your Visual Basic codes and then save your file into MDE or ACCDE format.

Please follow the below steps to convert a regular query to an embedded SQL query.

pic1 embedded sql

Step 1: Create a regular query in Microsoft Access.

pic2 embedded sql

Step 2: Click the “View” command on the ribbon and then choose the SQL View and you will see how the query look like in SQL coding.

pic3 embedded sql

Step 3: Copy the entire query using CTRL+A (select all) and CTRL+C (copy).

pic4 embedded sql

Step 4: If you want to create a new VB module, please click the “Create” command on the ribbon, then click the “Macro” and choose “Module” option to open the “Module”. You can also use an existing module, just open it and then put your SQL query in it.

pic5 embedded sql

Step 5: Create a new Subroutine in the module and paste the SQL query in it. In the below example, I created a new subroutine called “SearchByAcctNum()” and I pasted the SQL query in it. After you paste the SQL query, you will see that it is marked as red color because the VB editor regards your statement contains error in syntax. There are two reasons for the syntax error:

  • It is not a VB statement. You need to add “DoCmd.RunSQL” at the front and then a parenthesis and a double quotation mark. So the VB statement should look like this at the beginning:

DoCmd.RunSQL (“SELECT ….. “)

  • The SQL query code is too long and split into several lines for the VB editor to recognize its entity. You need to split the SQL statement by inserting & “ at the beginning and ” _ at the end. Please see below illustration.
pic7 embedded sql

Step 6: Once you are done with the subroutine and confirm that there is no error, you can put the name of the subroutine to the table of “Switchboard Items”. Your subroutine will be activated by the users when the users click the button on the Switchboard.

Step 7: Go to the “Switchboard Item” on the Tables through the navigation pane.

pic8 embedded sql

Step 8: In the Switchboard table, put the “8” under the “Command” field (to activate a VB Module) and put the name of the subroutine under the “Argument.

pic9 embedded sql

Step 9: If you create a new module, Access will ask you if you want to save the new module. Click “Yes” and give the new module a name and click “OK’ to save.

pic6 embedded sql

More Posts

Excel Vlookup Function

The VLOOKUP function in Excel is a powerful tool that allows you to search for a specific value in a table and return a corresponding