INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How To

Read text from a web-site by PeteJohnston
Posted: 15 Dec 03

I had to write some code in VB6 to import currency exchange rate data into a database and I thought I'd post it here in case anyone else needed to do the same thing. The API part of the code was from Ryan Woodward who has many thanks from me! The Text from the website can be retrieved and placed in an text field or string by using a simple function call from within any form event, eg. if you had a command button called btnRetrieve you could put it in btnRetrieve_Click(). I then used Split, Left, Mid and Right to extract the parts of the data which I needed. In the following, txtURL is the web-site address and txtURLSource is the string variable you are placing it into.

txtURLSource = OpenUrl(txtURL)

To implement it, create a module called rwInetXfer and copy the following into it. It worked a treat for me, both in VB6 and when I imported it into Access2K.

'--rwInetXfer
'::  ::ver 1.0vb::
'::  ::orig. author R. Woodward::
'::  ::Ryan_Woodward@yahoo.com::
'::
'::WHY:
'::  Many versions of the Microsoft Internet Transfer Control
'::  that shipped with VB are buggy or unreliable
'::  Here's a nice substitute with source code
'::  Currently only implements OpenUrl
'::
'::DESC:
'::  Internet File Transfer Object
'::  Retrieve internet files over http using
'::  Windows system DLLs (wininet) and system network config
'::
'::E.G.:
'::  Dim inet As rwInetXfer
'::  debug.Print "HTML SOURCE-"
'::  debug.Print inet.OpenUrl("http://www.yahoo.com")
'::
Option Explicit
Const ClassName = "rwInetXfer"

Public DontUseCache As Boolean

Private Enum InfoLevelEnum
    http_QUERY_CONTENT_TYPE = 1
    http_QUERY_CONTENT_LENGTH = 5
    http_QUERY_EXPIRES = 10
    http_QUERY_LAST_MODIFIED = 11
    http_QUERY_PRAGMA = 17
    http_QUERY_VERSION = 18
    http_QUERY_STATUS_CODE = 19
    http_QUERY_STATUS_TEXT = 20
    http_QUERY_RAW_HEADERS = 21
    http_QUERY_RAW_HEADERS_CRLF = 22
    http_QUERY_FORWARDED = 30
    http_QUERY_SERVER = 37
    http_QUERY_USER_AGENT = 39
    http_QUERY_SET_COOKIE = 43
    http_QUERY_REQUEST_METHOD = 45
    http_STATUS_DENIED = 401
    http_STATUS_PROXY_AUTH_REQ = 407
End Enum

Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Const INTERNET_OPEN_TYPE_PROXY = 3

Private Const SCUSERAGENT = "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.1)"
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private Const INTERNET_FLAG_ASYNC = &H10000000  ' this request is asynchronous (where supported)

Private Const INTERNET_FLAG_FROM_CACHE = &H1000000   ' use offline semantics
Private Const INTERNET_FLAG_OFFLINE = INTERNET_FLAG_FROM_CACHE
Private Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000

'   BOOL httpQueryInfo(
'       IN HINTERNET hhttpRequest,
'       IN DWORD dwInfoLevel,
'       IN LPVOID lpvBuffer,
'       IN LPDWORD lpdwBufferLength,
'       IN OUT LPDWORD lpdwIndex,
'   );

'--httpQueryInfo
'::DESC:
'::  Queries for information about an http request.
'::
Private Declare Function httpQueryInfo Lib "wininet.dll" Alias "httpQueryInfoA" _
    (ByVal hhttpRequest As Long, ByVal lInfoLevel As Long, ByRef sBuffer As Any, _
    ByRef lBufferLength As Long, ByRef lIndex As Long) As Integer


Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
    (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, _
    ByVal sProxyBypass As String, ByVal lFlags As Long) As Long


'--InternetOpenUrl
'::DESC:
'::  Open a handle for retrieving a URL
'::EG:
'::  hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
'::  hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_FROM_CACHE, 0)
'::  hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_EXISTING_CONNECT, 0)
'::  hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, 0, 0)
'::
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" _
    (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 Function InternetReadFile Lib "wininet.dll" _
    (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, _
    lNumberOfBytesRead As Long) As Integer

Private Declare Function InternetCloseHandle Lib "wininet.dll" _
    (ByVal hInet As Long) As Integer

Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" _
    (lpdwError As Long, ByVal lpszBuffer As String, ByVal lpdwBufferLength As Long) As Integer




'--OpenUrl(url)
'::DESC:
'::  Retrieve the page specified by "url"
'::  Returns string of page source
'::  On error, returns "error #"
'::     e.g. page not found returns "error 404"
'::
Public Function OpenUrl(ByVal sUrl As String) As String
    #If DEVREL < 1 Then
        On Error GoTo exitfunc
    #End If
    Dim s As String
    Dim sReadBuf As String * 2048   'a data buffer for InternetOpen fcns
    Dim bytesRead As Long
    Dim hInet As Long       'wininet handle
    Dim hUrl As Long        'url request handle
    Dim flagMoreData As Boolean
    Dim ret As String
    ' used for callling httpQueryInfo
    Dim sErrBuf As String * 255
    Dim sErrBufLen As Long
    Dim dwIndex As Long
    ' return codes and err code saves
    Dim lastErr As Long
    Dim bRet As Boolean
    Dim wRet As Integer
    ' http status code
    Dim httpCode As Integer
    ' grab a handle for using wininet
    hInet = InternetOpen(SCUSERAGENT, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    If Err.LastDllError <> 0 Then
        lastErr = Err.LastDllError
        ret = "error (wininet.dll," & lastErr & ")"
        GoTo exitfunc
    End If
    ' retrieve the requested URL
    If DontUseCache Then
        hUrl = InternetOpenUrl(hInet, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
    Else
        hUrl = InternetOpenUrl(hInet, sUrl, vbNullString, 0, 0, 0)
    End If
    If Err.LastDllError <> 0 Then
        lastErr = Err.LastDllError
        ret = "error (wininet.dll," & lastErr & ")"
        GoTo exitfunc
    End If
    ' get query info, this should give us a status code among other things
    sErrBufLen = 255
    bRet = httpQueryInfo(hUrl, http_QUERY_STATUS_CODE, ByVal sErrBuf, sErrBufLen, dwIndex)
    If Err.LastDllError <> 0 Then
        lastErr = Err.LastDllError
        ret = "error (wininet.dll," & lastErr & ")"
        GoTo exitfunc
    End If
    ' sErrBuf should now hopefully contain http status code stuff
    ' if the call failed, no status info was returned (i.e. sErrBuf is empty)
    '   then throw error
    If sErrBufLen = 0 Or Not bRet Then
        ret = "error"
        GoTo exitfunc
    Else
        ' retrieve the http status code
        httpCode = CInt(Left(sErrBuf, sErrBufLen))
        If httpCode >= 300 Then
            ret = "error " & httpCode
            GoTo exitfunc
        End If
    End If
    ' if we made it this far, then we can begin retrieving data
    flagMoreData = True
    Do While flagMoreData
        sReadBuf = vbNullString
        wRet = InternetReadFile(hUrl, sReadBuf, Len(sReadBuf), bytesRead)
        If Err.LastDllError <> 0 Then
            lastErr = Err.LastDllError
            ret = "error (wininet.dll," & lastErr & ")"
            GoTo exitfunc
        End If
        If wRet <> 1 Then
            ret = "error"
            GoTo exitfunc
        End If
        s = s & Left$(sReadBuf, bytesRead)
        If Not CBool(bytesRead) Then flagMoreData = False
    Loop
    ret = s
exitfunc:
    If hUrl <> 0 Then InternetCloseHandle (hUrl)
    If hInet <> 0 Then InternetCloseHandle (hInet)
    OpenUrl = ret
End Function


Private Sub Class_Initialize()
    DontUseCache = False
End Sub

Back to Microsoft: Access Modules (VBA Coding) FAQ Index
Back to Microsoft: Access Modules (VBA Coding) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close