Automatic Bullets for Textbox with Long Text Data Type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Merlene
    New Member
    • Sep 2018
    • 12

    Automatic Bullets for Textbox with Long Text Data Type

    I would firstly, like to add a bullet to a Form textbox as soon as the user begins typing and secondly, add a bullet to each new line. I have code that works fine when the datatype is short text but I am unable to get something that works for long text/ memo. I am using Ms Access 2013. This is what I use for the short text:

    [Private Sub Meeting_Notes_C hange()

    If Len(Trim(Meetin g_Notes.Text)) = 1 And _
    Meeting_Notes.T ext <> "• " Then _
    Meeting_Notes.T ext = "• " & Meeting_Notes.T ext

    End Sub]

    [Private Sub Meeting_Notes_K eyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 13 Then
    KeyCode = 0
    Meeting_Notes.T ext = Meeting_Notes.T ext & Chr(13) & Chr(10) & "• "
    End If

    End Sub]
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    Your code is nearly there. In your subs, you should add code to tell the cursor to go to the end of the text. Add this to the end of both subs:
    Code:
    Meeting_Notes.SelectionStart = Meeting_Notes.Text.Length + 1
    and you should change your KeyDown event to a KeyUp event. The enter key itself actually fires on the KeyDown event which causes the line return, so you'll want work to take place after that, which is the KeyUp event. Then your code can stay the same, but remove the Chr(10) portion. I hope this helps, and here is my whole code in VB.NET, for reference, which is pretty close in syntax.

    Code:
    Public Class Form1
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            If TextBox1.Text.Length = 1 And TextBox1.Text <> "•" Then
                TextBox1.Text = TextBox1.Text.Insert(0, "•")
                TextBox1.SelectionStart = TextBox1.Text.Length + 1
            End If
        End Sub
    
        Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
            Dim KeyCode As Integer = e.KeyCode
            If KeyCode = 13 Then
                TextBox1.Text = TextBox1.Text & "•"
                TextBox1.SelectionStart = TextBox1.Text.Length
            End If
        End Sub
    
    End Class

    Comment

    • PhilOfWalton
      Recognized Expert Top Contributor
      • Mar 2016
      • 1430

      #3
      I think this works, but in order to insert your bullet, I have used a Shift Return Key. The unshifted Return Key operates in the usual way.

      The Shift Return Key can be used in the middle of the text, as well as the end.

      It also strips any unused bullets at the end of the text.

      Code:
      Option Compare Database
      Option Explicit
      
      Private intSelStart As Integer
      Private intSelLength As Integer
      
      Private Sub MeetingNotes_Enter()
      
          If Len(MeetingNotes.Text) = 0 Then
              MeetingNotes.Text = "•" & Chr$(2)
              MeetingNotes.SelStart = 3           ' Next Line position after space
          End If
              
      End Sub
      
      Private Sub MeetingNotes_Exit(Cancel As Integer)
      
          If Len(MeetingNotes.Text) > 2 Then                  ' Need as least "•" & Chr$(2)
              If Right(MeetingNotes.Text, 2) = "•" & Chr$(2) Then
                  MeetingNotes.Text = Left(MeetingNotes.Text, Len(MeetingNotes.Text) - 2)
              End If
          End If
      
      End Sub
      
      Private Sub MeetingNotes_KeyDown(KeyCode As Integer, Shift As Integer)
      
          If KeyCode = 13 And Shift = acShiftMask Then         'Shifted Return
              intSelStart = MeetingNotes.SelStart
              KeyCode = 0
              MeetingNotes.Text = Left(MeetingNotes.Text, intSelStart) & vbCrLf & "•" & Chr$(2) & Mid(MeetingNotes.Text, intSelStart + 1)
              MeetingNotes.SelStart = MeetingNotes.SelStart + intSelStart + 4
          End If
          
      End Sub
      
      Private Sub MeetingNotes_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
      
      'set variables after making selection and releasing mousebutton
          intSelStart = MeetingNotes.SelStart
          intSelLength = MeetingNotes.SelLength
          
      End Sub
      A real bu..er to get to work.

      Phil

      Comment

      • Merlene
        New Member
        • Sep 2018
        • 12

        #4
        Luk3r,

        Thanks! The syntax was just slightly different but it pointed me in the right direction. Here's what I used

        [Meeting_Notes.S elStart = Len(Meeting_Not es.Text) + 1]

        Comment

        • Merlene
          New Member
          • Sep 2018
          • 12

          #5
          Thanks PhilOfWalton but I used the other suggestions as it was simpler to add the line of code.

          Comment

          • PhilOfWalton
            Recognized Expert Top Contributor
            • Mar 2016
            • 1430

            #6
            Just thought I would point out that Luk3r's method only adds the bullet at the end of the notes. The method I suggested allows the addition of a new bulleted line in the middle of existing text. Additionally I don't think there is a space between the bullet and the first word in the line.

            It's up to you which you use.

            Phil

            Comment

            • Merlene
              New Member
              • Sep 2018
              • 12

              #7
              Actually I see what you're saying. I tried your method and although longer, it works! Thanks.

              Comment

              • PhilOfWalton
                Recognized Expert Top Contributor
                • Mar 2016
                • 1430

                #8
                Always best to have code that works rather than the shortest code that almost works.

                Also useful to have the Return key (Chr$(13)) working normally so that you can insert blank lines between bullet points.

                I have revised the OnExit code, because what I sent you will only remove one "spare" bullet at the end of your text. This code removes all the spare bullets.

                Code:
                Private Sub MeetingNotes_Exit(Cancel As Integer)
                
                    If Len(MeetingNotes.Text) > 2 Then                  ' Need as least "•" & Chr$(2)
                        Trim (MeetingNotes)
                        Do Until Right(MeetingNotes.Text, 2) <> "•" & Chr$(2)
                            MeetingNotes.Text = Left(MeetingNotes.Text, Len(MeetingNotes.Text) - 2)
                            Trim (MeetingNotes)
                        Loop
                    End If
                
                End Sub
                Glad to be of help

                Phil

                Comment

                Working...