select a part of textbox data and change font of this part not all textbox value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • honar
    New Member
    • Aug 2013
    • 5

    select a part of textbox data and change font of this part not all textbox value

    hi everybody
    in ms-access 2010, i have a form with some textbox one of my textbox is p_name textbox over the form a put to different fonts with radio buttons ... i want to change the font of selectedt part of textbox value with radio button font .. when i select part of textbox value and i select another font in radio buttons the font of this part in textbox must change to that new font
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    How many font changes will be in the textbox? For example, if the default font is Calibri, would you only have one section that would be, say, Arial or would you have more sections that would be Times New Roman, Courier New, etc.?

    Comment

    • honar
      New Member
      • Aug 2013
      • 5

      #3
      Seth Schrock thank you for your answer
      yes i want one section would be another font only , only two font if i select any part of textbox data and click radio button that have arila font for example it must change this part to arila font , befor change it must be another font all textbox data

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32653

        #4
        Is that a new option? It's certainly not something I've ever heard of being possible in a TextBox.

        It's possible in an Excel cell, but that's quite another thing.

        PS. I've since learned that it is a new option in 2010.
        Last edited by NeoPa; Feb 4 '14, 12:49 AM. Reason: [z{new ACC2010}]

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Everything that you are requesting can be done but it must be in a specified manner. I'll create a scenario for you to follow.
          1. Create a Text Box on a Form and name it txtRich.
          2. Bind this Text Box to a Field whose Data Type is MEMO, namely: set its Control Source to a MEMO Field which is included in the Row Source of the Form.
          3. In the Table containing this MEMO Field set its Text Format Property to Rich Text.
          4. Open the Form and select any number of Characters in the txtRich Field.
          5. You can now modify the Font Characteristics of the Selected Text only, as in:
            Code:
            Me![txtRich].FontName = "Courier"
          6. Only the Font of the Selected Text will change to Courier.
          7. Any questions feel free to ask.

          Comment

          • Seth Schrock
            Recognized Expert Specialist
            • Dec 2010
            • 2965

            #6
            @Adezii, if you use your code, then the whole text box will be changed to the courier font. The OP is only wanting the portion that he selected in the textbox to be changed. I had created a solution, but it is limited to only one change of font. The second change wipes out the first change as it uses the PlainText() function to be able to get the selected text correctly.

            I'll see if I can dig up what I did (I think that I saved it) and post back here in a little bit (hopefully).
            Last edited by Seth Schrock; Feb 4 '14, 01:06 AM. Reason: Removed a statement that was made in error.

            Comment

            • Seth Schrock
              Recognized Expert Specialist
              • Dec 2010
              • 2965

              #7
              Okay, here is the environment that I built. I have a form (named frmRichText) that has a textbox (named txtRichText) and an option group (named optFont). The option group has three options (you can add all you want), Times New Roman, Arial and Courier New. I also take back that that you don't need it tied to a Memo field as you do need to make it be a memo field with the text format set to rich text. My testing was without binding the textbox to a data source.

              Now, for the code. You need to declare two variables at the module level. I also created an enum for the fonts.
              Code:
              Private Enum ssFont
                  ssTimes = 1
                  ssArial = 2
                  ssCour = 3
              End Enum
              
              Private intStart As Integer
              Private intLength As Integer
              In the textbox's MouseUp event, you need the following code.
              Code:
              Private Sub txtRichText_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
                  
                  intStart = Me.txtRichText.SelStart
                  intLength = Me.txtRichText.SelLength
              
              End Sub
              Next, I have the Option Group AfterUpdate event along with a custom function.
              Code:
              Private Sub optFont_AfterUpdate()
                  Dim strTextbox As String
                  Dim strBef As String
                  Dim strSel As String
                  Dim strAft As String
                  Dim font As String
                  
                  strTextbox = PlainText(Me.txtRichText)
                  
                  strSel = Mid(strTextbox, intStart + 1, intLength)
                  strBef = Left(strTextbox, intStart)
                  strAft = Mid(strTextbox, intStart + intLength + 1)
                  
                  Debug.Print strBef, strSel, strAft
                  
                  Select Case Me.optFont
                      Case ssTimes
                          font = "Times New Roman"
                          
                      Case ssArial
                          font = "Arial"
                          
                      Case ssCour
                          font = "Courier New"
                  
                  End Select
                  
                  Me.txtRichText = "<div>" & strBef & ChangeFont(strSel, font) & strAft & "</div>"
                  
              End Sub
              
              Private Function ChangeFont(Value As String, font As String)
                  
                  ChangeFont = "<font face=""" & font & """>" & Value & "</font>"
              
              End Function
              Now, I don't think that I have forgotten any important design details. Let me know if something doesn't work.

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                @Seth:
                Thank you for pointing this important mistake out to me, don't know what I was thinking! The approach that I proposed does in fact work. Instead of directly trying to modify the Font with VBA Code after selecting it, use the Mini Toolbar to change it (requires Access 2007). The Mini Toolbar becomes active after Text has been selected. As an example I used 8 different Fonts for the String listed below in the Text Box named txtRich. The Formatting is also preserved for each individual Record entry.
                Code:
                She Sells Sea Shells by the Sea Shore
                Remember that the Text Format property of the Text Box must be set to Rich Text and that its Control Source must be set to a MEMO Field whose Text Format should also be set to Rich Text.

                Comment

                • Seth Schrock
                  Recognized Expert Specialist
                  • Dec 2010
                  • 2965

                  #9
                  @Adezii That would be the simpler method, but since the OP requested it be done by clicking an option in an option group, I went the VBA route. Also, I don't think that mini toolbar is available in Access Runtime. This wasn't a requirement in the OP, but it was fun to work out the necessary requirements to do it in VBA.

                  Comment

                  • ADezii
                    Recognized Expert Expert
                    • Apr 2006
                    • 8834

                    #10
                    @Seth:
                    All good points.

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32653

                      #11
                      In case no-one caught Z's edit comment, he's pointed out that this is a new option in 2010. i've updated my earlier post to make that clear to anyone coming new to the thread.

                      Comment

                      Working...