A Variable Scope

Key points:

A private procedure can be called from any procedure in the same module.
The default scope for a sub procedure is “Public” unless you specifically designate it as Private.

Store all your procedures and functions you plan to use in your application in “Modules”, which you can call from your other database objects.

Here is the video:

Here is a handy chart for you to use when determining VBA variable scope:

Keyword Result
Dim The variable is only available inside the procedure in which it is declared.
Private The variable is available to all procedures in the module in which it is declared, but not to procedures in other modules in the application.
Public The variable is available to all procedures in all modules in the application.
Public Declare Function ShellAbout Lib "shell32.dll" Alias "ShellAboutA" (ByVal hwnd As Long, _
    ByVal szApp As String, ByVal szOtherStuff As String, ByVal hIcon As Long) As Long

Public Declare Function GetActiveWindow Lib "user32" () As Long
Public m_blnCloseAccess As Boolean

Public m_strControl As String
Public m_strForm As String
Public m_strSearchText As String

Public Sub About()
    Dim hwnd As Long
    Dim windowname As String
    Dim nl$
    Dim x
    
    nl$ = Chr$(10) + Chr$(13)
    hwnd = GetActiveWindow()
    
    x = ShellAbout(hwnd, "LoeblCom Services Database", nl$ + Chr$(169) + "LoeblCom Services 1999-2004, erik@loebl.com" + nl$, 0)
    
End Sub

Public Function IsLoaded(ThisForm As String) As Boolean
    'is the form loaded or not
    
    If SysCmd(acSysCmdGetObjectState, acForm, ThisForm) <> 0 Then
        IsLoaded = True
    Else
        IsLoaded = False 'the form is closed
    End If
    
End Function
Public Sub OpenMainSWB()
    'DoCmd.OpenForm "MainSWB"
    If IsLoaded("MainSWB") Then
        Forms("MainSWB").Visible = True
    End If
End Sub
Public Sub CloseMainSWB()
    'DoCmd.Close acForm, "MainSWB"
    If IsLoaded("MainSWB") Then
        Forms("MainSWB").Visible = False
    End If
End Sub
Public Sub OpenReportSWB()
    'DoCmd.OpenForm "ReportsSWB"
    If IsLoaded("ReportsSWB") Then
        Forms("ReportsSWB").Visible = True
    End If
End Sub
Public Sub CloseReportSWB()
    'DoCmd.Close acForm, "ReportsSWB"
    If IsLoaded("ReportsSWB") Then
        Forms("ReportsSWB").Visible = False
    End If
End Sub


Public Function BreakString(SomeText, Delimiter, before_comma) As Variant
    'Send an array back as a result to another variant in the calling proc
    Dim intStart As Integer
    Dim intNextDelim As Integer
    Dim strSplitText() As String
    Dim intCounter As Integer
    
    If Right(SomeText, 1) <> Delimiter Then 'make sure the last column gets counted
        SomeText = SomeText & Delimiter
    End If
    intStart = 1 'the starting position always changes, so it is a variable
    intNextDelim = InStr(1, SomeText, Delimiter)
    
    
    Do While intNextDelim >= 1 'stop when 0 (there are no more delimiters)
        intCounter = intCounter + 1
        ReDim Preserve strSplitText(intCounter)
        strSplitText(intCounter) = Mid(SomeText, intStart, intNextDelim - intStart) 'store value in an array
        
        If intCounter = before_comma Then
            BreakString = strSplitText(intCounter)
            Exit Function
        End If

        intStart = intNextDelim + 1 'move the start position up one
        intNextDelim = InStr(intStart, SomeText, Delimiter) 'find the next delimiter
    Loop
    BreakString = strSplitText 'return the array to the variant
End Function

<< How To Make A VBA Function | VBA Debug Part 1 >>




By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How To Parse A Flat File In Excel VBA

In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]

How can I interact with other Office applications (Excel) using VBA in Access?

Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]

How To Generate A XML File With Access VBA

XML is used to structure your data. You can send a CSV file to another person, but using XML, you can structure your data in a more elegant format, and get very specific about your fields, and what data is contained within your tags (your field names). This is different than a basic CSV file […]

Here Is A Customized Msgbox VBA Example

Here is an example of a customized VBA Msgbox. We giving some richtext and customizable flair to the rather ordinary message box. The following code provided with the code after the screenshot, is going to provide you with the ability to really make the ordinary message box shine! Sub MsgboxVBAExamples() Dialog.Box “VBAHowTo.com is your source […]

Previous Post

VBA Debug Part 1

Next Post

How To Make A VBA Function

Leave a Reply

Your email address will not be published. Required fields are marked *