Chris
Chris

Reputation: 219

Visual Basic - How to use a variable from one function in another

I have checked Google, and the suggested answers here, but have had no luck unfortunately.

The last thing I need to do is have an email read the rateNbr variable into the email body, but it just comes up empty.

I tried to make Public Function FuncRateCheckFile read as Public Function FuncRateCheckFile(ByVal rateNbr As String), to try and enable it to be called outside the function, but this then breaks the function when it is called elsewhere. :(

Here is the code, with comments as to where I am referring:

Public Function FuncRateCheckFile()

    Dim blnContinue As Boolean
    Dim strLine As String
    Dim strSearchFor, strSearchWrd, LineCount, objFSO, objTextFile, arrLines
    Dim dteNow As Date
    Dim newDate As String
    '//==============================================================================================
    '//  DECLARED
    Dim rateNbr As String
    '//==============================================================================================

    FuncRateCheckFile = False
    blnContinue = True


    If blnContinue Then

        Const ForReading = 1
            'Get todays date and reformat it
            dteNow = DateValue(Now)
            newDate = Format(dteNow, "dd/MM/yy")
            strSearchWrd = newDate
            'Read the whole file
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            Set objTextFile = objFSO.OpenTextFile(m_RateCheckFile, ForReading)

            LineCount = 0
            Do Until objTextFile.AtEndOfStream

                strLine = objTextFile.ReadLine()

                If InStr(strLine, strSearchWrd) <> 0 Then

                   arrLines = Split(strLine, vbCrLf)
                   LineCount = LineCount + 1

                End If
            Loop

                'Log a message to state how many lines have todays day, and if there are none, log an error
                If LineCount <> 0 Then
                    '//==============================================================================================
                    '//  "rateNbr" IS WHAT I AM TRYING TO GET TO PUT IN THE EMAIL
                    LogMessage "Rate file date is correct"
                    rateNbr = "Number of rates for " & newDate & " in the file recieved on " & newDate & " is " & LineCount
                    LogMessage rateNbr
                    EmailAdvice2
                    objTextFile.Close
                    '//==============================================================================================

                Else

                    blnContinue = False
                    LogError "Failed to retrieve Current Rate date, please check rate file.."
                    EmailAdvice
                    objTextFile.Close

                End If

    End If

    FuncRateCheckFile = blnContinue

    LogMessage "Completed Check Rate file"

End Function


Private Function EmailAdvice2()


Dim strSMTPFrom As String
Dim strSMTPTo As String
Dim strSMTPRelay As String
Dim strTextBody As String
Dim strSubject As String
Dim oMessage As Object
'//==============================================================================================
'//  DECLARED AGAIN
Dim rateNbr As String
'//==============================================================================================

Set oMessage = CreateObject("CDO.Message")
strSMTPFrom = "[email protected]"
strSMTPTo = "[email protected]"
strSMTPRelay = "smtp.relay.com"
'//==============================================================================================
'//  THIS MAKES THE TEXT BODY BLANK, BUT THE EMAIL STILL SENDS
strTextBody = rateNbr
'//==============================================================================================
strSubject = "Todays rates"
'strAttachment = "full UNC path of file"

oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPRelay
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
oMessage.Configuration.Fields.Update

oMessage.Subject = strSubject
oMessage.From = strSMTPFrom
oMessage.To = strSMTPTo
oMessage.textbody = strTextBody
'oMessage.AddAttachment strAttachment


oMessage.Send

End Function

I am positive that it is blank because I have declared rateNbr under EmailAdvice2() and then not given it anything to fill the variable with. But I don't know how to make it call the variable under FuncRateCheckFile().

Thanks to all for any assistance.

Upvotes: 0

Views: 3869

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39122

As Plutonix stated, this is a scope issue.

Move the declaration of your 'rateNbr' variable out to class level, and remove the local declarations inside your functions:

Dim rateNbr As String ' <-- out at class level it will be accessible from both functions

Public Function FuncRateCheckFile()
    ...
    ' REMOVE both the decalarations of "rateNbr" that are INSIDE your functions
    ...
End Function 

Private Function EmailAdvice2()
    ...
    ' REMOVE both the decalarations of "rateNbr" that are INSIDE your functions
    ...
End Function 

Upvotes: 1

Related Questions