<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>command button Archives | ZappySys Blog</title>
	<atom:link href="https://zappysys.com/blog/tag/command-button/feed/" rel="self" type="application/rss+xml" />
	<link>https://zappysys.com/blog/tag/command-button/</link>
	<description>SSIS / ODBC Drivers / API Connectors for JSON, XML, Azure, Amazon AWS, Salesforce, MongoDB and more</description>
	<lastBuildDate>Wed, 17 Jan 2024 11:26:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.4</generator>

<image>
	<url>https://zappysys.com/blog/wp-content/uploads/2023/01/cropped-zappysys-symbol-large-32x32.png</url>
	<title>command button Archives | ZappySys Blog</title>
	<link>https://zappysys.com/blog/tag/command-button/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Export REST API to MS Access using VBA Command Button</title>
		<link>https://zappysys.com/blog/export-rest-api-ms-access-using-vba-command-button/</link>
		
		<dc:creator><![CDATA[ZappySys]]></dc:creator>
		<pubDate>Sat, 29 Jun 2019 09:34:55 +0000</pubDate>
				<category><![CDATA[JSON File / REST API Driver]]></category>
		<category><![CDATA[Reporting - Microsoft Access]]></category>
		<category><![CDATA[REST API]]></category>
		<category><![CDATA[command button]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[odbc]]></category>
		<category><![CDATA[vba]]></category>
		<guid isPermaLink="false">https://zappysys.com/blog/?p=7317</guid>

					<description><![CDATA[<p>Introduction In 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 [&#8230;]</p>
<p>The post <a href="https://zappysys.com/blog/export-rest-api-ms-access-using-vba-command-button/">How to Export REST API to MS Access using VBA Command Button</a> appeared first on <a href="https://zappysys.com/blog">ZappySys Blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2><span id="Introduction">Introduction</span></h2>
<p><a href="https://zappysys.com/blog/wp-content/uploads/2019/06/VBA_250x250.png" target="_blank" rel="noopener"><img decoding="async" class="alignleft wp-image-7318 size-thumbnail" src="https://zappysys.com/blog/wp-content/uploads/2019/06/VBA_250x250-150x150.png" alt="VBA" width="150" height="150" srcset="https://zappysys.com/blog/wp-content/uploads/2019/06/VBA_250x250-150x150.png 150w, https://zappysys.com/blog/wp-content/uploads/2019/06/VBA_250x250.png 250w" sizes="(max-width: 150px) 100vw, 150px" /></a>In our previous blog we saw how to <a href="https://zappysys.com/blog/how-to-export-rest-api-to-csv/" target="_blank" rel="noopener">export REST API to CSV using c# or Python</a>. In this post we will look at specific example on <strong>How to Export REST API to MS Access using VBA Command Button</strong>, along with few other topics such as how to make <strong>REST API Call</strong>, how to read all Customers data from Odata using our ODBC JSON Driver and load it into the Ms Access Table.</p>
<p>We will go through the steps to read data from Odata and Load into MS Access using VBA Command Button.</p>
<p>In nutshell, this post will focus on how to make ODBC Driver Call using VBA.</p>
<p>So let’s get started.</p>
<h2><span id="Requirements">Requirements</span></h2>
<ol>
<li>First of all, you will need <a href="https://zappysys.com/products/odbc-powerpack/download/" target="_blank" rel="noopener">ZappySys ODBC PowerPack</a> installed.</li>
<li>Secondly, Make sure that Microsoft Access installed for VBA example.</li>
</ol>
<h2><span id="An_introduction_to_Rest_API_and_OData">An introduction to Rest API and OData</span></h2>
<p>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.</p>
<p>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:</p>
<div id="crayon-5bc180f1dd015720033381" class="crayon-syntax crayon-theme-vs2012 crayon-font-courier-new crayon-os-pc print-yes notranslate">
<div class="crayon-main">
<pre class="crayon-plain-tag">https://services.odata.org/V3/Northwind/Northwind.svc</pre>
</div>
</div>
<ol>
<li>By default, the data is displayed in XML format. To show the data in JSON use this URL:<br />
<pre class="crayon-plain-tag">https://services.odata.org/V3/Northwind/Northwind.svc/?$format=json</pre>
</li>
<li>There are collections of data like Categories, CustomerDemographic, Customers, Invoices, etc. For example, the following URL will show the data of the categories collection:<br />
<pre class="crayon-plain-tag">https://services.odata.org/V3/Northwind/Northwind.svc/Customers?$format=json</pre>
</li>
<li>In the next steps, we will use ZappySys drivers to connect to this URL and query using OData.</li>
</ol>
<h2><span id="Configure_ODBC_DSN_for_ZappySys_JSON_Driver">Configure ODBC DSN for ZappySys JSON Driver</span></h2>
<p>ODBC driver can be accessed in two modes.</p>
<ol>
<li>Using DSN</li>
<li>Without DSN (Supply direct Connection String e.g. <strong>DRIVER={ZappySys JSON Driver}; ……..</strong> )</li>
</ol>
<p>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.</p>
<p>Follow these steps to accomplish the task:</p>
<ol>
<li>First, <strong>Windows search</strong>, write <strong>ODBC</strong> and select the <strong>ODBC Data sources (32 bits)</strong>
<div id="attachment_2780" class="wp-caption aligncenter">
<div id="attachment_2780" style="width: 395px" class="wp-caption aligncenter"><a href="https://i1.wp.com/zappysys.com/blog/wp-content/uploads/2018/03/open-ODBC-Data-souce-administrator.png?ssl=1" target="_blank" rel="noopener"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-2780" class="wp-image-2780 size-full" src="https://i1.wp.com/zappysys.com/blog/wp-content/uploads/2018/03/open-ODBC-Data-souce-administrator.png?zoom=0.8999999761581421&amp;resize=385%2C520&amp;ssl=1" alt="Open ODBC Data source" width="385" height="520" /></a><p id="caption-attachment-2780" class="wp-caption-text">Open ODBC Data Source</p></div>
</div>
</li>
<li>As a second step, in ODBC Data source Administrator press the <strong>Add</strong> button.
<div id="attachment_2725" class="wp-caption aligncenter">
<div id="attachment_2725" style="width: 600px" class="wp-caption aligncenter"><a href="https://i1.wp.com/zappysys.com/blog/wp-content/uploads/2018/03/add-ZappySys.png?ssl=1" target="_blank" rel="noopener"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-2725" class="wp-image-2725 size-full" src="https://zappysys.com/blog/wp-content/uploads/2018/03/add-ZappySys.png" alt="Add ZappySys" width="590" height="423" srcset="https://zappysys.com/blog/wp-content/uploads/2018/03/add-ZappySys.png 590w, https://zappysys.com/blog/wp-content/uploads/2018/03/add-ZappySys-300x215.png 300w" sizes="(max-width: 590px) 100vw, 590px" /></a><p id="caption-attachment-2725" class="wp-caption-text">Add ZappySys</p></div>
</div>
</li>
<li>In this step, create the new data source, select <strong>ZappySys JSON Driver.</strong>
<div id="attachment_2772" class="wp-caption aligncenter">
<div id="attachment_2772" style="width: 302px" class="wp-caption aligncenter"><a href="https://i1.wp.com/zappysys.com/blog/wp-content/uploads/2018/03/create-new-data-source-zappysys-json-driver.png?ssl=1" target="_blank" rel="noopener"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-2772" class="wp-image-2772 size-medium" src="https://i1.wp.com/zappysys.com/blog/wp-content/uploads/2018/03/create-new-data-source-zappysys-json-driver.png?zoom=0.8999999761581421&amp;resize=292%2C218&amp;ssl=1" alt="add new zappysys" width="292" height="217" /></a><p id="caption-attachment-2772" class="wp-caption-text">Add new zappysys json driver</p></div>
</div>
</li>
<li>Here we have several properties, write a data source name. In this example, the name will be ZappySys JSON to Excel.</li>
<li>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:<br />
<pre class="crayon-plain-tag">https://services.odata.org/V3/Northwind/Northwind.svc/Customers?$format=json</pre>
You can also specify a local file path as Data SourceFor a single file:  c:\data\myfile_1.json<br />
For multiple files: c:\data\myfile_*.json</li>
<li>Configure the ODBC JSON Driver like this and test the connection:
<div id="attachment_7321" style="width: 730px" class="wp-caption aligncenter"><a href="https://zappysys.com/blog/wp-content/uploads/2018/10/odbc-JSON-driver-odata-configuration.png" target="_blank" rel="noopener"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-7321" class="wp-image-7321 size-medium_large" src="https://zappysys.com/blog/wp-content/uploads/2018/10/odbc-JSON-driver-odata-configuration-768x698.png" alt="Configure JSON Driver for REST API Call" width="720" height="654" srcset="https://zappysys.com/blog/wp-content/uploads/2018/10/odbc-JSON-driver-odata-configuration-768x698.png 768w, https://zappysys.com/blog/wp-content/uploads/2018/10/odbc-JSON-driver-odata-configuration-300x273.png 300w, https://zappysys.com/blog/wp-content/uploads/2018/10/odbc-JSON-driver-odata-configuration.png 849w" sizes="(max-width: 720px) 100vw, 720px" /></a><p id="caption-attachment-7321" class="wp-caption-text">Configure JSON Driver for REST API Call</p></div></li>
<li>Now click on Copy Connection string Button.
<div id="attachment_7322" style="width: 730px" class="wp-caption aligncenter"><a href="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-driver-copy-connection-string.png" target="_blank" rel="noopener"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-7322" class="wp-image-7322 size-medium_large" src="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-driver-copy-connection-string-768x672.png" alt="ODBC Driver : Copy Connection String" width="720" height="630" srcset="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-driver-copy-connection-string-768x672.png 768w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-driver-copy-connection-string-300x263.png 300w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-driver-copy-connection-string.png 802w" sizes="(max-width: 720px) 100vw, 720px" /></a><p id="caption-attachment-7322" class="wp-caption-text">ODBC Driver : Copy Connection String</p></div></li>
<li>That&#8217;s it Connection string is copied to clipboard. Now let&#8217;s use this connection string in the VBA code.</li>
</ol>
<h2><span id="How_to_import_REST_API_data_to_Excel">How to import REST API data to Access using VBA</span></h2>
<ol>
<li>Open the access database and create the table with the desire columns to get API data in it.
<div id="attachment_7324" style="width: 730px" class="wp-caption aligncenter"><a href="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-create-table-in-access.png" target="_blank" rel="noopener"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-7324" class="wp-image-7324 size-medium_large" src="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-create-table-in-access-768x511.png" alt="Create Table in Access" width="720" height="479" srcset="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-create-table-in-access-768x511.png 768w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-create-table-in-access-300x200.png 300w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-create-table-in-access-1024x682.png 1024w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-create-table-in-access-272x182.png 272w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-create-table-in-access.png 1546w" sizes="(max-width: 720px) 100vw, 720px" /></a><p id="caption-attachment-7324" class="wp-caption-text">Create Table in Access</p></div></li>
<li>Now close the table, and go to Create menu and click on Form Design to add the new blank form in design view.
<div id="attachment_7325" style="width: 642px" class="wp-caption aligncenter"><a href="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-create-form-desing-access.png" target="_blank" rel="noopener"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-7325" class="wp-image-7325 size-full" src="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-create-form-desing-access.png" alt="Access : Create a New Form in Design View" width="632" height="402" srcset="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-create-form-desing-access.png 632w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-create-form-desing-access-300x191.png 300w" sizes="(max-width: 632px) 100vw, 632px" /></a><p id="caption-attachment-7325" class="wp-caption-text">Access : Create a New Form in Design View</p></div></li>
<li>It will open the new form in the design view, now add new button with the desire text.
<div id="attachment_7326" style="width: 730px" class="wp-caption aligncenter"><a href="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-add-access-command-button.png" target="_blank" rel="noopener"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-7326" class="wp-image-7326 size-medium_large" src="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-add-access-command-button-768x459.png" alt="Access : Add Command Button" width="720" height="430" srcset="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-add-access-command-button-768x459.png 768w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-add-access-command-button-300x179.png 300w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-add-access-command-button-1024x612.png 1024w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-add-access-command-button.png 1215w" sizes="(max-width: 720px) 100vw, 720px" /></a><p id="caption-attachment-7326" class="wp-caption-text">Access : Add Command Button</p></div></li>
<li>Now right click on button and go to properties and click on &#8230; builder button and select Code Builder.
<div id="attachment_7327" style="width: 730px" class="wp-caption aligncenter"><a href="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-select-code-builder-access.png" target="_blank" rel="noopener"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-7327" class="wp-image-7327 size-medium_large" src="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-select-code-builder-access-768x529.png" alt="Access : Code builder" width="720" height="496" srcset="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-select-code-builder-access-768x529.png 768w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-select-code-builder-access-300x207.png 300w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-select-code-builder-access.png 934w" sizes="(max-width: 720px) 100vw, 720px" /></a><p id="caption-attachment-7327" class="wp-caption-text">Access : Code builder</p></div></li>
<li>When you click the button, it will open a new VBA instance, executing the command event associated with that button click.<br />
Next, insert the provided code inside the <i>Command5_Click()</i> function. In this example, the button is named Command5, and that&#8217;s why it&#8217;s added within this click function.<br />
The provided code encompasses the following steps:</p>
<ol>
<li><strong>Ensure the table is closed:</strong><br />
It&#8217;s crucial to close the table beforehand; otherwise, executing the subsequent command may result in an error.</li>
<li><strong>Truncate the table:<br />
</strong>Clear the existing data from the table. If you want to add fresh data each time</li>
<li><strong>Make the API call:<br />
</strong>Utilize the ODBC Driver connection string to initiate an API call.</li>
<li><strong>Insert into the Access table:<br />
</strong>Use the retrieved data from the API call to insert new records into the Access table.</li>
<li><strong>Open the table:<br />
</strong>Reopen the table to inspect the newly inserted data.</li>
</ol>
<pre class="crayon-plain-tag">' Pass your ZappySys Driver Connection String: Refer to below link to learn how to obtain the ZappySys Driver connection string:
    ' https://community.zappysys.com/t/how-to-copy-the-zappysys-driver-connection-string/172
    Dim zsConnStr As String
    zsConnStr = "DRIVER={ZappySys JSON Driver};DataPath='https://services.odata.org/V3/Northwind/Northwind.svc/Customers?$format=json';Filter='$.value[*]';IncludeParentColumns=0;RequestMethod='GET';RequestHeaders='cache-control: no-cache || Accept: */*'"
        
    'ZappySys Driver Query
    Dim zsDriverQuery As String
    zsDriverQuery = "Select * from $"
 
    'MS Access Table Name
    Dim tableName As String
    tableName = "tblCustomers"

    ' 1. Close Table If Open
    CloseTableIfOpen tableName

    ' 2. Truncate Table (Optional): If you want to load fresh data each time, uncomment the line below.
    'TruncateTable tableName

    ' 3. Import Data From API Into Table
    ImportDataFromAPIIntoTable zsConnStr, zsDriverQuery, tableName

    ' 4. Open Table for Viewing (Optional)
    OpenTableForViewing tableName

    ' Show Message after Loading Data Successfully
    MsgBox "Data Loaded Successfully!"</pre>
<div id="attachment_10917" style="width: 744px" class="wp-caption alignnone"><a href="https://zappysys.com/blog/wp-content/uploads/2019/06/ms-access-vba-button-onclick.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-10917" class="size-full wp-image-10917" src="https://zappysys.com/blog/wp-content/uploads/2019/06/ms-access-vba-button-onclick.png" alt="ms-access-vba-button-onclick" width="734" height="809" srcset="https://zappysys.com/blog/wp-content/uploads/2019/06/ms-access-vba-button-onclick.png 734w, https://zappysys.com/blog/wp-content/uploads/2019/06/ms-access-vba-button-onclick-272x300.png 272w" sizes="(max-width: 734px) 100vw, 734px" /></a><p id="caption-attachment-10917" class="wp-caption-text">VBA Button OnClick Function</p></div>
<p>&nbsp;</li>
<li>Below the <i>Private Sub Command5_Click()</i> function, please add the necessary functions and procedures as provided below:</p><pre class="crayon-plain-tag">Sub ImportDataFromAPIIntoTable(zsConnStr As String, zsDriverQuery As String, tableName As String)
    Dim dscn As New ADODB.Connection
    dscn.Open zsConnStr

    Dim rs As Object
    Set rs = New ADODB.Recordset
    
    ''ZappySys Driver Query Goes here
    rs.Open zsDriverQuery, dscn

    Dim strQuery As String
    strQuery = ""
    ' Check if the recordset is not empty
    If Not rs.EOF Then
        ' Get the field names
        Dim fieldNames As String
        fieldNames = ""

        For i = 1 To rs.Fields.Count
            fieldNames = fieldNames &amp; rs.Fields(i - 1).Name
            If i &lt; rs.Fields.Count Then
                fieldNames = fieldNames &amp; ","
            End If
        Next i

        ' Generate the INSERT script
        Dim insertScript As String
        ' Iterate through the recordset to get the values
        Do Until rs.EOF
            ' Generate Insert script
            insertScript = "INSERT INTO " &amp; tableName &amp; " (" &amp; fieldNames &amp; ") VALUES"
            insertScript = insertScript &amp; vbCrLf &amp; "("
            For i = 1 To rs.Fields.Count
                ' Assuming all fields are text, you might need to handle different data types appropriately
                
                If IsNull(rs.Fields(i - 1).Value) Then
                    insertScript = insertScript &amp; "''"
                Else
                    insertScript = insertScript &amp; "'" &amp; EscapeSingleQuote(rs.Fields(i - 1).Value) &amp; "'"
                End If
                
                
                If i &lt; rs.Fields.Count Then
                    insertScript = insertScript &amp; ","
                End If
            Next i
            insertScript = insertScript &amp; ")"
            
            'Insert data
            CurrentProject.Connection.Execute insertScript
            
            rs.MoveNext
        Loop
    End If
    
    ' Close the recordset and connection
    rs.Close
    dscn.Close
    Set rs = Nothing
    Set conn = Nothing
    
End Sub

Sub OpenTableForViewing(tableName As String)
    If Not IsTableOpen(tableName) Then
        DoCmd.OpenTable tableName
    End If
End Sub

Public Function IsTableOpen(strName As String) As Boolean
     IsTableOpen = SysCmd(acSysCmdGetObjectState, acTable, strName)
End Function

Sub CloseTableIfOpen(tableName As String)
    If IsTableOpen(tableName) Then
        DoCmd.SelectObject acTable, tableName
        DoCmd.RunCommand acCmdSaveRecord
        DoCmd.Close
    End If
End Sub


Sub TruncateTable(tableName As String)
    Dim strSql As String
    strSql = "DELETE FROM " &amp; tableName &amp; ";"
    CurrentProject.Connection.Execute strSql
End Sub

Public Function EscapeSingleQuote(strData As String) As String
     EscapeSingleQuote = Replace(strData, "'", "''")
End Function</pre><p>
</li>
<li>Now, save the VBA form code and provide some appropriate form names.
<div id="attachment_7332" style="width: 730px" class="wp-caption aligncenter"><a href="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-vba-save-form.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-7332" class="size-medium_large wp-image-7332" src="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-vba-save-form-768x943.png" alt="Save form vba code" width="720" height="884" srcset="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-vba-save-form-768x943.png 768w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-vba-save-form-244x300.png 244w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-vba-save-form-834x1024.png 834w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-vba-save-form.png 849w" sizes="(max-width: 720px) 100vw, 720px" /></a><p id="caption-attachment-7332" class="wp-caption-text">Save form vba code</p></div>
<p>&nbsp;</li>
<li>Make sure to add the reference to the Microsoft ActiveX Data Objects X.0 Library in the project settings.In Microsoft Access VBA,<br />
You can add the reference to the Microsoft ActiveX Data Objects X.0 Library by following these steps:</p>
<ol>
<li>In the VBA editor, go to &#8220;Tools&#8221; in the menu and select &#8220;References.&#8221;</li>
<li>In the References dialog box, scroll down the list to find &#8220;Microsoft ActiveX Data Objects X.0 Library&#8221; (where &#8220;X.0&#8221; corresponds to the version number, such as 6.1, 2.8, etc.).</li>
<li>Check the box next to the appropriate version to enable the reference.</li>
<li>Click &#8220;OK&#8221; to save the changes and close the References dialog box.</li>
</ol>
<p>Now, your Access project has a reference to the Microsoft ActiveX Data Objects library, and you can use its features in your VBA code.</p>
<div id="attachment_7336" style="width: 730px" class="wp-caption aligncenter"><a href="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-add-reference.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-7336" class="size-medium_large wp-image-7336" src="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-add-reference-768x648.png" alt="Access : Add reference" width="720" height="608" srcset="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-add-reference-768x648.png 768w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-add-reference-300x253.png 300w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-add-reference.png 804w" sizes="(max-width: 720px) 100vw, 720px" /></a><p id="caption-attachment-7336" class="wp-caption-text">Access : Add reference</p></div>
<p>If the MS ActiveX reference is not added, an error will be thrown.</p>
<div id="attachment_7335" style="width: 721px" class="wp-caption aligncenter"><a href="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-vba-error-type-not-found.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-7335" class="wp-image-7335 size-full" src="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-vba-error-type-not-found-e1705479944324.png" alt="VBA : Error Type not defined" width="711" height="797" srcset="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-vba-error-type-not-found-e1705479944324.png 711w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-vba-error-type-not-found-e1705479944324-268x300.png 268w" sizes="(max-width: 711px) 100vw, 711px" /></a><p id="caption-attachment-7335" class="wp-caption-text">VBA : Error Type not defined</p></div></li>
<li>Close the code project, open the form in Form View, and click on the button.
<div id="attachment_7333" style="width: 730px" class="wp-caption aligncenter"><a href="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-click-button.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-7333" class="size-medium_large wp-image-7333" src="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-click-button-768x705.png" alt="Access : Click Command Button" width="720" height="661" srcset="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-click-button-768x705.png 768w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-click-button-300x275.png 300w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-click-button.png 939w" sizes="(max-width: 720px) 100vw, 720px" /></a><p id="caption-attachment-7333" class="wp-caption-text">Access : Click Command Button</p></div></li>
<li>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.
<div id="attachment_7334" style="width: 730px" class="wp-caption aligncenter"><a href="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-code-success-message.png"><img loading="lazy" decoding="async" aria-describedby="caption-attachment-7334" class="size-medium_large wp-image-7334" src="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-code-success-message-768x705.png" alt="Access : Open the table" width="720" height="661" srcset="https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-code-success-message-768x705.png 768w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-code-success-message-300x275.png 300w, https://zappysys.com/blog/wp-content/uploads/2019/06/odbc-access-code-success-message.png 939w" sizes="(max-width: 720px) 100vw, 720px" /></a><p id="caption-attachment-7334" class="wp-caption-text">Access : Open the table</p></div></li>
</ol>
<div class="content_block" id="custom_post_widget-8935"><h2>Troubleshooting Errors</h2>
<p>While running in Access\Excel\other and reading data from DSN created with ODBC PowerPack, if you get this error "<strong>License type [ODBC_PP_TRIAL] not found or its expired</strong>"</p>

<p>Please refer to this article for the same:  <a href="https://zappysys.zendesk.com/hc/en-us/articles/360042521533-Troubleshooting-License-type-ODBC-PP-TRIAL-not-found-or-its-expired-error-in-Microsoft-Access" target="_blank" rel="noopener">Troubleshooting "License type [ODBC_PP_TRIAL] not found or its expired" error in Microsoft Access</a></p></div>
<h2><span id="Conclusion">Conclusion</span></h2>
<p>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 <a href="https://zappysys.com/products/odbc-powerpack/download/" target="_blank" rel="noopener">ZappySys ODBC installer here</a>.</p>
<h2><span id="References">References</span></h2>
<ul>
<li><a href="https://zappysys.com/products/odbc-powerpack/download/" target="_blank" rel="noopener">ZappySys ODBC installer.</a></li>
<li><a href="https://stackoverflow.com/questions/49984642/access-odbc-query" target="_blank" rel="noopener">Access ODBC Query</a></li>
<li><a href="https://docs.microsoft.com/en-us/sql/ado/guide/appendixes/using-ado-with-microsoft-visual-basic?view=sql-server-2017" target="_blank" rel="noopener">Using ADO with Microsoft Visual Basic and Visual Basic for Applications</a></li>
</ul>
<h2>
Form VBA Code:</h2>
<p>After completing the migration of the higher-level VBA code, you&#8217;ll notice a transformation in the structure of your forms&#8217; code.</p><pre class="crayon-plain-tag">Option Compare Database

Private Sub Command5_Click()
 
    ' Pass your ZappySys Driver Connection String: Refer to below link to learn how to obtain the ZappySys Driver connection string:
    ' https://community.zappysys.com/t/how-to-copy-the-zappysys-driver-connection-string/172
    Dim zsConnStr As String
    zsConnStr = "DRIVER={ZappySys JSON Driver};DataPath='https://services.odata.org/V3/Northwind/Northwind.svc/Customers?$format=json';Filter='$.value[*]';IncludeParentColumns=0;RequestMethod='GET';RequestHeaders='cache-control: no-cache || Accept: */*'"
        
    'ZappySys Driver Query
    Dim zsDriverQuery As String
    zsDriverQuery = "Select * from $"
 
    'MS Access Table Name
    Dim tableName As String
    tableName = "tblCustomers"

    ' 1. Close Table If Open
    CloseTableIfOpen tableName

    ' 2. Truncate Table (Optional): If you want to load fresh data each time, uncomment the line below.
    'TruncateTable tableName

    ' 3. Import Data From API Into Table
    ImportDataFromAPIIntoTable zsConnStr, zsDriverQuery, tableName

    ' 4. Open Table for Viewing (Optional)
    OpenTableForViewing tableName

    ' Show Message after Loading Data Successfully
    MsgBox "Data Loaded Successfully!"
    
End Sub

Sub ImportDataFromAPIIntoTable(zsConnStr As String, zsDriverQuery As String, tableName As String)
    Dim dscn As New ADODB.Connection
    dscn.Open zsConnStr

    Dim rs As Object
    Set rs = New ADODB.Recordset
    
    ''ZappySys Driver Query Goes here
    rs.Open zsDriverQuery, dscn

    Dim strQuery As String
    strQuery = ""
    ' Check if the recordset is not empty
    If Not rs.EOF Then
        ' Get the field names
        Dim fieldNames As String
        fieldNames = ""

        For i = 1 To rs.Fields.Count
            fieldNames = fieldNames &amp; rs.Fields(i - 1).Name
            If i &lt; rs.Fields.Count Then
                fieldNames = fieldNames &amp; ","
            End If
        Next i

        ' Generate the INSERT script
        Dim insertScript As String
        ' Iterate through the recordset to get the values
        Do Until rs.EOF
            ' Generate Insert script
            insertScript = "INSERT INTO " &amp; tableName &amp; " (" &amp; fieldNames &amp; ") VALUES"
            insertScript = insertScript &amp; vbCrLf &amp; "("
            For i = 1 To rs.Fields.Count
                ' Assuming all fields are text, you might need to handle different data types appropriately
                
                If IsNull(rs.Fields(i - 1).Value) Then
                    insertScript = insertScript &amp; "''"
                Else
                    insertScript = insertScript &amp; "'" &amp; EscapeSingleQuote(rs.Fields(i - 1).Value) &amp; "'"
                End If
                
                
                If i &lt; rs.Fields.Count Then
                    insertScript = insertScript &amp; ","
                End If
            Next i
            insertScript = insertScript &amp; ")"
            
            'Insert data
            CurrentProject.Connection.Execute insertScript
            
            rs.MoveNext
        Loop
    End If
    
    ' Close the recordset and connection
    rs.Close
    dscn.Close
    Set rs = Nothing
    Set conn = Nothing
    
End Sub

Sub OpenTableForViewing(tableName As String)
    If Not IsTableOpen(tableName) Then
        DoCmd.OpenTable tableName
    End If
End Sub

Public Function IsTableOpen(strName As String) As Boolean
     IsTableOpen = SysCmd(acSysCmdGetObjectState, acTable, strName)
End Function

Sub CloseTableIfOpen(tableName As String)
    If IsTableOpen(tableName) Then
        DoCmd.SelectObject acTable, tableName
        DoCmd.RunCommand acCmdSaveRecord
        DoCmd.Close
    End If
End Sub


Sub TruncateTable(tableName As String)
    Dim strSql As String
    strSql = "DELETE FROM " &amp; tableName &amp; ";"
    CurrentProject.Connection.Execute strSql
End Sub

Public Function EscapeSingleQuote(strData As String) As String
     EscapeSingleQuote = Replace(strData, "'", "''")
End Function</pre><p>
&nbsp;</p>
<h2></h2>
<p>The post <a href="https://zappysys.com/blog/export-rest-api-ms-access-using-vba-command-button/">How to Export REST API to MS Access using VBA Command Button</a> appeared first on <a href="https://zappysys.com/blog">ZappySys Blog</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
