Macro to format data in MS Access Form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DrPete
    New Member
    • Aug 2015
    • 10

    Macro to format data in MS Access Form

    Hello
    I've just joined, and this is my first question!
    I have an 2003 MS Access Form named frmCurrentBills with a text field in the form txtboxBackgroun d.
    The default font for the data in that field is Arial 10 (set everywhere as far as I can see), but often after a cut and paste to that field from an external source the font ends up being the external source font, which results in me having to manually reformat.
    I want to a macro or event that will go to that txtboxBackgroun d, select the data, and change the data to Arial 10.

    DoCmd.GoToContr ol "txtBoxBackgrou nd" will take me there, but I can’t get passed this.
    Any help would be much appreciated.
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    Check the "text format" property on that textbox. I believe if you change it to "Plain Text" you will not have that problem.

    Jim

    Comment

    • DrPete
      New Member
      • Aug 2015
      • 10

      #3
      Thanks, Jim
      But I need the box to be rich text because the boss likes bullet lists..

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32663

        #4
        In the AfterUpdate() Event Procedure of the control you would set the .FontName & .FontSize properties back to the desired values :
        Code:
        Private Sub txtBoxBackground_AfterUpdate()
            With Me.txtBoxBackground
                If .FontName <> "Arial" Then .FontName = "Arial"
                If .FontSize <> 10 Then .FontSize = 10
            End With
        End Sub
        It shouldn't be necessary to set the focus back to the control in order to do this.

        NB. This is air-code so please test it out first.

        Comment

        • DrPete
          New Member
          • Aug 2015
          • 10

          #5
          Thanks, NeoPa
          I created an event procedure for the text box (AfterUpdate), I entered your code exactly as you stated, saved it and all the rest. Back in the form, I then changed the data font and size, and then moved elsewhere in the form, pressed other buttons etc, but the text did not change to Arial 10. Nothing seemed to happen that I could see.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32663

            #6
            Can you confirm the code is actually running?

            The property needs to be set to "EventProcedure " and the security settings must allow any code to run.

            Comment

            • DrPete
              New Member
              • Aug 2015
              • 10

              #7
              I have other EventProcedure and Macros connected to other boxes and buttons, and they all run okay.
              I think the code is running, but just not doing its job. But how can I tell?

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32663

                #8
                Originally posted by DrPete
                DrPete:
                I think the code is running, but just not doing its job. But how can I tell?
                Either add a breakpoint to the first line (Debugging in VBA), or add a new line in the procedure with just :
                Code:
                Stop
                In either case if it doesn't break then it isn't running. If it does stop then just hit F5 to continue normally.

                Comment

                • DrPete
                  New Member
                  • Aug 2015
                  • 10

                  #9
                  Thanks, NeoPa
                  Yes, the code is running, but for some reason won't change the font name and size.

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32663

                    #10
                    Very curious. Are you sure your database is an MDB (2003 format)? RTF was only introduced in 2007 (ACCDB). I can't imagine how formatted text can be stored or shown in a TextBox that's MDB.

                    Comment

                    • DrPete
                      New Member
                      • Aug 2015
                      • 10

                      #11
                      Sorry, my mistake. It is 2010 version. Does this make a difference?

                      Comment

                      • jforbes
                        Recognized Expert Top Contributor
                        • Aug 2014
                        • 1107

                        #12
                        If you have set your TextBox to use RichText you will run into a lot of interesting little challenges like this. If you can talk your user or Boss into using Plain Text instead you will probably save yourself a lot of headaches in the future.
                        The biggest drawback I've seen is that the HTML Tags can get in the way of a SQL Where clause and records that you would think would be included in a Recordset aren't because of the hidden tags. The second and equally disturbing drawback is the amazing amount of extra characters that can end up in the field.

                        But if you can't talk your Boss out of the Rich Text setting, then using a Regex to strip out Tags might work for you. There are lots of ways to go about manipulating HTML, but it gets pretty crazy pretty fast. Regex is comparatively simple,https://en.wikipedia.org/wiki/Regular_expression, but even though it is simpler, it can be a nightmare of it's own.

                        I use this function. If your up for it, you'll want to put it in a VBA Code Module:
                        Code:
                        Public Function replaceWithRegex(ByVal sTemp As String, ByRef sPattern As String, ByRef sReplaceWith As String) As String
                            Dim RegEx As Object
                            Set RegEx = CreateObject("VBScript.RegExp")
                            RegEx.Global = True
                            RegEx.Pattern = sPattern
                            replaceWithRegex = RegEx.Replace(sTemp, sReplaceWith)
                        End Function
                        Then you can call it like so:
                        Code:
                        Private Sub Command2_Click()
                            Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*font([\s\S]*?)>", "")
                        End Sub
                        When the Command2 button is clicked, it will send the contents of the TextBox named Text0 to replaceWithRege x() along with a Regex pattern that defines an HTML <Font> tag to replaced by an empty string.

                        I played around with this a little, 'cause it looked fun, and when I copy text from a web page that has text of varying sizes into a TextBox, the text sizes vary as they do on the original web page. When the button is clicked, the font size becomes uniform and that of the TextBox's Font Size Property.

                        Hopefully this will help you. And if you want to explore some Regex patterns on your own, this site is really helpful: http://www.regexr.com/

                        Comment

                        • DrPete
                          New Member
                          • Aug 2015
                          • 10

                          #13
                          Thank you. I will give it a try as soon as I can get exclusive control of the database. Cheers!

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32663

                            #14
                            Originally posted by DrPete
                            DrPete:
                            Sorry, my mistake. It is 2010 version. Does this make a difference?
                            Only in as much as it's hard to work when you don't know the facts as you have to try to form a picture in your mind of what's what. A bit like that picture painters paint of bricks that always appear to lead on to the next one in sequence, yet go around in a loop. Each connection looks fine, but none of it actually makes sense. If one were to ask me a technical question about it, such as which is the highest, then I'd be sunk. There is nothing there to work with logically. That's why it's so important to present the information carefully and accurately when asking questions.

                            On this issue I'm afraid I have to admit that I've reached the end of my road. I've never used them (RTF TextBoxes) myself, except for trying things out for people such as yourself. I think I've summed up my whole understanding of the subject in my previous posts. Sorry.
                            Last edited by NeoPa; Sep 2 '15, 10:53 PM.

                            Comment

                            • DrPete
                              New Member
                              • Aug 2015
                              • 10

                              #15
                              Neopa,
                              Thank you for your help. Trying something that does not work is not a fail, but the elimination of one or more issues or possible solutions. Knowing what will not work helps you narrow down what will or may work. You have been a great help. Thank you. I haven't tried RegEx just yet, as I needed some clear time and sole user access to the database. I hope it works. Cheers

                              Comment

                              Working...