Color Comments Green (1 Viewer)

strive4peace

AWF VIP
Local time
Today, 12:38
Joined
Apr 3, 2020
Messages
1,003
Hello fellow Access Lovers!

We all want code we post to be better understood -- whether we're asking questions or answering them -- or coming back ourselves at some future time!
So this download is to give you green comments! without having to tag them yourself :)

Experienced programmers may not realize how much those green lines really help those who are struggling to understand!

Attached are 3 versions of this tool to add color tags to code for posting, like here on Access World Forums. And now one more version too ... NEW! (30 April) add-in to color comments AND keywords

Choose whichever download you want:
  1. ACCDB: Access 2007 database (works in higher versions too) -- ColorCommentsGreen_s4p_200412_ACCDB.zip
  2. MDB: 2000 database. Perhaps someone can test this and make sure it works ok? thanks! Compile it first! and then SAVE. It isn't compiled for better chance of success -- mdb2K_ColorCommentsGreen_s4p_200412_NOT_COMPILED.zip
  3. ACCDA : Add-in (this is an ACCDB that's been renamed. Works in 2007+ higher versions too) -- Addin_ColorCommentsGreen_s4p.zip
  4. ACCDA: Add-in to Color Comments Green and Keywords Blue https://msaccessgurus.com/tool/Addin_ColorCode.htm
The download has:
  1. one form (f_ColorCOMMENTsGreen_s4p) with all the code it needs behind the form
  2. one table (s4p_Code) The table is only necessary to allow longer code -- otherwise Access runs out of string space.
  3. AutoExec macro -- maximize and open form ... Add-in only maximizes
Add-in note:
The Add-in also has a UsysRegInfo table for registry information. To install an Add-in, you must run Access as Administrator. By doing this, you can launch the add-in anytime, and save time so you can use the form to tag code anytime you're in Access! -- in any database!
Here is an 11-minute video to teach you how to install an Access Addin (and make one if you want to do that too!):​

When you open the database, the form to tag code automatically opens (or when you launch the Add-in)
  1. Paste or write what you want in the Original Code textbox.
  2. TAB out of the Original Code control to run its AfterUpdate event, which adds tags to display the Result code for you.
  3. Press Alt-C to Copy the result code to the clipboard so you can paste it in a forum post, or wherever else you want. The shortcut seems to work better than clicking the button using Access 2007 with my crazy dual-installation.
    Alternately, of course, you can select and copy whatever you want. F2 to toggle between insertion point and select whole value.

form_ColorCommentsGreen.png


On the form, there are 4 textboxes showing you the tags that will be added before and after code, and before and after comments. They're set for Access World Forums using rich text code. You can change these tags to something else, if desired -- for instance, enter HTML if you have web pages with code that you want to make better understood.

When you make the form window taller, in 2007+, the code boxes grow since they're anchored. 2003- doesn't have anchoring -- but you can always go to the design view and change the size of the boxes yourself. If you resize the window, before you save it, change some property so it sticks.

In your AWF post:
  • click the icon to Toggle BB code so you see the codes instead of the effects, and paste
    ToggleBBcode.png

    Otherwise the codes won't work right!
Result code in your post

Here is an example of what the code will look like in your post, using the code behind the f_ColorCOMMENTsGreen_s4p form:

Rich (BB code):
Option Compare Database
Option Explicit  'require variable declaration

'NEED table: s4p_Code
'  so this will work with long code too

Private Sub Form_Load()
'200410 strive4peace
   'clear old data
   Me.codeOrig = Null
   Me.codeResult = Null
End Sub

Private Sub codeOrig_AfterUpdate()
'200410,11,12 strive4peace
'read lines from codeOrig
'construct string to post in codeResult with specified tags
'process one line at a time

   Dim sOrig As String _
      , sLine As String _
      , sBeforeComment As String _
      , sAfterComment As String _
      , sDeli As String _
      , vResult As Variant _
      , iPosSingleQuote As Integer _
      , iPosDoubleQuote1 As Integer _
      , iPosDoubleQuote2 As Integer _
      , i As Integer _
      , bInComment As Boolean _
      , bAllComment As Boolean _
      , bLook As Boolean

   Dim aLine() As String
   'set delimiter for lines
   sDeli = vbCrLf

   'initialize vResult
   vResult = Null

   With Me
      'exit if code to process is blank
      If IsNull(.codeOrig) Then GoTo Proc_WriteResult
      'code to process
      sOrig = .codeOrig
      'set text to add before and after comment
      sBeforeComment = Nz(.txtBeforeComment, "")
      sAfterComment = Nz(.txtAfterComment, "")
   End With
   bInComment = False
   bAllComment = False

   'split code at line breaks
   aLine = Split(sOrig, sDeli)

   'process each line -- append color code before and after comment
   For i = LBound(aLine) To UBound(aLine)
      sLine = aLine(i)
      bAllComment = False
      iPosDoubleQuote1 = 0
      iPosDoubleQuote2 = 0

      'see if there is a single quote inside the string
      iPosSingleQuote = InStr(sLine, "'")
      If iPosSingleQuote > 0 Then
         If Left(Trim(sLine), 1) = "'" Then
            bAllComment = True
         Else
            'make sure single quote isn't inside double quotes
            bLook = True
            Do While bLook
               'see if there is a double quote before the single quote
               iPosDoubleQuote1 = InStr(Left(sLine, iPosSingleQuote), """")
               If iPosDoubleQuote1 > 0 Then
                  'see if there is a double quote after the single quote
                  iPosDoubleQuote2 = InStr(iPosSingleQuote + 1, sLine, """")
                  If iPosDoubleQuote2 > 0 Then
                     'look for another single quote after the double quote end
                     iPosSingleQuote = InStr(iPosDoubleQuote2 + 1, sLine, "'")
                     If Not iPosSingleQuote > 0 Then
                        bLook = False
                     End If
                  Else
                     bLook = False
                  End If
               Else
                  bLook = False
               End If
            Loop
         End If
      End If
      If bAllComment Or iPosSingleQuote > 0 Then
         If bInComment And Not bAllComment Then
            'comment is at end of line
            'end previous comment
            vResult = vResult & sAfterComment
            bInComment = False
         End If

         If Not bInComment Then
            sLine = Left(sLine, iPosSingleQuote - 1) _
               & sBeforeComment _
               & Mid(sLine, iPosSingleQuote)
            bInComment = True
         End If
      Else
         If bInComment = True Then
            vResult = vResult & sAfterComment
            bInComment = False
         End If
      End If

      vResult = (vResult + vbCrLf) & sLine
   Next i

   With Me
      vResult = .txtBeforeCode & vResult & .txtAfterCode
   End With

Proc_WriteResult:
   Me.codeResult = vResult
End Sub

Private Sub cmd_Copy2Clipboard_Click()
'200411 strive4peace
'copy result code to the clipboard
   Dim sCode As String
   With Me.codeResult
      If Nz(.Value, "") = "" Then Exit Sub
      sCode = .Value
   End With
   'MSForms.DataObject
   With CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
      .SetText sCode
      .PutInClipboard
   End With
   MsgBox "Press Ctrl-V to paste code with tags where you want it", , "Done"
End Sub

EDIT: 12 April 2020: REPLACED DOWLOADS

changed the code to look to see if single quote in in a double quoted string. If so, it is ignored. This helps some of the coloring issues ... maybe most? If you see another place where it doesn't color what it should, or colors what it shouldn't, please post the line it failed on. And if you know how to fix it, post your code change so the download can be updated too (or maybe I'll figure it out), -- collaboration, and inspiration, drives us to be the best

Please share your comments! Thank you!
 

Attachments

  • Addin_ColorCommentsGreen_s4p.zip
    39.9 KB · Views: 828
  • mdb2K_ColorCommentsGreen_s4p_200412_NOT_COMPILED.zip
    20.5 KB · Views: 726
  • ColorCommentsGreen_s4p_200412_ACCDB.zip
    36.9 KB · Views: 584
Last edited:

strive4peace

AWF VIP
Local time
Today, 12:38
Joined
Apr 3, 2020
Messages
1,003
you're welcome, and thanks, Colin!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:38
Joined
May 7, 2009
Messages
19,169
that can be set in VBA editor menu, Tools->Options...->Editor Format
 

Gasman

Enthusiastic Amateur
Local time
Today, 17:38
Joined
Sep 21, 2011
Messages
14,045
I think Crystal means when you post here Arnel?

Any comments just look like other lines of code until you spot the ' ?

Rich (BB code):
Private Sub cmdBrowse_Click()

    On Error GoTo Err_Handler
   
    Dim SB_Attachment As String
    Dim strFolder As String
    Dim strFile As String
    Dim intPos As Integer
   
    SB_Attachment = BrowseFile("C:\Temp")
   
    If SB_Attachment <> "" Then
        ' get folder and file names
        intPos = InStrRev(SB_Attachment, "\\Sjo2054\shared\Data_Files\")
        strFolder = Left(SB_Attachment, intPos - 1)
        strFile = Mid(SB_Attachment, intPos + 1)
       
        ' populate text boxes on form
        SB_Attachment = SB_Attachment
     
    End If
   
Exit_Here:
    Exit Sub
   
Err_Handler:
    MsgBox Err.Description
    Resume Exit_Here

End Sub
 
Last edited:

strive4peace

AWF VIP
Local time
Today, 12:38
Joined
Apr 3, 2020
Messages
1,003
am I missing something, Arne (or is it Arnel)? How does changing options in VBA help color code that is posted on Access World Forums?
 
Last edited:

strive4peace

AWF VIP
Local time
Today, 12:38
Joined
Apr 3, 2020
Messages
1,003
Any comments just look like other lines of code until you spot the ' ?

Gasman, Thanks, but that doesn't quite work, as I discovered when writing code to color comments for my web pages ... there could be a single quoted string inside double quotes. That is why iCountSingleQuote Mod 2 is excluded

Without doing more logic, like getting the position of possible double quotes around it, or a lot more to figure out what is code and excluding that, I opted for a not-so-perfect but close way ;)
 
Last edited:

strive4peace

AWF VIP
Local time
Today, 12:38
Joined
Apr 3, 2020
Messages
1,003
Happy Easter! Today is a day that symbolizes new beginnings ... wishing the best for you!

If you've already downloaded this tool, you might want to get the updated version (12 April 2020). Now the form also has a version number in the header (current is 200412 = yymmdd) so you can see what version you're using.

Today, I edited the code to look to see if the single quote found is in a double quoted string. If so, it is ignored, and code looks for the next one. It seems there should be a better way to do that loop ... any ideas? ... an Access application is never done ;)

please share your ideas and comments ~ thank you
 
Last edited:

strive4peace

AWF VIP
Local time
Today, 12:38
Joined
Apr 3, 2020
Messages
1,003
NEW! add-in to color comments AND keywords

Tool > Add-in > Color VBA Code - Green comments, Blue Keywords

especially for you, Mick ~
 

Users who are viewing this thread

Top Bottom