Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a message from MS Access using twilio?

I have done a lot of research but I have found nothing. Has anyways already done this? Any examples?

I want to send a message using a button in a form so it should call a vba code and use some information in the same form to send the message. Any ideas would be great!!

I'm using Access 2010

like image 855
calengineer Avatar asked Dec 14 '25 23:12

calengineer


2 Answers

Twilio evangelist here.

The short answer is you need to POST to the Twilio REST API using VBA.

Here is a bit of sample code that shows how to do this using XMLHTTP:

Public Function GET_MESGS()
    Dim Message As String
    Dim Number As String

    On Error GoTo Error_Handler

    Const NOINTERNETAVAILABLE = -2147012889

    Dim objSvrHTTP As XMLHTTP
    Dim varProjectID, varCatID, strT As String

    Set objSvrHTTP = New XMLHTTP
    objSvrHTTP.Open "POST", "https://api.twilio.com/2010-04-01/Accounts/[YOUR_ACCOUNT_SID]/SMS/", False, "[YOUR_ACCOUNT_SID]", "[YOUR_AUTH_TOKEN]"

    objSvrHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    objSvrHTTP.send "message=" & Message & "&from=15555555555&to=" & Number

    debug.print objSvrHTTP.responseText

    ‘If objSvrHTTP.status = 201 Then
    ‘txtXML = objSvrHTTP.responseText
    ‘MsgBox "Sent"
    ElseIf objSvrHTTP.status = 400 Then
        MsgBox "Failed with error# " & _
            objSvrHTTP.status & _
            " " & objSvrHTTP.statusText & vbCrLf & vbCrLf
    ElseIf objSvrHTTP.status = 401 Then
        MsgBox "Failed with error# " & objSvrHTTP.status & _
            " " & objSvrHTTP.statusText & vbCrLf & vbCrLf
    Else
        MsgBox "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
            MsgBox "Connection to the internet cannot be made or " & _
                "Twilio website address is wrong"

        Case Else
            MsgBox "Error: " & Err.Number & "; Description: " & Err.Description

            Resume Exit_Procedure

        Resume

    End Select

End Function

Devin

like image 127
Devin Rader Avatar answered Dec 16 '25 23:12

Devin Rader


Here is a VBA example that can very easily be adapted for Access.

Sub SendSMS()

'credit to Coding is Fun
'https://www.youtube.com/channel/UCZjRcM1ukeciMZ7_fvzsezQ
'https://www.youtube.com/watch?v=v49tYBmnnFg&list=FLDXOykjNf5zoOlCFtYYyPng&index=3

'Authentication
ACCOUNTSID = ActiveSheet.Range("C4").Value
AUTHTOKEN = ActiveSheet.Range("C5").Value

'Variables
fromNumber = "%2B" & ActiveSheet.Range("C6").Value   'use as variable or something else to call number DO NOT ADD leading +
toNumber = ActiveSheet.Range("C8").Value 'use as variable or something else to call number DO NOT ADD leading +
BodyText = ActiveSheet.Range("C9").Value

'Use XML HTTP
Set Request = CreateObject("MSXML2.ServerXMLHTTP.6.0")

'Specify URL
Url = "https://api.twilio.com/2010-04-01/Accounts/" & ACCOUNTSID & "/Messages.json"

'Post URL with authentication
Request.Open "POST", Url, False, ACCOUNTSID, AUTHTOKEN

'Request Header
Request.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

'Concatenate PostData
postData = "From=" & fromNumber & "&To=%2B" & toNumber & "&Body=" & BodyText

'Send the Post Data
Request.send postData

'[OPTIONAL] Get response text (result)
MsgBox Request.responseText

End Sub
like image 20
Fouche Viljoen Avatar answered Dec 16 '25 22:12

Fouche Viljoen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!