Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to web service in MS Access with VBA

Is it possible to connect to a web service (for example send a HTTP Request) via VBA in Microsoft Access? For example, the user clicks a button on a form, then a HTTP Request is sent to a web service that responds with OK.

Has anyone done this before?

Note: VBA, not VB.NET.

like image 260
Chris Avatar asked Mar 26 '13 18:03

Chris


People also ask

Does MS Access have an API?

Access doesn't provide any functionality to directly access the data from a HTTP endpoint (REST API). It can only function as a database(backend) in this scenario and you would need to look into other solutions to get the data from the database and provide it from a HTTP endpoint (REST API).

Does Microsoft Access have a Web server?

Microsoft Access is being phased out for web databases in favor of more robust servers like MySQL and Microsoft SQL Server. If you've got a small database, you can still get away with hosting a site based on Access.

Can you use VBA in Access?

Like macros, VBA lets you add automation and other functionality to your Access application. You can extend VBA by using third-party controls, and you can write your own functions and procedures for your own specific needs.


Video Answer


3 Answers

This is code I've used quite successfully with Access 2003. It's from the interwebs, copied and re-copied ages ago. It creates a XMLHttpRequest Object, sends an HTTP GET request, and returns the results as a string.

Public Function http_Resp(ByVal sReq As String) As String

    Dim byteData() As Byte
    Dim XMLHTTP As Object

    Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")

    XMLHTTP.Open "GET", sReq, False
    XMLHTTP.send
    byteData = XMLHTTP.responseBody

    Set XMLHTTP = Nothing

    http_Resp = StrConv(byteData, vbUnicode)

End Function

sReq is the URL; the function returns the response. You may need to make sure ActiveX Data Objects are enabled under your References (in the VBA editor, go to Tools > References).

like image 121
nucleon Avatar answered Oct 17 '22 21:10

nucleon


This is the code , which I used. You need to first reference Microsoft XML V6 for this code to work.

Public Sub GetPerson()
    'For API
    Dim reader As New XMLHTTP60

    reader.Open "GET", "www.exmple.com/users/5428a72c86abcdee98b7e359", False
    reader.setRequestHeader "Accept", "application/json"
    reader.send


    Do Until reader.ReadyState = 4
        DoEvents
    Loop

    If reader.Status = 200 Then
        Msgbox (reader.responseText)
    Else
        MsgBox "Unable to import data."
    End If
End Sub
like image 24
Adarsh Madrecha Avatar answered Oct 17 '22 21:10

Adarsh Madrecha


I have used the "Microsoft Office 2003 Web Services Toolkit 2.01" toolkit (available here) on a few projects. It worked pretty well for me, although I also wrote the web services it was talking to, so I had the luxury of being able to fiddle with both ends of the process when getting it to actually work. :)

In fact, I just upgraded one of those apps from Access_2003 to Access_2010 and the SOAP client part of the app continued to work without modification. However, I did encounter one wrinkle during pre-deployment testing:

My app would not compile on a 64-bit machine running 32-bit Office_2010 because it did not like the early binding of the SoapClient30 object. When I switched to using late binding for that object the code would compile, but it did not work. So, for that particular app I had to add a restriction that 64-bit machines needed to be running 64-bit Office.

Also, be aware that Microsoft's official position is that "All SOAP Toolkits have been replaced by the Microsoft .NET Framework." (ref. here).

like image 42
Gord Thompson Avatar answered Oct 17 '22 19:10

Gord Thompson