Using Access VBA to extract remote database data items via MSXML2.XMLHTTP.6.0.

Said El Noshokaty 1 Reputation point
2021-11-30T11:00:12.023+00:00

Hi,

I'm trying to write Access VBA subroutine by which I can extract remote database data items using MSXML2.XMLHTTP.6.0. Hereinafter a sample subroutine that results in "Access is denied" when executing WinHttpReq.Send. I need to know why, as windows remote access is tuned on? I also need to know how to save the extracted data item in memory, rather than a file, as to write it to an Access table later on.

Sub ExtractDataItems()

Dim url As String, filePath As String, attempts As Integer
Dim WinHttpReq As Object, oStream  As Object

' DESKTOP-0EKQG3O\SQLExpress, sosdata, cargo, broker are the names of SQL Server, database, record, and field, respectively
url = "http://"MyUrl"/DESKTOP-0EKQG3O\SQLExpress/sosdata/cargo/broker?dt=" & Format(Now(), "yyyymmddhhmmss")
filePath = "c:\sos\SQLServerExtracts.txt"
attempts = 3
Set WinHttpReq = CreateObject("MSXML2.XMLHTTP.6.0")
On Error GoTo TryAgain
TryAgain:
attempts = attempts - 1
Err.Clear
If attempts > 0 Then
WinHttpReq.Open "GET", url, False
WinHttpReq.Send
url = WinHttpReq.ResponseBody

   If WinHttpReq.Status = 200 Then
      Set oStream = CreateObject("ADODB.Stream")
      oStream.Open
      oStream.Type = 1
      oStream.Write WinHttpReq.ResponseBody
      oStream.SaveToFile filePath, 2 ' 1 = no overwrite, 2 = overwrite
      oStream.Close
   End If
Else
   MsgBox ("A4. No internet connection is available.")
End If

End Sub

Best regards.

Said

Microsoft 365 and Office Access Development
SQL Server Other
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2021-12-02T08:27:40.977+00:00

    sosdata, cargo, broker are the names of SQL Server, database, record, and field, respectively

    You want to access SQL Server via Http? In which way? Over old & deprecated Soap Endpoint? Have you created such? Or over an other endpoint; which one?

    0 comments No comments

  2. Said El Noshokaty 1 Reputation point
    2021-12-02T19:29:25.953+00:00

    Hi Olaf,

    You, as a Microsoft MVP, perhaps know better than myself what MSXML2.XMLHTTP.6.0. is. My understanding is that it is a Microsoft protocol that uses both the SOAP protocol and REST architecture. I'm using a VBA subroutine, which I found on the internet, to extract data items on the web sites. I used it successfully before. The problem now is that I'm using it to extract data items from my SQL Server database called "sosdata" on my computer as a test before I use it with other customers later on. The question is why it fails this time, given the comments you have seen.

    Best regards.

    Said


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.