structuring code

shutzy

Registered User.
Local time
Today, 12:24
Joined
Sep 14, 2011
Messages
775
im looking to use twilio sms services. i am integrating it into my database. to begin with i want the code behind a button on click event.

twilio help has given me some code that will work but the way it is set out i am strugling with to put in a private function

i am wanting to structure if any one can help.

Code:
[COLOR=#2b2e2f][FONT=Lucida Sans Unicode]Sub test()[/FONT][/COLOR]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]sendSMS "646-480-1977", "8583829141", "Sending SMS is easy with Twilio!"[/COLOR][/FONT]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]End Sub[/COLOR][/FONT]
[COLOR=#2b2e2f][FONT=Lucida Sans Unicode]Function sendSMS(fromNumber As String, toNumber As String, body As String)[/FONT][/COLOR]
[COLOR=#2b2e2f][FONT=Lucida Sans Unicode]'Function RectangleArea(Height As Double, Width As Double) As Double[/FONT][/COLOR]
[COLOR=#2b2e2f][FONT=Lucida Sans Unicode]' replace with your account's settings[/FONT][/COLOR]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]' available in the Dashboard[/COLOR][/FONT]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]accountSid = "xxx"[/COLOR][/FONT]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]authToken = "yyy"[/COLOR][/FONT]
[COLOR=#2b2e2f][FONT=Lucida Sans Unicode]' setup the URL[/FONT][/COLOR]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]baseUrl = "[URL="https://api.twilio.com/"][COLOR=#0072c6]https://api.twilio.com[/COLOR][/URL]"[/COLOR][/FONT][COLOR=#2b2e2f]
[FONT=Lucida Sans Unicode]smsUrl = baseUrl & "/2010-04-01/Accounts/" & accountSid & "/SMS/Messages"[/FONT][/COLOR]
[COLOR=#2b2e2f][FONT=Lucida Sans Unicode]' setup the request and authorization[/FONT][/COLOR]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]Dim http As MSXML2.ServerXMLHTTP60[/COLOR][/FONT]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]'Set http = Server.CreateObject("MSXML2.ServerXMLHTTP60")[/COLOR][/FONT]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]Set http = New MSXML2.ServerXMLHTTP60[/COLOR][/FONT]
[COLOR=#2b2e2f][FONT=Lucida Sans Unicode]http.Open "POST", smsUrl, False, accountSid, authToken[/FONT][/COLOR]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"[/COLOR][/FONT]
[COLOR=#2b2e2f][FONT=Lucida Sans Unicode]' message parameters[/FONT][/COLOR]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]from = fromNumber ' the number to send the message from[/COLOR][/FONT]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]recipient = toNumber ' the number to send the message to[/COLOR][/FONT]
[COLOR=#2b2e2f][FONT=Lucida Sans Unicode]Dim postData As String[/FONT][/COLOR]
[COLOR=#2b2e2f][FONT=Lucida Sans Unicode]postData = "From=" & from[/FONT][/COLOR]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]postData = postData & "&To=" & recipient[/COLOR][/FONT]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]postData = postData & "&Body=" & body[/COLOR][/FONT]
[COLOR=#2b2e2f][FONT=Lucida Sans Unicode]' send the POST data[/FONT][/COLOR]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]http.send postData[/COLOR][/FONT]
[COLOR=#2b2e2f][FONT=Lucida Sans Unicode]' optionally write out the response if you need to check if it worked[/FONT][/COLOR]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]' Response.Write http.responseText[/COLOR][/FONT]
[COLOR=#2b2e2f][FONT=Lucida Sans Unicode]' clean up[/FONT][/COLOR]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]Set http = Nothing[/COLOR][/FONT]
[FONT=Lucida Sans Unicode][COLOR=#2b2e2f]End Function[/COLOR][/FONT]

im not sure if putting it in a private function is the correct way but until i know more about private, public etc i would prefer it to be this way. then at least i can edit it and make it what i want to move foreward.

also what will happen. i see 'RectangleArea(Height As Double, Width As Double) As Double'. will this open a rectangle box?

thanks
 
G'd evening,
The answer to your last? question is not. That function is commented ' and won't do anything. If you uncomment that line the code will break. You can't declare a function within a function.
By the way, in simple words, a public function is a procedure that you can call from anywhere in your code.
A private function is a procedure that you can call just within the scope where is declared.

G'd luck
 
ive tried to do it as it should be, through creating a module and then putting

Private Sub btnTexts_Click()
sendSMS "118-324-1616", "7919158140", "Sending SMS is easy with Twilio!"
End Sub

behind a btn on click event. i have now got a
Compile error
variable not defined

with

accountSid =

hightlighted.

can anyone help me with this. just to get it going.

thanks
 
Shutzy, you have hardly declare any of the variables.. Also http is not really a great variable name.. so I changed it to varHTTP.. There is no need of assigning the fromNumber and toNumber to another variable..
Code:
Sub test()
    sendSMS "646-480-1977", "8583829141", "Sending SMS is easy with Twilio!"
End Sub

Function sendSMS(fromNumber As String, toNumber As String, body As String)
[COLOR=Green]    'Function RectangleArea(Height As Double, Width As Double) As Double
    ' replace with your account's settings
    ' available in the Dashboard[/COLOR]
    
    Dim accountSid As String, authToken As String
    Dim baseURL As String, smsURL As String
    Dim varHTTP As MSXML2.ServerXMLHTTP60
    Dim postData As String
    
    accountSid = "xxx"
    authToken = "yyy"
    
   [COLOR=Green] 'Setup the URL[/COLOR]
    baseURL = "https://api.twilio.com"
    smsURL = baseURL & "/2010-04-01/Accounts/" & accountSid & "/SMS/Messages"
    
    [COLOR=Green]'Setup the request and authorization[/COLOR]
    Set varHTTP = New MSXML2.ServerXMLHTTP60
    varHTTP.Open "POST", smsURL, False, accountSid, authToken
    varHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    
    [COLOR=Green]'Message parameters[/COLOR]
    postData = "From=" & fromNumber
    postData = postData & "&To=" & toNumber
    postData = postData & "&Body=" & body
    
    [COLOR=Green]'Send the POST data[/COLOR]
    varHTTP.Send postData
   [COLOR=Green] ' optionally write out the response if you need to check if it worked
    ' Response.Write varHTTP.responseText
    ' clean up[/COLOR]
    Set varHTTP = Nothing
End Function
 
thanks for the help AGAIN!.

im trying to contruct the message now that it is working. im trying to refer to text boxes within the form. i have tried

Code:
Private Sub btnTexts_Click()
sendSMS "118-324-1616", DLookup("[MobileTelephoneNumber]", "tmptblTextReminders", "OrderID = " & [Forms]![frmAppointment-Status1And2]![OrderID]), "Hi Me.FirstName, This is your reminder that you have an appointment on Me.DateOfTreatment at Me.OrderTime.!"
End Sub

and tried

Code:
Private Sub btnTexts_Click()
sendSMS "118-324-1616", DLookup("[MobileTelephoneNumber]", "tmptblTextReminders", "OrderID = " & [Forms]![frmAppointment-Status1And2]![OrderID]), "Hi [Me.FirstName], This is your reminder that you have an appointment on [Me.DateOfTreatment] at [Me.OrderTime].!"
End Sub

the test text comes through as it is written rather than refering to the text box contents. im guessing that this is because it think the whole thing is String. how do i tell it that it is part of the string but to fill in the gaps?

thanks

just to note. the previous code i posted was supplied by the support at twilio. i will forward the new code that i have used and confirm that it works so they can send it out to people like me. thank you. you have helped many others with this.
 
You should always concatenate the variables else the whole thing as you said will be a string.. Something like..
Code:
Private Sub btnTexts_Click()
    sendSMS "118-324-1616", DLookup("[MobileTelephoneNumber]", "tmptblTextReminders", "OrderID = " & [Forms]![frmAppointment-Status1And2]![OrderID]), "Hi [COLOR=Red][B]" &[/B][/COLOR] Me.FirstName [COLOR=Red][B]& "[/B][/COLOR], This is your reminder that you have an appointment on [COLOR=Red][B]" &[/B][/COLOR] Me.DateOfTreatment [COLOR=Red][B]& "[/B][/COLOR] at [COLOR=Red][B]" &[/B][/COLOR] Me.OrderTime [COLOR=Red][B]& "[/B][/COLOR].!"
End Sub
 
thanks. i knew i had to do something but wasnt sure what. just one last thing before i really make this wole thing very complicated. the time keeps coming through as hh:mm:ss. is the a possibility to format

" & Format(Me.OrderTime,"hh:mm") & "

i tried that but it didnt work. i think i have the right ideas just missing the fundamentals of coding.

thanks
 
sorry, scratch that. i just tried it again and its working.

Format(Me.OrderTime, "hh:nn")

i think it was the space between the , and the first " that did it.
 
Hi all,

I am attempting to implement Twilio SMS into my Access 2013 application. I tried using the code below and I cannot get it to work at all. I've uncommented the Response line to see what is going on however I keep getting a compile error:

Variable not declared

So, I've tried inserting:
Dim Response As Variant

and then running the code but I receive an 'Object is required' error. Can anyone point me in the right direction with this? Many thanks.



Function sendSMS(fromNumber As String, toNumber As String, body As String)

' replace with your account's settings
' available in the Dashboard

Dim accountSid As String, authToken As String
Dim baseURL As String, smsURL As String
Dim varHTTP As MSXML2.ServerXMLHTTP60
Dim postData As String

accountSid = "aaaaa"
authToken = "bbbb"

'Setup the URL
baseURL = "https://api.twilio.com"
smsURL = baseURL & "/2010-04-01/Accounts/" & accountSid & "/SMS/Messages"

'Setup the request and authorization
Set varHTTP = New MSXML2.ServerXMLHTTP60
varHTTP.Open "POST", smsURL, False, accountSid, authToken
varHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

'Message parameters
postData = "From=" & fromNumber
postData = postData & "&To=" & toNumber
postData = postData & "&Body=" & body

'Send the POST data
varHTTP.send postData
' optionally write out the response if you need to check if it worked
Response.Write varHTTP.responseText
' clean up
Set varHTTP = Nothing
End Function
 

Users who are viewing this thread

Back
Top Bottom