Stock Server Anywhere?

Posted on 1999-07-22
Medium Priority
Last Modified: 2010-10-05

Is there a free stock quoting service server out there which I could interface with vb5/6. And if so, can you please provide a source code example of how to retrieve stock quotes from this server?

Question by:BabyFace
1 Comment

Accepted Solution

sharmon earned 800 total points
ID: 1526774
'Here ya go, this works fine and uses Yahoo's stock server
'You need to add the following to your form...
'Microsoft Inet Control
'2 Text boxes one named txtSymbol and the other named
'   txtQuotes
'1 Command button named cmdGetQuotes
'Paste the following code into the form and you're ready
'to go.  Make sure you are connected to the internet and
'enter your Stock Symbol into the first text box, click
'your command button and it should return no problem.
'If you don't like the format of the stock information
'just reformat it to fit your needs.  There is no error
'checking in this code as I don't know what you want to
'use it for, but it will definetly get you what you are
'looking for.

'----------BEGIN CODE HERE-----------------------------
' Get a row from the response string.
Private Function GetRow(response As String) As String
Dim pos As Integer
Dim symbol As String
Dim last_time As String
Dim last_price As String
Dim change_amount As String
Dim change_percent As String

    ' Find the "<tr" starting the row.
    pos = InStr(response, "<tr")
    If pos = 0 Then
        response = ""
        GetRow = ""
        Exit Function
    End If
    response = Mid$(response, pos)

    ' Find the items in this row.
    symbol = GetRowItem(response)
    last_time = GetRowItem(response)
    If InStr(last_time, "No such ticker symbol.") > 0 Then
        GetRow = "No such ticker symbol."
        Exit Function
    End If

    last_price = GetRowItem(response)
    change_amount = GetRowItem(response)
    change_percent = GetRowItem(response)

    GetRow = symbol & ": " & _
        last_time & ", " & _
        last_price & ", " & _
        change_amount & ", " & _
End Function

' Get the next table item from the table.
Private Function GetRowItem(response As String) As String
Dim start_pos As Integer
Dim end_pos As Integer
Dim pos As Integer
Dim count As Integer
Dim ch As String
Dim txt As String

    ' Find the "<td" and "</td" that bracket
    ' the item.
    start_pos = InStr(response, "<td")
    end_pos = InStr(start_pos, response, "</td")

    ' Save characters between these where the
    ' outstanding brackets match.
    count = 1
    For pos = start_pos + 1 To end_pos
        ch = Mid$(response, pos, 1)
        If ch = "<" Then
            count = count + 1
        ElseIf ch = ">" Then
            count = count - 1
            If count = 0 Then txt = txt & ch
        End If
    Next pos

    GetRowItem = txt
    response = Mid$(response, end_pos)
End Function

Private Function ParseResponse(ByVal response As String) As String
Dim start_pos As Integer
Dim end_pos As Integer
Dim i As Integer
Dim quotes As String
Dim new_row As String

    ' Find the table that contains the
    ' interesting information.
    start_pos = InStr(response, "Last Trade")
    If start_pos = 0 Then
        ParseResponse = "Error parsing response."
        Exit Function
    End If
    ' See where the table ends.
    end_pos = InStr(start_pos, response, "</table>")
    response = Mid$(response, start_pos, end_pos - start_pos + Len("</table>"))

    ' Parse the rows from the table.
        new_row = GetRow(response)
        If Len(new_row) = 0 Then Exit Do
        quotes = quotes & new_row & vbCrLf

    ParseResponse = quotes
End Function

Private Sub cmdGetQuotes_Click()
Dim not_first_symbol As Boolean
Dim symbol As String
Dim query_url As String
Dim i As Integer
Dim response As Variant

    MousePointer = vbHourglass
    txtQuotes.Text = ""
        query_url = "http://quote.yahoo.com/q?s="
            symbol = LCase$(Trim$(txtSymbol.Text))
            If Len(symbol) > 0 Then
                If not_first_symbol Then _
                    query_url = query_url & "%2C"
                query_url = query_url & symbol
                not_first_symbol = True
            End If
        query_url = query_url & "&d=v1"
        ' Open the URL.
        response = inetQuotes.OpenURL(query_url)

    ' Parse the response.
    txtQuotes.Text = ParseResponse(CStr(response))

    MousePointer = vbDefault
End Sub

' Cancel any pending commands.
Private Sub Form_Unload(Cancel As Integer)
End Sub


Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question