Christian Specht avatar Christian Specht committed 6a7a4f4

change variable names to prevent VBA from automatically changing the case
(see readme)

Comments (0)

Files changed (3)

 ## Links
 
 - [Download vba-helpers.bas directly](https://bitbucket.org/christianspecht/vba-helpers/raw/tip/vba-helpers.bas) (see "Setup" below)
-- [Found a bug?](https://bitbucket.org/christianspecht/vba-helpers/issues/new)
+- [Report a bug](https://bitbucket.org/christianspecht/vba-helpers/issues/new)
 - [Main project page on Bitbucket](https://bitbucket.org/christianspecht/vba-helpers)
 
 ---
 
 ##### Coding Guidelines
 
-All variable names must be in lower case, to avoid [VBA changing the case automatically when mixing upper/lower case](http://stackoverflow.com/q/4852735) - this is **very** annoying when using source control.
+Unfortunately, [VBA globally changes the case of variable names when you mix upper/lower case](http://stackoverflow.com/q/4852735).  
+This is **very** annoying when using source control.  
+It's even worse when VBA Helpers is imported into another VBA project, and the case of *the variables in this project* is changed because some of them happened to have the same names like some of the VBA Helpers variables.
+
+To avoid this, all variable names in VBA Helpers must adhere to the following guidelines:
+
+- lower case only
+- suffixed by `_vbah` (for "VBA Helpers"), e.g. `foo_vbah`
+
 
 ##### Committing changes
 

vba-helpers-dev.bas

 Option Compare Database
 Option Explicit
 
-Const vbahelpersdevfilename As String = "vba-helpers-dev.bas"
-Const vbahelpersdevmodulename As String = "VBAHelpersDev"
-Const vbahelperstestfilename As String = "vba-helpers-tests.bas"
-Const vbahelperstestmodulename As String = "VBAHelpersTests"
+Const vbahelpersdevfilename_vbah As String = "vba-helpers-dev.bas"
+Const vbahelpersdevmodulename_vbah As String = "VBAHelpersDev"
+Const vbahelperstestfilename_vbah As String = "vba-helpers-tests.bas"
+Const vbahelperstestmodulename_vbah As String = "VBAHelpersTests"
 
 Public Sub VBAHelpers_Export()
     'Exports all modules to the current directory (for source control) and increases the version number in the VBA Helpers module.
 
-    Const versionstring As String = "'# Version "
-    Dim exportfile As String
+    Const versionstring_vbah As String = "'# Version "
+    Dim exportfile_vbah As String
     
     'export VBA Helpers
-    exportfile = Path_Combine(Path_GetCurrentDirectory, vbahelpersfilename)
-    Application.SaveAsText acModule, vbahelpersmodulename, exportfile
+    exportfile_vbah = Path_Combine(Path_GetCurrentDirectory, vbahelpersfilename_vbah)
+    Application.SaveAsText acModule, vbahelpersmodulename_vbah, exportfile_vbah
 
     'set version number
-    Dim lines1() As String
-    Dim lines2() As String
-    Dim i As Long
+    Dim lines1_vbah() As String
+    Dim lines2_vbah() As String
+    Dim i_vbah As Long
 
-    lines1 = File_ReadAllLines(exportfile)
-    ReDim lines2(UBound(lines1))
-    For i = 0 To UBound(lines1)
-        If String_StartsWith(lines1(i), versionstring) Then
-            lines2(i) = versionstring & format(Now, "yyyymmdd.hhmmss")
+    lines1_vbah = File_ReadAllLines(exportfile_vbah)
+    ReDim lines2_vbah(UBound(lines1_vbah))
+    For i_vbah = 0 To UBound(lines1_vbah)
+        If String_StartsWith(lines1_vbah(i_vbah), versionstring_vbah) Then
+            lines2_vbah(i_vbah) = versionstring_vbah & format(Now, "yyyymmdd.hhmmss")
         Else
-            lines2(i) = lines1(i)
+            lines2_vbah(i_vbah) = lines1_vbah(i_vbah)
         End If
     Next
 
-    File_WriteAllLines exportfile, lines2
+    File_WriteAllLines exportfile_vbah, lines2_vbah
 
     'export tests
-    exportfile = Path_Combine(Path_GetCurrentDirectory, vbahelperstestfilename)
-    Application.SaveAsText acModule, vbahelperstestmodulename, exportfile
+    exportfile_vbah = Path_Combine(Path_GetCurrentDirectory, vbahelperstestfilename_vbah)
+    Application.SaveAsText acModule, vbahelperstestmodulename_vbah, exportfile_vbah
 
     'export dev functions
-    exportfile = Path_Combine(Path_GetCurrentDirectory, vbahelpersdevfilename)
-    Application.SaveAsText acModule, vbahelpersdevmodulename, exportfile
+    exportfile_vbah = Path_Combine(Path_GetCurrentDirectory, vbahelpersdevfilename_vbah)
+    Application.SaveAsText acModule, vbahelpersdevmodulename_vbah, exportfile_vbah
 
 End Sub
 
 Public Sub VBAHelpers_Import()
     'Imports all VBA Helpers modules from the current directory.
 
-    Dim exportfile As String
-    Dim message As String
+    Dim exportfile_vbah As String
+    Dim message_vbah As String
 
     'import tests
-    exportfile = Path_Combine(Path_GetCurrentDirectory, vbahelperstestfilename)
-    If Dir(exportfile) = "" Then
-        message = String_Format("Couldn't find test class:{0}{1}", vbCrLf, exportfile)
-        MsgBox message, vbCritical
+    exportfile_vbah = Path_Combine(Path_GetCurrentDirectory, vbahelperstestfilename_vbah)
+    If Dir(exportfile_vbah) = "" Then
+        message_vbah = String_Format("Couldn't find test class:{0}{1}", vbCrLf, exportfile_vbah)
+        MsgBox message_vbah, vbCritical
         Exit Sub
     End If
     
-    Application.LoadFromText acModule, vbahelperstestmodulename, exportfile
+    Application.LoadFromText acModule, vbahelperstestmodulename_vbah, exportfile_vbah
 
 
     'import dev functions
-    exportfile = Path_Combine(Path_GetCurrentDirectory, vbahelpersdevfilename)
-    If Dir(exportfile) = "" Then
-        message = String_Format("Couldn't find dev functions:{0}{1}", vbCrLf, exportfile)
-        MsgBox message, vbCritical
+    exportfile_vbah = Path_Combine(Path_GetCurrentDirectory, vbahelpersdevfilename_vbah)
+    If Dir(exportfile_vbah) = "" Then
+        message_vbah = String_Format("Couldn't find dev functions:{0}{1}", vbCrLf, exportfile_vbah)
+        MsgBox message_vbah, vbCritical
         Exit Sub
     End If
     
-    Application.LoadFromText acModule, vbahelpersdevmodulename, exportfile
+    Application.LoadFromText acModule, vbahelpersdevmodulename_vbah, exportfile_vbah
     
 
     'import VBA Helpers (use update function from actual VBA Helpers module)
 '# VBA Helpers
 '# A collection of useful VBA functions
 '#
-'# Version 20120831.210207
+'# Version 20121109.172711
 '# (the version number is just the current date/time)
 '#
 '# Copyright (c) 2012 Christian Specht
 Option Compare Database
 Option Explicit
 
-Public Const vbahelpersfilename As String = "vba-helpers.bas"
-Public Const vbahelpersmodulename As String = "VBAHelpers"
+Public Const vbahelpersfilename_vbah As String = "vba-helpers.bas"
+Public Const vbahelpersmodulename_vbah As String = "VBAHelpers"
 
-Const directoryseparatorchar As String = "\"
-Const environmentnewline As String = vbCrLf
+Const directoryseparatorchar_vbah As String = "\"
+Const environmentnewline_vbah As String = vbCrLf
 
 '##########################################################################################################################################
 
-Public Function File_ReadAllLines(ByVal path As String) As String()
+Public Function File_ReadAllLines(ByVal path_vbah As String) As String()
     'Reads a text file and returns a string array, each array item containing a line from the file.
     
-    Dim i As Integer
-    Dim tmp As String
-    Dim filelines As Long
-    Dim arraylines As Long
-    Dim retval() As String
+    Dim i_vbah As Integer
+    Dim tmp_vbah As String
+    Dim filelines_vbah As Long
+    Dim arraylines_vbah As Long
+    Dim retval_vbah() As String
     
-    i = FreeFile
-    Close #i
+    i_vbah = FreeFile
+    Close #i_vbah
     
-    Open path For Input As #i
+    Open path_vbah For Input As #i_vbah
     
-    filelines = 0
-    arraylines = 0
+    filelines_vbah = 0
+    arraylines_vbah = 0
     
-    Do While Not EOF(i)
+    Do While Not EOF(i_vbah)
         
-        If arraylines <= filelines Then
-            arraylines = arraylines + 100
-            ReDim Preserve retval(arraylines - 1)
+        If arraylines_vbah <= filelines_vbah Then
+            arraylines_vbah = arraylines_vbah + 100
+            ReDim Preserve retval_vbah(arraylines_vbah - 1)
         End If
         
-        Line Input #i, tmp
-        retval(filelines) = tmp
+        Line Input #i_vbah, tmp_vbah
+        retval_vbah(filelines_vbah) = tmp_vbah
         
-        filelines = filelines + 1
+        filelines_vbah = filelines_vbah + 1
         
     Loop
     
-    ReDim Preserve retval(filelines - 1)
+    ReDim Preserve retval_vbah(filelines_vbah - 1)
     
-    Close #i
+    Close #i_vbah
     
-    File_ReadAllLines = retval
+    File_ReadAllLines = retval_vbah
     
 End Function
 
-Public Function File_ReadAllText(ByVal path As String) As String
+Public Function File_ReadAllText(ByVal path_vbah As String) As String
     'Reads a text file and returns the content in a string variable.
     
-    Dim contents() As String
+    Dim contents_vbah() As String
     
-    contents = File_ReadAllLines(path)
+    contents_vbah = File_ReadAllLines(path_vbah)
     
-    If UBound(contents) > 0 Then
-        File_ReadAllText = (Join(contents, environmentnewline))
+    If UBound(contents_vbah) > 0 Then
+        File_ReadAllText = (Join(contents_vbah, environmentnewline_vbah))
     End If
 
 End Function
 
-Public Sub File_WriteAllLines(ByVal path As String, contents() As String)
+Public Sub File_WriteAllLines(ByVal path_vbah As String, contents_vbah() As String)
     'Writes the content of a string array into a text file, each array item into a new line.
 
-    File_WriteAllText path, Join(contents, environmentnewline)
+    File_WriteAllText path_vbah, Join(contents_vbah, environmentnewline_vbah)
 
 End Sub
 
-Public Sub File_WriteAllText(ByVal path As String, ByVal contents As String)
+Public Sub File_WriteAllText(ByVal path_vbah As String, ByVal contents_vbah As String)
     'Writes the content of a string variable into a text file.
     
-    Dim i As Integer
+    Dim i_vbah As Integer
     
-    i = FreeFile
+    i_vbah = FreeFile
     
-    Close #i
+    Close #i_vbah
     
-    Open path For Output As #i
-    Print #i, contents
-    Close #i
+    Open path_vbah For Output As #i_vbah
+    Print #i_vbah, contents_vbah
+    Close #i_vbah
 
 End Sub
 
-Public Function Path_Combine(ParamArray paths() As Variant) As String
+Public Function Path_Combine(ParamArray paths_vbah() As Variant) As String
     'Combines several strings into a path and takes care of directory separators, i.e. `path_combine("c:\","\foo","bar")` will return `c:\foo\bar`
     
-    Dim path As Variant
-    Dim retval As String
+    Dim path_vbah As Variant
+    Dim retval_vbah As String
     
-    For Each path In paths
+    For Each path_vbah In paths_vbah
     
-        If String_StartsWith(path, directoryseparatorchar) Then
-            path = Mid(path, Len(directoryseparatorchar) + 1)
+        If String_StartsWith(path_vbah, directoryseparatorchar_vbah) Then
+            path_vbah = Mid(path_vbah, Len(directoryseparatorchar_vbah) + 1)
         End If
     
-        If String_EndsWith(path, directoryseparatorchar) Then
-            path = Left(path, Len(path) - Len(directoryseparatorchar))
+        If String_EndsWith(path_vbah, directoryseparatorchar_vbah) Then
+            path_vbah = Left(path_vbah, Len(path_vbah) - Len(directoryseparatorchar_vbah))
         End If
     
-        retval = retval & path & directoryseparatorchar
+        retval_vbah = retval_vbah & path_vbah & directoryseparatorchar_vbah
     
     Next
     
-    If String_EndsWith(retval, directoryseparatorchar) Then
-        retval = Left(retval, Len(retval) - Len(directoryseparatorchar))
+    If String_EndsWith(retval_vbah, directoryseparatorchar_vbah) Then
+        retval_vbah = Left(retval_vbah, Len(retval_vbah) - Len(directoryseparatorchar_vbah))
     End If
     
-    Path_Combine = retval
+    Path_Combine = retval_vbah
 
 End Function
 
     
 End Function
 
-Public Function Path_GetDirectoryName(ByVal path As String) As String
+Public Function Path_GetDirectoryName(ByVal path_vbah As String) As String
     'Receives a complete path, returns only the directory.
     
-    Dim i As Long
+    Dim i_vbah As Long
     
-    If Len(path) > 3 Then
+    If Len(path_vbah) > 3 Then
     
-        i = InStrRev(path, directoryseparatorchar)
+        i_vbah = InStrRev(path_vbah, directoryseparatorchar_vbah)
     
-        If i > 3 Then
-            Path_GetDirectoryName = Left(path, i - 1)
+        If i_vbah > 3 Then
+            Path_GetDirectoryName = Left(path_vbah, i_vbah - 1)
         End If
 
     End If
     
 End Function
 
-Public Function Path_GetFileName(ByVal path As String) As String
+Public Function Path_GetFileName(ByVal path_vbah As String) As String
     'Receives a complete path, returns only the file name.
     
-    Dim i As Long
+    Dim i_vbah As Long
     
-    i = InStrRev(path, directoryseparatorchar)
+    i_vbah = InStrRev(path_vbah, directoryseparatorchar_vbah)
     
-    If i > 0 And i < Len(path) Then
-        Path_GetFileName = Mid(path, i + 1)
+    If i_vbah > 0 And i_vbah < Len(path_vbah) Then
+        Path_GetFileName = Mid(path_vbah, i_vbah + 1)
     End If
 
 End Function
 
-Public Function Path_GetFileNameWithoutExtension(ByVal path As String) As String
+Public Function Path_GetFileNameWithoutExtension(ByVal path_vbah As String) As String
     'Receives a complete path, returns only the file name without extension.
     
-    Dim filename As String
-    Dim i As Long
+    Dim filename_vbah As String
+    Dim i_vbah As Long
     
-    filename = Path_GetFileName(path)
+    filename_vbah = Path_GetFileName(path_vbah)
     
-    i = InStrRev(filename, ".")
+    i_vbah = InStrRev(filename_vbah, ".")
     
-    If i = 0 Then
-        Path_GetFileNameWithoutExtension = filename
-    ElseIf i > 0 Then
-        Path_GetFileNameWithoutExtension = Left(filename, i - 1)
+    If i_vbah = 0 Then
+        Path_GetFileNameWithoutExtension = filename_vbah
+    ElseIf i_vbah > 0 Then
+        Path_GetFileNameWithoutExtension = Left(filename_vbah, i_vbah - 1)
     End If
     
 End Function
 
-Public Function String_Contains(ByVal main As String, ByVal value As String) As Boolean
+Public Function String_Contains(ByVal main_vbah As String, ByVal value_vbah As String) As Boolean
     'Returns `True` if the second parameter occurs within the first parameter.
     
-    String_Contains = (InStr(1, main, value) > 0)
+    String_Contains = (InStr(1, main_vbah, value_vbah) > 0)
     
 End Function
 
-Public Function String_EndsWith(ByVal main As String, ByVal value As String) As Boolean
+Public Function String_EndsWith(ByVal main_vbah As String, ByVal value_vbah As String) As Boolean
     'Returns `True` if the second parameter matches the end of the first parameter.
     
-    String_EndsWith = (Right(main, Len(value)) = value)
+    String_EndsWith = (Right(main_vbah, Len(value_vbah)) = value_vbah)
     
 End Function
 
-Public Function String_Format(ByVal format As String, ParamArray args() As Variant)
+Public Function String_Format(ByVal format_vbah As String, ParamArray args_vbah() As Variant)
     'Replaces numbered placeholders ({0}, {1}, ...) in the first parameter by the corresponding value from the additional parameter list.
 
-    Dim numberofargs As Integer
-    Dim i As Integer
+    Dim numberofargs_vbah As Integer
+    Dim i_vbah As Integer
     
-    numberofargs = UBound(args)
+    numberofargs_vbah = UBound(args_vbah)
     
-    For i = 0 To 100
+    For i_vbah = 0 To 100
     
-        If i <= numberofargs Then
-            format = Replace(format, "{" & i & "}", args(i))
+        If i_vbah <= numberofargs_vbah Then
+            format_vbah = Replace(format_vbah, "{" & i_vbah & "}", args_vbah(i_vbah))
         Else
-            format = Replace(format, "{" & i & "}", "")
+            format_vbah = Replace(format_vbah, "{" & i_vbah & "}", "")
         End If
     
     Next
     
-    String_Format = format
+    String_Format = format_vbah
 
 End Function
 
-Public Function String_PadLeft(ByVal inputstring, ByVal totalwidth, Optional ByVal paddingchar = " ")
+Public Function String_PadLeft(ByVal inputstring_vbah, ByVal totalwidth_vbah, Optional ByVal paddingchar_vbah = " ")
     'Right-aligns the first string parameter by padding it on the left with the second string parameter, up to the total specified width.
     'Example: `String_PadLeft("foo",5,"a")` will return `aafoo`
     
-    String_PadLeft = Right(String(totalwidth, Left(paddingchar, 1)) & inputstring, totalwidth)
+    String_PadLeft = Right(String(totalwidth_vbah, Left(paddingchar_vbah, 1)) & inputstring_vbah, totalwidth_vbah)
     
 End Function
 
-Public Function String_PadRight(ByVal inputstring, ByVal totalwidth, Optional ByVal paddingchar = " ")
+Public Function String_PadRight(ByVal inputstring_vbah, ByVal totalwidth_vbah, Optional ByVal paddingchar_vbah = " ")
     'Left-aligns the first string parameter by padding it on the right with the second string parameter, up to the total specified width.
     'Example: `String_PadRight("foo",5,"a")` will return `fooaa`
     
-    String_PadRight = Left(inputstring & String(totalwidth, Left(paddingchar, 1)), totalwidth)
+    String_PadRight = Left(inputstring_vbah & String(totalwidth_vbah, Left(paddingchar_vbah, 1)), totalwidth_vbah)
     
 End Function
 
-Public Function String_StartsWith(ByVal main As String, ByVal value As String) As Boolean
+Public Function String_StartsWith(ByVal main_vbah As String, ByVal value_vbah As String) As Boolean
     'Returns `True` if the second parameter matches the beginning of the first parameter.
     
-    String_StartsWith = (Left(main, Len(value)) = value)
+    String_StartsWith = (Left(main_vbah, Len(value_vbah)) = value_vbah)
     
 End Function
 
 Public Function VBAHelpers_Update()
     'Updates VBA Helpers to newer version by importing a downloaded file (file must be in same folder as current Access database)
     
-    Dim exportfile As String
-    Dim message As String
+    Dim exportfile_vbah As String
+    Dim message_vbah As String
 
-    exportfile = Path_Combine(Path_GetCurrentDirectory, vbahelpersfilename)
+    exportfile_vbah = Path_Combine(Path_GetCurrentDirectory, vbahelpersfilename_vbah)
 
-    If Dir(exportfile) = "" Then
-        message = String_Format("Couldn't find VBA Helpers file in current directory:{0}{1}{0}{0}VBA Helpers update failed!", vbCrLf, exportfile)
-        MsgBox message, vbCritical
+    If Dir(exportfile_vbah) = "" Then
+        message_vbah = String_Format("Couldn't find VBA Helpers file in current directory:{0}{1}{0}{0}VBA Helpers update failed!", vbCrLf, exportfile_vbah)
+        MsgBox message_vbah, vbCritical
         Exit Function
     End If
 
-    Application.LoadFromText acModule, vbahelpersmodulename, exportfile
+    Application.LoadFromText acModule, vbahelpersmodulename_vbah, exportfile_vbah
 
 End Function
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.