473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Macro to format data in MS Access Form

10
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 txtboxBackground.
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 txtboxBackground, select the data, and change the data to Arial 10.

DoCmd.GoToControl "txtBoxBackground" will take me there, but I can’t get passed this.
Any help would be much appreciated.
Aug 25 '15 #1

✓ answered by jforbes

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:
Expand|Select|Wrap|Line Numbers
  1. Public Function replaceWithRegex(ByVal sTemp As String, ByRef sPattern As String, ByRef sReplaceWith As String) As String
  2.     Dim RegEx As Object
  3.     Set RegEx = CreateObject("VBScript.RegExp")
  4.     RegEx.Global = True
  5.     RegEx.Pattern = sPattern
  6.     replaceWithRegex = RegEx.Replace(sTemp, sReplaceWith)
  7. End Function
Then you can call it like so:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command2_Click()
  2.     Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*font([\s\S]*?)>", "")
  3. End Sub
  4.  
When the Command2 button is clicked, it will send the contents of the TextBox named Text0 to replaceWithRegex() 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/

21 2175
jimatqsi
1,271 Expert 1GB
Check the "text format" property on that textbox. I believe if you change it to "Plain Text" you will not have that problem.

Jim
Aug 25 '15 #2
DrPete
10
Thanks, Jim
But I need the box to be rich text because the boss likes bullet lists..
Aug 25 '15 #3
NeoPa
32,556 Expert Mod 16PB
In the AfterUpdate() Event Procedure of the control you would set the .FontName & .FontSize properties back to the desired values :
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtBoxBackground_AfterUpdate()
  2.     With Me.txtBoxBackground
  3.         If .FontName <> "Arial" Then .FontName = "Arial"
  4.         If .FontSize <> 10 Then .FontSize = 10
  5.     End With
  6. 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.
Aug 25 '15 #4
DrPete
10
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.
Aug 26 '15 #5
NeoPa
32,556 Expert Mod 16PB
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.
Aug 26 '15 #6
DrPete
10
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?
Aug 26 '15 #7
NeoPa
32,556 Expert Mod 16PB
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 :
Expand|Select|Wrap|Line Numbers
  1. 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.
Aug 26 '15 #8
DrPete
10
Thanks, NeoPa
Yes, the code is running, but for some reason won't change the font name and size.
Aug 27 '15 #9
NeoPa
32,556 Expert Mod 16PB
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.
Aug 27 '15 #10
DrPete
10
Sorry, my mistake. It is 2010 version. Does this make a difference?
Aug 27 '15 #11
jforbes
1,107 Expert 1GB
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:
Expand|Select|Wrap|Line Numbers
  1. Public Function replaceWithRegex(ByVal sTemp As String, ByRef sPattern As String, ByRef sReplaceWith As String) As String
  2.     Dim RegEx As Object
  3.     Set RegEx = CreateObject("VBScript.RegExp")
  4.     RegEx.Global = True
  5.     RegEx.Pattern = sPattern
  6.     replaceWithRegex = RegEx.Replace(sTemp, sReplaceWith)
  7. End Function
Then you can call it like so:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command2_Click()
  2.     Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*font([\s\S]*?)>", "")
  3. End Sub
  4.  
When the Command2 button is clicked, it will send the contents of the TextBox named Text0 to replaceWithRegex() 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/
Aug 27 '15 #12
DrPete
10
Thank you. I will give it a try as soon as I can get exclusive control of the database. Cheers!
Aug 28 '15 #13
NeoPa
32,556 Expert Mod 16PB
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.
Sep 2 '15 #14
DrPete
10
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
Sep 4 '15 #15
DrPete
10
jforbes - you da man! It works, works works! Thank you a million. You made me look good to the boss, who thinks I am the best thing since sliced bread!
But one more question - I want any italics or bold formatting to be not italics and not bold after pasting into my text box. Can that be done?
Sep 4 '15 #16
jforbes
1,107 Expert 1GB
Glad to hear it.

Here are examples of Bold, Italics and a few other Regex patterns. The one at the end should remove them all. You'll need to play around with it to make sure that it is working for you.

Expand|Select|Wrap|Line Numbers
  1. ' Bold
  2. Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*b([\s\S]*?)>", "")
  3. ' Strong
  4. Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*strong([\s\S]*?)>", "")
  5. ' Italic
  6. Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*(i[\s\S]*?)>", "")
  7. ' Emphasized
  8. Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*(em[\s\S]*?)>", "")
  9. ' Small
  10. Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*(small[\s\S]*?)>", "")
  11. ' Marked/Highlighted
  12. Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*(mark[\s\S]*?)>", "")
  13. ' Deleted
  14. Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*(del[\s\S]*?)>", "")
  15. ' Inserted
  16. Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*(ins[\s\S]*?)>", "")
  17. ' Subscript
  18. Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*(sub[\s\S]*?)>", "")
  19. ' Superscript
  20. Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*(sup[\s\S]*?)>", "")
  21.  
  22. ' Everything
  23. Me.Text0.Value = replaceWithRegex(Me.Text0.Value, "<\/*b([\s\S]*?)>|<\/*strong([\s\S]*?)>|<\/*(i[\s\S]*?)>|<\/*(em[\s\S]*?)>|<\/*(small[\s\S]*?)>|<\/*(mark[\s\S]*?)>|<\/*(del[\s\S]*?)>|<\/*(ins[\s\S]*?)>|<\/*(sub[\s\S]*?)>|<\/*(sub[\s\S]*?)>|<\/*(sup[\s\S]*?)>", "")
Sep 4 '15 #17
DrPete
10
jforbes
Brilliant advice again. One thing I have noticed is that if a text box is blank and without any data having been entered into it, and then I press the command button to format the text box, I get an VBA error message stating "invalid use of Null. I have tried inserting an If Me.Text0.value <> Null Then... (regex code) type expression, but that seems to render the code impotent. Any clues here?
Sep 7 '15 #18
jforbes
1,107 Expert 1GB
Doing something like the following would probably be a better way of calling the function as it would only call it if there was something to work on and it would address Nulls.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command2_Click()
  2.     Dim sText As String
  3.     sText = Nz(Me.Text0.Value, "")
  4.     if Len(sText) > 0 Then Me.Text0.Value = replaceWithRegex(sText , "<\/*font([\s\S]*?)>", "")
  5. End Sub
Sep 7 '15 #19
NeoPa
32,556 Expert Mod 16PB
Hi JForbes.

I want to thank you for this code too. I use Replace() in my code a great deal. So much even, that I have my own MultiReplace() function that handles multiple pairs of find and replace parameters. I also use RegEx in my text editor to do some very powerful data (and code) preparatory work. What I didn't have until now was procedure to handle replacing with RegEx in code.

Now I do!! Excellent.
Expand|Select|Wrap|Line Numbers
  1. Public Function RegExReplace(varString As Variant _
  2.                            , strFrom As String _
  3.                            , strTo As String) As String
  4.     If varString > "" Then
  5.         With CreateObject("VBScript.RegExp")
  6.             .Global = True
  7.             .Pattern = strFrom
  8.             RegExReplace = .Replace(CStr(varString), strTo)
  9.         End With
  10.     End If
  11. End Function
PS. This handles a varString parameter of a control that has nothing (Null) in it.
Sep 12 '15 #20
jforbes
1,107 Expert 1GB
Glad to hear it NeoPa, and I like the addition of checking for a blank string.
Sep 15 '15 #21
NeoPa
32,556 Expert Mod 16PB
: - )
Sep 18 '15 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Iain Miller | last post by:
Struggling a bit here & would be grateful for any help. I have a table which has a list of people in it. Each person has a unique ID automatically allocated by Access but also belongs to one of 5...
8
by: Colleyville Alan | last post by:
I have been working on an Access app that takes info from a file and writes it to a spreadsheet on a form, simultaneously saving the spreadsheet to Excel. I got the idea that the same concept...
2
by: Roxie Aho | last post by:
Using Access 2003. Linked table from SQL Server has phone as 1234567890. Query has phone formatted as (@@@) @@@-@@@@, displaying (123) 456-7890. Data access page is based on the query. Phone...
0
by: sunilkeswani | last post by:
Hi I have a Data Access Page which is linked to a Access Database table, and the page represents data in a Pivot Chart format. I want to know if there is any way by which - the pivot chart...
0
by: Qajussi | last post by:
Hi! I am trying to create data entry forms like MS access forms using asp.net. I am talking about a form like MS access forms which let you enter a new record, edit, navigate to previous and next...
1
by: Mike | last post by:
Hi all, Hum i just erased the whole message by mistake, :( Ok so i want to hide a specific field in an Access form using a toggle button, but i cant seem to get the right syntax. Right now im...
0
by: charlie weaver | last post by:
Is it possible to send an email in HTML format using and Access Form created with VB 6? Currently my form sends as RTF, but I cant seem to figure out how to send in HTML format.
1
by: fishmore | last post by:
MS Access question: I have made an access form with unbound control source fields and have written a Macro that will make a new record and store the data from each unbound field in a table. This...
7
Jerry Maiapu
by: Jerry Maiapu | last post by:
Hello everyone, I have searched the forum and internet on collecting data from outlook into access with custom formatting but no there is no resource to assist. I 'have looked at the following...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.