创建与基于REST的API的Microsoft Access数据库连接

时间:2015-09-24 18:16:19

标签: api rest ms-access access-vba database-connection

我正在尝试在Microsoft Access 2013中创建基于REST的API(this API, to be specific)提供的数据的实时链接。最终目标是使数据在查询中可用,就像它是本地数据库一样。

如何实现这一目标?具体来说,我正在努力如何让Access根据请求调用API。我能想到实现类似结果的唯一方法是编写一个脚本,通过API提取整个数据库并将其转换为Access可读格式,然后以设定的时间间隔运行该脚本。但我真的很想找到一个实时工作的解决方案,即使它比本地缓存数据库慢一些。

1 个答案:

答案 0 :(得分:11)

由于对RESTful Web服务的调用实际上只是一种特定的HTTP请求,您至少可以使用Microsoft XML库向Web服务发送HTTP请求并解析它返回的任何内容。例如,当我运行以下VBA代码

' VBA project Reference required:
' Microsoft XML, v3.0

Dim httpReq As New MSXML2.ServerXMLHTTP
httpReq.Open "GET", "http://whois.arin.net/rest/poc/KOSTE-ARIN", False
httpReq.send
Dim response As String
response = httpReq.responseText
Debug.Print response

字符串变量response包含对我的请求的XML响应。它看起来像这样(在重新格式化以获得可读性之后):

<?xml version='1.0'?>
<?xml-stylesheet type='text/xsl' href='http://whois.arin.net/xsl/website.xsl' ?>
<poc xmlns="http://www.arin.net/whoisrws/core/v1" xmlns:ns2="http://www.arin.net/whoisrws/rdns/v1"
xmlns:ns3="http://www.arin.net/whoisrws/netref/v2" termsOfUse="https://www.arin.net/whois_tou.html"
inaccuracyReportUrl="http://www.arin.net/public/whoisinaccuracy/index.xhtml">
  <registrationDate>2009-10-02T11:54:45-04:00</registrationDate>
  <ref>http://whois.arin.net/rest/poc/KOSTE-ARIN</ref>
  <city>Chantilly</city>
  <companyName>ARIN</companyName>
  <iso3166-1>
    <code2>US</code2>
    <code3>USA</code3>
    <name>UNITED STATES</name>
    <e164>1</e164>
  </iso3166-1>
  <firstName>Mark</firstName>
  <handle>KOSTE-ARIN</handle>
  <lastName>Kosters</lastName>
  <emails>
    <email>markk@kosters.net</email>
    <email>markk@bjmk.com</email>
  </emails>
  <resources termsOfUse="https://www.arin.net/whois_tou.html"
  inaccuracyReportUrl="http://www.arin.net/public/whoisinaccuracy/index.xhtml">
    <limitExceeded limit="256">false</limitExceeded>
  </resources>
  <phones>
    <phone>
      <number>+ 1-703-227-9870</number>
      <type>
        <description>Office</description>
        <code>O</code>
      </type>
    </phone>
  </phones>
  <postalCode>20151</postalCode>
  <comment>
    <line number="0">I&#39;m really MAK21-ARIN</line>
  </comment>
  <iso3166-2>VA</iso3166-2>
  <streetAddress>
    <line number="0">3635 Concorde Parkway</line>
  </streetAddress>
  <updateDate>2015-05-26T11:36:55-04:00</updateDate>
</poc>

您的网络服务返回的内容可能会略有不同。或者,与上面的ARIN whois RWS的情况一样,您可能有多种数据格式可供选择; XML只是默认值。我可以使用

请求纯文本回复
httpReq.Open "GET", "http://whois.arin.net/rest/poc/KOSTE-ARIN.txt", False

在这种情况下response将包含

#
# ARIN WHOIS data and services are subject to the Terms of Use
# available at: https://www.arin.net/whois_tou.html
#


Name:           Kosters, Mark 
Handle:         KOSTE-ARIN
Company:        ARIN
Address:        3635 Concorde Parkway
City:           Chantilly
StateProv:      VA
PostalCode:     20151
Country:        US
RegDate:        2009-10-02
Updated:        2015-05-26
Comment:        I'm really MAK21-ARIN
Phone:          +1-703-227-9870 (Office)
Email:          markk@bjmk.com
Email:          markk@kosters.net
Ref:            http://whois.arin.net/rest/poc/KOSTE-ARIN
#
# ARIN WHOIS data and services are subject to the Terms of Use
# available at: https://www.arin.net/whois_tou.html
#