Proper use of XMLHTTP

I am writing some code in Excel VBA to access information on a piece of instrumentation. I have the following code written that sort of works by I need help with the xmlhttp.ReadyState portion.  If I check the ready state in the Do loop it never returns a 4. I put the msgbox in there to see what it was doing. On the first iterration it returns a 1. Once I click OK on the message box it exits the loop then returns a 4 and then returns the response text . I thought I was not giving it enough time to respond so I put in the counter. That did not work. Why does the msgbox trigger the correct ready state? How do I fix it?
Public Function Post_Data(ByVal dString As String, ByVal URL As String, ByVal NeedResponse As Boolean) As String
    
    Dim xmlhttp As Object
    Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
    
    xmlhttp.Open "POST", URL, NeedResponse
 
    ' Indicate type of request being submitted (e.g. form data)
    xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-   urlencoded"
 
    ' Indicate the header length
    xmlhttp.setRequestHeader "Content-Length", Len(dString) + 1
    MsgBox "Sending data string " & dString
    xmlhttp.Send (dString)
    
    Dim counter As Double
    
    Do While xmlhttp.ReadyState <> 4
        'MsgBox "Ready State A = " & xmlhttp.ReadyState
        counter = counter + 1
        If counter = 10000 Then Exit Do
    Loop
    MsgBox "Counter = " & counter
    'MsgBox "Ready State B = " & xmlhttp.ReadyState
    'MsgBox "Status = " & xmlhttp.Status
    If xmlhttp.ReadyState = 4 Then
        If xmlhttp.Status = 200 Then
            Post_Data = xmlhttp.ResponseText
        ElseIf xmlhttp.Status <> 200 Then
            MsgBox "Server error: " & xmlhttp.Status
        End If
    ElseIf xmlhttp.ReadyState <> 4 Then
        MsgBox "Ready State error: " & xmlhttp.ReadyState
    End If
End Function

Open in new window

pwkenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DaveCommented:
Why do you have a counter - I'm guessin that its exiting prematurely

Inside your Do loop you could use

a) nothing
b) DoEvents
c) Sleep (either s an API or as part of Windows Script Host)

Cheers

Dave
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pwkenAuthor Commented:
I put the counter in to attempt to see what was going on within the loop. I also thought of it as a safety net in the event that the ready state never reached 4.

The DoEvents works and solves my original question so points will be awarded; however, this leads me to another question. How to handle the Do loop in the event that the response state never reaches 4?
0
DaveCommented:
Hi,

I've never seen it not get achieve readystate.

But you would need to use a timer, or counter to exit the loop if that was of concern

Cheers

Dave
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.