Making a subroutine Global?

kwvh
3StarLounger
Posts: 308
Joined: 24 Feb 2010, 13:41

Making a subroutine Global?

Post by kwvh »

I have some code, borrowed from a prominent brilliant user here, that I would like to make available throughout my project. In other words, I would like for any report designed to be able to use the following, without having to add it to every report:

Code: Select all

Sub DrawLines(R As Report, ParamArray xPos())
    ' Number of twips in one inch.
    Const OneInch = 1440
    ' Maximum height of a section in inches.
    Const MaxHeight = 22
    ' Loop index
    Dim i As Integer
    ' Draw vertical line at xPos(i) inches from left margin. Covers entire section.
    For i = LBound(xPos) To UBound(xPos)
        R.Line (xPos(i) * OneInch, 0)-(xPos(i) * OneInch, MaxHeight * OneInch)
    Next i
End Sub
How can I change this sub to a global function or constant?

Thanks in advance for your ideas.

Ken

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Making a subroutine Global?

Post by HansV »

Place the code in a standard module, i.e. the kind of module that you create by selecting Insert | Module in the Visual Basic Editor. You'll be able to call DrawLines from every report, for example like this in the On Print event of the Detail section:

Code: Select all

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
  Call DrawLines(Me, 0, 2, 3.5)
End Sub
This will draw vertical lines at 0 inches, 2 inches and 3.5 inches from the left margin.
Best wishes,
Hans

kwvh
3StarLounger
Posts: 308
Joined: 24 Feb 2010, 13:41

Re: Making a subroutine Global?

Post by kwvh »

Thanks Hans,

I don't have something correct. Below is the module and the code calling the function.

Code: Select all

' Module to drawlines
Option Compare Database
Option Explicit

Function DrawLines(R As Report, ParamArray xPos())
    ' Number of twips in one inch.
    Const OneInch = 1440
    ' Maximum height of a section in inches.
    Const MaxHeight = 22
    ' Loop index
    Dim i As Integer
    ' Draw vertical line at xPos(i) inches from left margin. Covers entire section.
    For i = LBound(xPos) To UBound(xPos)
        R.Line (xPos(i) * OneInch, 0)-(xPos(i) * OneInch, MaxHeight * OneInch)
    Next i
End Function

' Function to call the module.
Function fDrawLines4PerfReports()
    ' Draw vertical lines at X" from left margin.
    Call DrawLines(Me, 2.5938, 3.0625, 3.7813, 4.5521, 5.1771, 5.6458, 6.3854, 7.1667, 7.7604, 8.6458, 9.3958, 10.0208, 10.5104)

End Function
What did I miss?

Thanks!

Ken

kwvh
3StarLounger
Posts: 308
Joined: 24 Feb 2010, 13:41

Re: Making a subroutine Global?

Post by kwvh »

Oops, nevermind. I had named the module DrawLines instead of modDrawlines. duh!