[AccessD] Exchange Rate - Currency Converter thingy

Gustav Brock gustav at cactus.dk
Fri Jun 29 05:15:04 CDT 2012


Hi Shamil

Yes:

strXML = OpenURL("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml")

using API calls:

<code>
Private Declare Sub InternetCloseHandle Lib "wininet.dll" ( _
  ByVal hInet As Long)
  
Private Declare Function InternetOpenA Lib "wininet.dll" ( _
  ByVal sAgent As String, _
  ByVal lAccessType As Long, _
  ByVal sProxyName As String, _
  ByVal sProxyBypass As String, _
  ByVal lFlags As Long) _
  As Long
  
Private Declare Function InternetOpenUrlA Lib "wininet.dll" ( _
  ByVal hOpen As Long, _
  ByVal sUrl As String, _
  ByVal sHeaders As String, _
  ByVal lLength As Long, _
  ByVal lFlags As Long, _
  ByVal lContext As Long) _
  As Long
  
Private Declare Sub InternetReadFile Lib "wininet.dll" ( _
  ByVal hFile As Long, _
  ByVal sBuffer As String, _
  ByVal lNumBytesToRead As Long, _
  ByRef lNumberOfBytesRead As Long)

Public Function OpenURL( _
  ByVal URL As String, _
  Optional ByVal OpenType As Long) _
  As String
  
  Const IOTPreconfig  As Long = 0
  Const IOTDirect     As Long = 1
  Const IOTProxy      As Long = 3

  Const INET_RELOAD = &H80000000
  
  Dim hInet As Long
  Dim hURL As Long
  Dim Buffer As String * 2048
  Dim Bytes As Long
  
  Select Case OpenType
    Case IOTPreconfig, IOTDirect, IOTProxy
      ' OK
    Case Else
      Exit Function
  End Select
  
  'Inet-Connection öffnen:
  hInet = InternetOpenA(vbNullString, OpenType, vbNullString, vbNullString, 0)
  hURL = InternetOpenUrlA(hInet, URL, vbNullString, 0, INET_RELOAD, 0)
  
  'Daten sammeln:
  Do
    InternetReadFile hURL, Buffer, Len(Buffer), Bytes
    If Bytes = 0 Then Exit Do
    OpenURL = OpenURL & Left$(Buffer, Bytes)
  Loop
  
  'Inet-Connection schließen:
  InternetCloseHandle hURL
  InternetCloseHandle hInet

End Function
</code>

I have used this to extract values from standard HTML pages, but for extensive use of xml files I would use the XML library.

/gustav


>>> mcp2004 at mail.ru 29-06-12 11:46 >>>
Hi Gustav --

Do you mean downloading and parsing source XML byte by byte?
*
Thank you.

-- Shamil

Fri, 29 Jun 2012 11:42:46 +0200 ** "Gustav Brock" <gustav at cactus.dk>:
 
  
  
Hi Shamil

"naive-hardcoder" ... he he, quick and dirty, next step would be to just download the xml-string and parse it.

/gustav


>>> mcp2004 at mail.ru 29-06-12 11:07 >>>
Hi All --

Here is my final code sample for the subject task - I'd call it "naive-hardcoder" approach:

Public Sub TestMe1()
Dim uri As String
uri = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"
Dim xmlDoc As New MSXML2.DOMDocument30
xmlDoc.async = False
xmlDoc.Load (uri)
Dim xml As String
xml = xmlDoc.DocumentElement.xml
Dim pos1 As Integer
Dim pos2 As Integer
Dim currencyCode As String
currencyCode = "RUB"
pos1 = InStr(xml, "currency=""" + currencyCode)
pos1 = InStr(pos1, xml, "rate=""")
pos1 = InStr(pos1 + 1, xml, """")
pos2 = InStr(pos1 + 1, xml, """")
Dim currencyRate As String
currencyRate = Mid(xml, pos1 + 1, pos2 - pos1 - 1)
Debug.Print "EUR/" + currencyCode + " => " + currencyRate
End Sub

Thank you.

-- Shamil



More information about the AccessD mailing list