+ Reply to Thread
Results 1 to 9 of 9

Passing a private variable to another module

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    Passing a private variable to another module

    I have some code in which I need to pass the value of "j" from one module to another. "j" is declared in Sheet1 (Data) under the Microsoft Excel Objects.

    Option Explicit
    
    Private Sub CommandButton1_Click()
    
        Dim i As Long
        Dim j As Integer
        Dim lastrow As Integer
    I need to pass the value of j to a class module which is coded as follows:
    Option Explicit
    
    Private pWinHttpRequest As WinHttp.WinHttpRequest
    
    Friend Function GetHistoricalData(Symbol As String, _
        Optional FromDate As Date = #12:00:00 AM#, _
        Optional ToDate As Date = #12:00:00 AM#, _
        Optional Interval As String = "Daily") As ADODB.Recordset
        
        Dim URL As String, ResponseText As String
        Dim pRecordSet As ADODB.Recordset
        Dim DateString As String, IntervalString As String
        Dim RTS() As String, RTFI
        Dim x As Long
    .

    I have tried a number of different options such declaring "j" has a Public Variable and trying pass "j" ByVal but can't get any of these options to work. Not sure what I am doing wrong. Thanks.
    Last edited by maacmaac; 11-26-2008 at 02:27 PM.

  2. #2
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    It needs to be declared as public on a General Module.

    HTH

    Jason

  3. #3
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176
    I declared "j" as a public variable but not sure if my declare statement is correct or if I have declared it in the correct part of the code. I have included all the code and were each is stored and highlighted were I am using my variable. Thanks.

    This part of the code is stored under "Microsoft Excel Objects" (This is were I get a compile error).
    Option Explicit
    
    Private pWinHttpRequest As WinHttp.WinHttpRequest
    
    Friend Function GetHistoricalData(Symbol As String, _
        Optional FromDate As Date = #12:00:00 AM#, _
        Optional ToDate As Date = #12:00:00 AM#, _
        Optional Interval As String = "Daily") As ADODB.Recordset
        
        Dim URL As String, ResponseText As String
        Dim pRecordSet As ADODB.Recordset
        Dim DateString As String, IntervalString As String
        Dim RTS() As String, RTFI
        Dim x As Long
    
        If FromDate <> #12:00:00 AM# Or ToDate <> #12:00:00 AM# Then
            If FromDate = 0 And ToDate > 0 Then
                FromDate = #1/1/1900#
            ElseIf FromDate > 0 And ToDate = 0 Then
                ToDate = Date
            End If
            DateString = "&a=" & Format(Month(FromDate) - 1, "00") & "&b=" _
                            & Format(FromDate, "DD") & "&c=" & Format(FromDate, "YYYY") & _
                         "&d=" & Format(Month(ToDate) - 1, "00") & "&e=" _
                            & Format(ToDate, "DD") & "&f=" & Format(ToDate, "YYYY")
        End If
    
        
        URL = "http://ichart.finance.yahoo.com/table.csv?s=" & Symbol & DateString & IntervalString
        
        pWinHttpRequest.Open "GET", URL, False
        pWinHttpRequest.Send
         
        ResponseText = pWinHttpRequest.ResponseText
        If InStr(ResponseText, "<title>Yahoo! - 404 Not Found</title>") Then
                Err.Raise 10002, "HistoricalStockDataFromYahoo.GetHistoricalData", _
                "Line " & j & ": Invalid Search Parameters."
        End If
        
        Set pRecordSet = New ADODB.Recordset
        
        pRecordSet.Fields.Append "Date", adDBDate
        pRecordSet.Fields.Append "Close", adCurrency
        pRecordSet.Open
        
        RTS = Split(ResponseText, Chr(10))
        
        For x = LBound(RTS) + 1 To UBound(RTS)
            If RTS(x) <> "" Then
                RTFI = Split(RTS(x), ",")
                pRecordSet.AddNew Array("Date", "Close"), Array(RTFI(0), RTFI(4))
                pRecordSet.Update
            End If
        Next x
    
        pRecordSet.MoveFirst
        Set GetHistoricalData = pRecordSet
    End Function
    
    Private Sub Class_Initialize()
        On Error Resume Next
        Set pWinHttpRequest = New WinHttpRequest
        If pWinHttpRequest Is Nothing Then
            Err.Raise 10000, "HistoricalStockDataFromYahoo.Class_Initialize", _
            "Could not create WinHttp.WinHttpRequest object..."
        End If
    End Sub
    This part of the code is stored under "Class Module"
    Option Explicit
    Public j As Integer
    
    
    Private Sub CommandButton1_Click()
    
        Application.ScreenUpdating = False
        
        Dim HSDFY As HistoricalStockDataFromYahoo
        Dim rs As ADODB.Recordset
        Dim i As Long
        Dim lastrow As Integer
    
        On Error GoTo Err_CommandButton1_Click
        
        Range("C2:D" & Rows.Count).ClearContents
    
        i = 2
    
        lastrow = Range("A" & Rows.Count).End(xlUp).Row
    
        For j = 2 To lastrow
            Set HSDFY = New HistoricalStockDataFromYahoo
            Set rs = HSDFY.GetHistoricalData(Cells(j, 1).Value, Cells(j, 2).Value, Cells(j, 2).Value)
            rs.MoveFirst
            Cells(j, 3).CopyFromRecordset rs
            i = i + 1
        Next j
    
    Exit Sub
    
    Err_CommandButton1_Click:
        Select Case Err.Number
            Case 10000
                MsgBox Err.Description
                j = j + 1
                i = i + 1
            Case 10001
                'invalid interval
                MsgBox Err.Description
                j = j + 1
                i = i + 1
            Case 10002
                'query failed
                MsgBox Err.Description
                j = j + 1
                i = i + 1
           Case Else
                MsgBox "Invalid Search Parameters or other error.  No data was returned."
                j = j + 1
                i = i + 1
        End Select
        Resume
    
        Application.ScreenUpdating = True
        
    End Sub

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    I don't understand. Put them in a Module.

    You may need to delcare the Public in both the Module it is used in and the other.

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481
    Are you sure about where each code sample is?

    Are you saying your class exposes click events?


    I thought I had posted a reply, maybe I forgot to submit.

    You can declare a public variable in the class or a private variable with get/let properties.

    In your main code you will need to assign the value to the property before executing the function that uses it other wise the value will be zero
    Cheers
    Andy
    www.andypope.info

  6. #6
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176
    Andy -

    Sorry, you lost me in your comments. I'm not sure what the "get/let" properties are nor do I know where to assign the value. The whole assigning variables and letting other procedures use these variables is somewhat new to me. Realistically, I would really like to declare "j" as a private variable and then pass the value to the function that needs it. I have attached a copy of the spreadsheet so you can see where each code is stored. HTH. Thanks again for your comments.
    Attached Files Attached Files

  7. #7
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    As I said, if you will copy the Public to the top of both the sheet and the Module, it will work. Or, Dim j in the module where it was not defined as Public.

    While you have few rows and J as integer will work, it is generally a good practice to dim variables that reference row numbers as Long.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1