I am trying to use a custom control that is a 3rd party implementation of the VB Winsock control. The control is called OSWinsock (http://www.ostrosoft.com/oswinsck/os...reference.aspx). I have added this as a reference into VBA and can create the oswinsock object. I am having trouble using the controls methods properly though. Based on the example provided by the author, I added the control into my form via the following:
Option Compare Database
Option Explicit
Dim WithEvents wsTCP As OSWINSCK.Winsock
This now provides the following events to the form:
object.OnClose()
connection closed
object.OnConnect()
connection to remote host established
object.OnConnectionRequest(requestID As Long)
connection request from remote host
object.OnDataArrival(bytesTotal As Long)
new data arrived
object.OnError(Number As Integer, Description As String, Scode As Long, Source As String, HelpFile As String, HelpContext As Long, CancelDisplay As Boolean)
error occured
object.OnSendComplete()
data send is complete
object.OnSendProgress(bytesSent As Long, bytesRemaining As Long)
data send is in progress
object.OnStatusChanged(ByVal Status As String)
component status changed
On my access form, I have a command button that runs a bunch of VBA code and then calls the Connect method of the oswinsock object as follows:
Set wsTCP = CreateObject("OSWINSCK.Winsock")
wsTCP.Connect strServer, lngPort
The connection is made successfully as in the OnConnect event of the wsTCP object I get the following from a debug statement that calls the wsTCP.Status property:
wsTCP_OnConnect: wsTCP.Status = Connect
Also, in the OnDataArrival event of the wsTCP object I have the following and I can see that the remote server responded when I read the value of strTemp
wsTCP.GetData strTemp
My problem is this. I need to programmatically communicate with the remote server using multiple calls to the SendData and GetData methods of the wsTCP object. I thought I could simply call the Connect method and then do something like this, all within the same OnClick event from the command button on my form. Something like this:
Set wsTCP = CreateObject("OSWINSCK.Winsock")
wsTCP.Connect strServer, lngPort
wsTCP.GetData strTemp 'get response from server after connection is made
wsTCP.SendData strDataToSend
wsTCP.GetData strTemp 'get response after sending 1st command
wsTCP.SendData strDataToSend 'send the next command
etc... (including other code to check that the connection has been made, send the responses to a buffer, etc)
However, even though I call the Connect method, the connection doesn't get made until AFTER the rest of the code in the OnClick event of the command button completes. If I take all that other code (GetData/SendData) and put it into a different Command button and click it, it executes successfully, i.e. the Connect method opens a socket to the remote server after I click Command button1 and then code from Command button 2 uses that open socket connection to SendData/GetData successfully. I need to make multiple connections to different servers that I read in from a file so I need this to all happen programmatically without the need to click on a second command button to actually communicate with the remote server.
Any advice would be helpful.
Thanks