How to Export REST API to MS Access using VBA Command Button

Introduction

VBAIn our previous blog we saw how to export REST API to CSV using c# or Python. In this post we will look at specific example on How to Export REST API to MS Access using VBA Command Button, along with few other topics such as how to make REST API Call, how to read all Customers data from Odata using our ODBC JSON Driver and load it into the Ms Access Table.

We will go through the steps to read data from Odata and Load into MS Access using VBA Command Button.

In nutshell, this post will focus on how to make ODBC Driver Call using VBA.

So let’s get started.

Requirements

  1. First of all, you will need ZappySys ODBC PowerPack installed.
  2. Secondly, Make sure that Microsoft Access installed for VBA example.

An introduction to Rest API and OData

In this example, we will use OData (Open Data Protocol) to consume REST API. REST API (Representational State Transfer Application Program Interface) allows handling the interoperability between computers and the internet.

In REST API we can handle web services in different formats. In this example, we will work with the Northwind example. The Northwind example is available in this URL:

  1. By default, the data is displayed in XML format. To show the data in JSON use this URL:
  2. There are collections of data like Categories, CustomerDemographic, Customers, Invoices, etc. For example, the following URL will show the data of the categories collection:
  3. In the next steps, we will use ZappySys drivers to connect to this URL and query using OData.

Configure ODBC DSN for ZappySys JSON Driver

ODBC driver can be accessed in two modes.

  1. Using DSN
  2. Without DSN (Supply direct Connection String e.g. DRIVER={ZappySys JSON Driver}; …….. )

In this article, we will use the DSN approach (User DSN). We will first add the ZappySys JSON Driver in the ODBC Data Source Administrator.

Follow these steps to accomplish the task:

  1. First, Windows search, write ODBC and select the ODBC Data sources (32 bits)
    Open ODBC Data source

    Open ODBC Data Source

  2. As a second step, in ODBC Data source Administrator press the Add button.
    Add ZappySys

    Add ZappySys

  3. In this step, create the new data source, select ZappySys JSON Driver.
    add new zappysys

    Add new zappysys json driver

  4. Here we have several properties, write a data source name. In this example, the name will be ZappySys JSON to Excel.
  5. The Data Source (URL or file path) can specify the URL of the source or if it is a local file, you can specify the local path. In this example, the URL is:
    You can also specify a local file path as Data SourceFor a single file:  c:\data\myfile_1.json
    For multiple files: c:\data\myfile_*.json
  6. Configure the ODBC JSON Driver like this and test the connection:
    Configure JSON Driver for REST API Call

    Configure JSON Driver for REST API Call

  7. Now click on Copy Connection string Button.
    ODBC Driver : Copy Connection String

    ODBC Driver : Copy Connection String

  8. That’s it Connection string is copied to clipboard. Now let’s use this connection string in the VBA code.

How to import REST API data to Access using VBA

  1. Open the access database and create the table with the desire columns to get API data in it.
    Create Table in Access

    Create Table in Access

  2. Now close the table, and go to Create menu and click on Form Design to add the new blank form in design view.
    Access : Create a New Form in Design View

    Access : Create a New Form in Design View

  3. It will open the new form in the design view, now add new button with the desire text.
    Access : Add Command Button

    Access : Add Command Button

  4. Now right click on button and go to properties and click on … builder button and select Code Builder.
    Access : Code builder

    Access : Code builder

  5. When you click the button, it will open a new VBA instance, executing the command event associated with that button click.
    Next, insert the provided code inside the Command5_Click() function. In this example, the button is named Command5, and that’s why it’s added within this click function.
    The provided code encompasses the following steps:
    1. Ensure the table is closed:
      It’s crucial to close the table beforehand; otherwise, executing the subsequent command may result in an error.
    2. Truncate the table:
      Clear the existing data from the table. If you want to add fresh data each time
    3. Make the API call:
      Utilize the ODBC Driver connection string to initiate an API call.
    4. Insert into the Access table:
      Use the retrieved data from the API call to insert new records into the Access table.
    5. Open the table:
      Reopen the table to inspect the newly inserted data.
    ms-access-vba-button-onclick

    VBA Button OnClick Function

     

  6. Below the Private Sub Command5_Click() function, please add the necessary functions and procedures as provided below:

  7. Now, save the VBA form code and provide some appropriate form names.
    Save form vba code

    Save form vba code

     

  8. Make sure to add the reference to the Microsoft ActiveX Data Objects X.0 Library in the project settings.In Microsoft Access VBA,
    You can add the reference to the Microsoft ActiveX Data Objects X.0 Library by following these steps:
    1. In the VBA editor, go to “Tools” in the menu and select “References.”
    2. In the References dialog box, scroll down the list to find “Microsoft ActiveX Data Objects X.0 Library” (where “X.0” corresponds to the version number, such as 6.1, 2.8, etc.).
    3. Check the box next to the appropriate version to enable the reference.
    4. Click “OK” to save the changes and close the References dialog box.

    Now, your Access project has a reference to the Microsoft ActiveX Data Objects library, and you can use its features in your VBA code.

    Access : Add reference

    Access : Add reference

    If the MS ActiveX reference is not added, an error will be thrown.

    VBA : Error Type not defined

    VBA : Error Type not defined

  9. Close the code project, open the form in Form View, and click on the button.
    Access : Click Command Button

    Access : Click Command Button

  10. After clicking the button, it will initiate a REST API call, insert the data into the Access table, and open the table to display the inserted records along with a success message.
    Access : Open the table

    Access : Open the table

Troubleshooting Errors

While running in Access\Excel\other and reading data from DSN created with ODBC PowerPack, if you get this error "License type [ODBC_PP_TRIAL] not found or its expired"

Please refer to this article for the same:  Troubleshooting "License type [ODBC_PP_TRIAL] not found or its expired" error in Microsoft Access

Conclusion

In this article, we saw how to use VBA to call REST API. We show how to do REST API calls in Access using the ZappySys ODBC driver. Also, we show how to write VBA Code in MS Access for command button to make REST API call, truncate access table and insert record in the access table using VBA code. If you liked this article and you want to try, you can download the ZappySys ODBC installer here.

References

Form VBA Code:

After completing the migration of the higher-level VBA code, you’ll notice a transformation in the structure of your forms’ code.

 

Posted in JSON File / REST API Driver, Reporting - Microsoft Access, REST API and tagged , , , .