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