Sending text messages with Twilio from MS Access

Hacks

As I have started using Twilio in my applications extensively, for one of the clients there was a need to send out texts from the MS Access database application. After looking around, I have read about MS Outlook text messaging service, but most of the providers were quite shady, so I had decided to write code myself. Which I am sharing now with you.

I started off with turning on Microsoft XML library first in References menu in Microsoft Access in order to make my application able of accessing Internet and sending out XML requests to Twilio:

After this is done, just create a new function in your Modules and throw this code in. Remember to replace YOUR_TWILIO_SID_HERE and YOUR_TWILIO_AUTH_TOKEN_HERE with appropriate values:

Public Function SendSmsTwilioMessage(From As String, ToNumber As String, MessageBody As String) As String

Dim Message As String
Dim Number As String

On Error GoTo Error_Handler

Const NOINTERNETAVAILABLE = -2147012889
Dim objSvrHTTP As ServerXMLHTTP
Dim varProjectID, varCatID, strT As String
Set objSvrHTTP = New ServerXMLHTTP

 

objSvrHTTP.Open "POST", "https://api.twilio.com/2010-04-01/Accounts/YOUR_TWILIO_SID_HERE/SMS/Messages.Xml", False, "YOUR_TWILIO_SID_HERE", "YOUR_TWILIO_AUTH_TOKEN_HERE"
objSvrHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
strT = "From=" & From & "&To=" & ToNumber & "&Body=" & MessageBody

objSvrHTTP.send strT

If objSvrHTTP.Status = 201 Then
txtXML = objSvrHTTP.responseText
SendSmsTwilioMessage = "Sent"

ElseIf objSvrHTTP.Status = 400 Then
SendSmsTwilioMessage = "Failed with error# " & _
objSvrHTTP.Status & _
" " & objSvrHTTP.statusText & vbCrLf & vbCrLf

ElseIf objSvrHTTP.Status = 401 Then

SendSmsTwilioMessage = "Failed with error# " & objSvrHTTP.Status & _
" " & objSvrHTTP.statusText & vbCrLf & vbCrLf

Else

SendSmsTwilioMessage = "Failed with error# " & objSvrHTTP.Status & _
" " & objSvrHTTP.statusText
End If


Exit_Procedure:

On Error Resume Next
Set objSvrHTTP = Nothing

Exit Function

Error_Handler:

Select Case Err.Number

Case NOINTERNETAVAILABLE

SendSmsTwilioMessage = "Connection to the internet cannot be made or " & _
"Twilio website address is wrong"

Case Else

SendSmsTwilioMessage = "Error: " & Err.Number & "; Description: " _

& Err.Description

Resume Exit_Procedure

Resume

End Select

End Function

 

7 Comments

  • Anthony Griggs says:

    Thanks for the nice piece of code sending Twilio messages via MS Access. I’ve done a lot of work in Access but not with web integration so I appreciate the heads up as you’ve saved me hours in research and development… I’m going to take this as a starting point and rack my brain and see what I come up with in receiving messages from Twilio via MS Access as well… If your interested in seeing what my solution is shoot me an email and I send you a copy when I am done.

    Thanks again

    Anthony

  • Peter Kalina says:

    Hi, i would like no know, how i can send sms message wia MS Access. For example i have all data on MS Access table and i would like send sms automatically. Can you help me please.

    Thanks

  • Marcy Gaynes says:

    This was working great but now I think the parent organization is blocking the twilio site. Do you know of a way to get around this?

    Thanks

  • edan mimran says:

    Nice code a:)

    i added it to my MS access and when i try to complie it, i get an error :
    User-defined type not defined.

    this error is for this line:
    Dim objSvrHTTP As ServerXMLHTTP

    any idea how to solve this error?
    thanks.

  • Mike S says:

    Oleksiy great tutorial on Twilio/ Ms Access Integration. Did you ever have any luck with capturing incoming SMS messages?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.