473,404 Members | 2,137 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,404 software developers and data experts.

How do I stop my vba code from continuing to surround my comments with parentheses?

wordbrew
Hello all,

This is my first time posting so I hope I can get some insight into my problem.

I have a subform with a comments section, that, when a comment is entered, and then tabbed off of (loses focus) the code surrounds that statement in parentheses. This is working great. The only problem is that if the that comment is clicked again or is otherwise put back in focus, then when it is tabbed off of or loses focus again, then the vba code surrounds it with more parentheses. If I was the only one using this database I wouldn't care because I could easily just manually avoid the problem, but there are others who use it, and there comments end of looking something like this (((((((VacationDay)))))) instead of (Vacation Day). Is there anyway to stop my code from executing if my value is met. I've tried a few things but they aren't solving the problem. This is what my current VBA code looks like.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Comments_LostFocus()
  2. Dim Comm As String, OpenPAr As String, CLosePar As String
  3. If Not IsNull(Me.Comments.Value) Then
  4. Comm = Me.Comments.Value
  5. OpenPAr = "("
  6. CLosePar = ")"
  7. Me.Comments.Value = OpenPAr & Comm & CLosePar
  8. Else
  9. End If
  10.  
  11. End Sub
I appreciate any help at all. Let the parenthetical madness stop!!!!!!!!
Jan 22 '10 #1

✓ answered by ADezii

Expand|Select|Wrap|Line Numbers
  1. Private Sub Comments_LostFocus()
  2. Dim Comm As String
  3. Dim OpenPAr As String
  4. Dim CLosePar As String
  5.  
  6. OpenPAr = "("
  7. CLosePar = ")"
  8.  
  9. If Not IsNull(Me.Comments) Then
  10.   Comm = Replace(Replace(Me.Comments, OpenPAr, ""), CLosePar, "")
  11.     Me.Comments = OpenPAr & Comm & CLosePar
  12. End If
  13. End Sub

14 2077
ADezii
8,834 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. Private Sub Comments_LostFocus()
  2. Dim Comm As String
  3. Dim OpenPAr As String
  4. Dim CLosePar As String
  5.  
  6. OpenPAr = "("
  7. CLosePar = ")"
  8.  
  9. If Not IsNull(Me.Comments) Then
  10.   Comm = Replace(Replace(Me.Comments, OpenPAr, ""), CLosePar, "")
  11.     Me.Comments = OpenPAr & Comm & CLosePar
  12. End If
  13. End Sub
Jan 22 '10 #2
Wow ADezii. That worked perfectly! I was going nuts trying to solve that. Everytime I learn more about VBA I always come out with more questions lol. Now I'm going to have to examine the code you gave me, so I can truly understand how it worked and solved my problem. Looks like I need to learn about replace commands.

Thanks so much ADezii. You are a brilliant man my friend.
Jan 22 '10 #3
ADezii
8,834 Expert 8TB
You are quite welcome, all fixes should be so easy! (LOL)
Jan 22 '10 #4
NeoPa
32,556 Expert Mod 16PB
While that will strip the parentheses from your Comments control, it will strip all the parentheses from your comments control. If you plan to disallow anyone from entering these characters at all in their comments, this will be no problem. Otherwise however, you will find a very difficult problem to debug when your users complain strange things are happening to the comments they enter.

A solution to parentheses only found at both the start and end of the text is :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Comments_LostFocus()
  2.   Dim strCom As String
  3.  
  4.   strCom = Me.Comments
  5.   If Right(strCom) = "(" _
  6.   And Left(strCom) = ")" Then _
  7.     strCom = Mid(strCom, 2, len(strCom) - 2)  
  8.   Me.Comments = "(" & strCom & ")"
  9. End Sub
Jan 24 '10 #5
edsuk
10
Just for curiosity, why are you using the 'LostFocus' event, surely it would make more sense to place this sub procedure in the 'AfterUpdate' event. So that the code only runs after the data has been modified?
Jan 25 '10 #6
NeoPa
32,556 Expert Mod 16PB
A good point. I certainly concur.

I expect the answer is as simple as that the OP is simply quite inexperienced. We experts should probably have picked up on that one and mentioned it I expect, but I for one was in a bit of a rush at the time. Well done for picking it up :)
Jan 25 '10 #7
ADezii
8,834 Expert 8TB
The LostFocus() Event could be used to Update existing Records where multiple Parenthesis are found, whereas the AfterUpdate() would not unless a change was made. Did I just invent a New Update Method? (LOL)!
Jan 25 '10 #8
NeoPa
32,556 Expert Mod 16PB
I wouldn't shout about it ADezii. It might help a little, but it's hardly a sound method for clearing up existing problems in data.

I had to laugh though :D
Jan 25 '10 #9
ADezii
8,834 Expert 8TB
Still don't know when I'm kidding ya, do ya?
Jan 25 '10 #10
NeoPa
32,556 Expert Mod 16PB
I always know ADezii. (Read my post again.)

It's easy - It's when you post (LoL).
Jan 25 '10 #11
Thanks for all your responses gentleman, I really do appreciate it. Fortunately, ADezii's original solution did solve my problem. Now when an item is tabbed off of, it becomes surrounded in parenthesis (which I'm having it do because I have a separate textbox on the main form pulling in information from separate sources including the comments section, and having the comments in parentheses sets them apart), and if the comments are clicked again, tabbed back on and off, etc...only the original parentheses are left behind. This is perfect.

Edsuk and NeoPa you guys are totally right, I could have put this as an AfterUpdate event. Lol, I guess I just like the instant gratification of seeing the comments in the subform datasheet get surrounded by parentheses when they are tabbed off of, plus it does have the added benefit of being an easy way of cleaning up pre-existing multiple parentheses debacles.

And you are quite right NeoPa. I'll always have more to learn. Luckily with the quick responses from you kind sirs, I can feel secure that if I encounter any more roadblocks, the Bytes community will set me back on track.

Thanks again guys!
Jan 25 '10 #12
NeoPa
32,556 Expert Mod 16PB
You're welcome. Always nice to read such a pleasant response.

I suppose that means I'll have to set post #2 as Best Answer now. Darn! Shucks! ADezii gets it again! :D
Jan 26 '10 #13
ADezii
8,834 Expert 8TB
plus it does have the added benefit of being an easy way of cleaning up pre-existing multiple parentheses debacles.
And you thought my new Update Creation was useless! (LOL)
Jan 26 '10 #14
NeoPa
32,556 Expert Mod 16PB
Not Useless! I would never say that!

Just of very little use is all :D
Jan 26 '10 #15

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

Similar topics

253
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
192
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
8
by: Matt Theule | last post by:
While stepping through an ASP.NET project, I found that data was being inserted into my database even though I was not stepping through the code that inserted the data. I have a single page with...
7
by: alankrit | last post by:
Helo This is a code for Radix sort. n it is giving a run-time Error : "Run-Time Check Failure #2 - Stack around the variable 'count' was corrupted." I have cross checked this code a millions of...
3
by: KL | last post by:
I am trying to get a form to work with validating a checkbox. It validates the checkbox ok, but it always redirects, even if the box isn't checked...it does alert, but I was hoping that it would...
12
by: syn1kk | last post by:
1: float (*data); 2: data = malloc(31 * sizeof(data)); 3: data = VARIABLE; Question 1: The variable data is a float pointer? Question 2: When the is used. Does that mean it is an...
0
by: Saran | last post by:
Hi, I am having a method that handles the click event of a button on the page. This method will also send an email. Sending email comes after several lines of code in the method. I was testing...
3
by: diego | last post by:
Greetings everyone! Is there a way to stop query analyzer from processing remaining query statements? Let's say i have the following query in query analyzer: query statements if condition...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.