• 5 Subscribers

This question already has an answer here:

I am trying to practice embedding my code. I created a subroutine called JrnlHeader to declare variables I will use in another subroutine. I am currently only concerned with one variable named Header. I would like to know why Header is empty in subroutine PrintToTextFile and how I can fix it to be able to use strings declared in JrnlHeader.

Private Sub JrnlHeader()
    Dim Header As String
    Dim SeqNo As String
    Dim SeqVar As String
    Dim Bu As String
    Dim BuVar As String
    Dim JrnlID As String
    Dim JrnlIDVar As String
    Dim JrnlDate As String
    Dim JrnlDateVar As String
    Dim Descr As String
    Dim DescrVar As String
    Dim Ledger As String
    Dim LedgerVar As String
    Dim Source As String
    Dim SourceVar As String
    Dim CurEff As String
    Dim Reverse As String
    Dim AutoLn As String
    Dim AdjEnt As String

    Header = "<JRNL_HDR_IMP>"
    SeqNo = "<SEQNO>" & SeqVar & "</SEQNO>"
    Bu = "<BUSINESS_UNIT>" & BuVar & "</BUSINESS_UNIT>"
    JrnlID = "<JOURNAL_ID>" & JrnlIDVar & "</JOURNAL_ID>"
    JrnlDate = "<JOURNAL_DATE>" & JrnlDateVar & "</JOURNAL_DATE>"
    Descr = "<DESCR254>" & DescrVar & "</DESCR254>"
    Ledger = "<LEDGER_GROUP>" & LedgerVar & "</LEDGER_GROUP>"
    Source = "<SOURCE>" & SourceVar & "</SOURCE>"
    CurEff = "<CUR_EFFDT>" & JrnlDateVar & "</CUR_EFFDT>"
    Reverse = "<REVERSAL_CD>N</REVERSAL_CD>"
End Sub

Sub PrintToTextFile()
    Dim FileNum As Integer


    FileNum = FreeFile ' next free filenumber

    'Open "C:\Temp\TEXTFILE.TXT" For Output As #FileNum ' creates the new file
    Open "C:\temp\TEXTFILE.TXT" For Append As #FileNum
    Print #FileNum, Header
    Close #FileNum ' close the file
End Sub

2 answers in total

YowE3K Posted at 2017-01-11 20:34:46Z

You have defined Header to be a local variable in JrnlHeader. This means its scope does not extend to other subroutines/functions.

You can define the scope of the variable to be "module" level, by placing the Dim Header As String statement prior to the first subroutine/function within the code module. Then its value will be available when execution resumes in PrintToTextFile.

Alternatively, you could change your code to pass the variable as a parameter between the two functions:

Sub PrintToTextFile()

    Dim Header As String
    JnrlHeader Header
    Print #FileNum, Header
End Sub

Sub JrnlHeader(Header As String)
    '...  (but don't include any declaration of Header!)
    Header = "<JRNL_HDR_IMP>"
End Sub

But, judging by how many variables are being set up in JrnlHeader, I think you will want to go with using a module-level scoped variable approach.

Dave Clough Posted at 2017-01-11 20:27:23Z

The two subroutines have different scope. Variables defined JrnlHeader are not available in PrintToTextFile. If you want header to be available in PrintToTextFile the change it to PrintToTextFile(header as string) and call PrintToTextFile(header) from JrnlHeader.

Answer this questsion