Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Subscript text in a RichText Box

Status
Not open for further replies.

scidb

Technical User
Dec 30, 2004
20
US
Hello All,

I need help with a RichTextBox Control. I have an MS Access report that has a RichTextBox control. The control gets its data from a field in the database that contains chemical formulas, e.g. H2O. What I need to do is search the RichTextBox for any numbers and make them subscripts, in H2O the "2" would be subsripted.
I have been doing some searching for VB code samples to help me do this. If I understand correctly I would need to programatically select the numbers and use the SelectCharOffset Property. Does anyone have any experience with this, I am a complete newbie to VB.

Any help would be greatly appreciated! Thanks
 
Sheco,

It didn't give the same error message! But now I get a Microsoft Visual Basic Run-time '424'; Object required error message and the debug routine highlights the line
RichTextBox1.Text = Text1.Text
 
erm, mmm that will be hard to diagnose without actually seeing your report...

Given the line above, and the error message.... My guess is that you either don't have anything on your form named Text1 or you don't have anything named RichTextBox1 ??

Perhaps did you rename these things?

orrrr, it could be that you DO have them but since this is the Report_Open() event that they have not actually been created yet as objects in memory at the time that the event fires.
 
Oh, it looks like from some of your code way up at the top that maybe your rich text box has a different name.

Perhaps it is named rtb1 instead of RichTextBox1 ???
 
Sheco,

My rich text box name was originally rtb1, but I changed it to match strongm's code. Now my rtb name is "RichTextBox1", but I don't have anything called Text1. What is that line of code doing anyway? The line:
RichTextBox1.Text = Text1.Text comes from strongm.

I guess we may have to give up! Thanks for your persistance.
 
Well I don't know what that Text1 thing was all about. Perhaps strongm made a little sample program to test his theory and then just did a cut-n-paste and it is a remnent from his sample.

Anyway, just looking back at your code near the top I see this:
rtb1.Text = [MF] 'This is the field name for the string


So maybe you want:

RichTextBox1.Text = [MF]

Or maybe, if your RTB already contains the value of interest then just get rid of that line altogether.


 
Yes, my habit isn't generally to give full solutions, just examples about how something might be done (note, for example, that there is no error checking either). I try to leave a little bit of work to the person who asked the original question
 
Sheco/strongm,

I am still working to implement all of your suggestions and I just wanted to say thank you for your time and willingness to help!

 
strongm and/or anyone that can help

I have been working with you suggested code, but I am still having trouble implementing it. I want to use it in an Access Report, but there is a bug with ActiveX controls where you can't see the rich textbox content in print preview mode, so I am using a MS Access form to test it.

I have had some partial success, but at present the entire string is being reduced in size and not just the mymatches. Here is the code as I have it now:

Private Sub Form_Load()

Dim re As RegExp
Dim mymatch As Match
Dim mymatches As MatchCollection


Set re = New RegExp

re.Global = True
re.Pattern = "\d"

Set mymatches = re.Execute(rtb.Text)

For Each mymatch In mymatches
MsgBox "My Match is " & mymatch, vbInformation, "Current Match"
With rtb
.SelStart = mymatch.FirstIndex
.SelLength = 1
.SelCharOffset = -20
.SelFontSize = .SelFontSize * 2 / 3
End With

Next

End Sub

Iam not sure what is going wrong, but it seems to be in the With...End With section. The entire string's font size is being reduced and not just the numbers.
An example of the string is: C10H13NO•2C3H6O

 
Sorry, I submitted before I finished. What I need to do is subscript the numbers in red, but the number "2" in black needs to be left full size. These exceptions to the subscripting would always occur after the "•". Can you qualify a VB script regular expression to not include some things? Any help would be appreciated! Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top