jonesgj
asked on
Store and retrieve Rich text to and from an access database.
I need to store and retrieve Rich text from a rich text box control to and from an access database.
I am using VB .NET 2003, Access 2002 (also looking at VB2005)
I run into problems when I try to use an SQL INSERT statement to take the content of the RTB into the column. I have code which strips out characters such as commas and this works to store plain text. Obviously, with Rich text, there are even more control/formatting characters.
Is there an easy soluton? Is it possible?
Thanks
Jonesgj
I am using VB .NET 2003, Access 2002 (also looking at VB2005)
I run into problems when I try to use an SQL INSERT statement to take the content of the RTB into the column. I have code which strips out characters such as commas and this works to store plain text. Obviously, with Rich text, there are even more control/formatting characters.
Is there an easy soluton? Is it possible?
Thanks
Jonesgj
ASKER
Hi razor,
I am trying to save the text and its formating. Up til now I have just been required to store the simple plain text held in my text box. Now, however, I need to use a rich text box instead of a text box because I need to apply some kind of formatting to the text. This formatting needs to be retained and held in the database, and recalled from the database into a rich text box when needed formatting intact.
Sending the RTB.rtf to the db fails.
Thanks in anticipation
Jonesgj
I am trying to save the text and its formating. Up til now I have just been required to store the simple plain text held in my text box. Now, however, I need to use a rich text box instead of a text box because I need to apply some kind of formatting to the text. This formatting needs to be retained and held in the database, and recalled from the database into a rich text box when needed formatting intact.
Sending the RTB.rtf to the db fails.
Thanks in anticipation
Jonesgj
>>
Sending the RTB.rtf to the db fails.
<<
It works OK for me, with a relatively simple databinding, provided the set-up is right.
1) You will probably need to use a Memo field in the Access Table, because the text that does the formatting takes up a lot of space.
2) The databinding (which has to be done in code, because the .rtf property doesn't appear in the "Advanced bindings" drop down in the RichTextBox's property window) should be on the following lines
rtb1.DataBindings.Add("rtf ", ds.Tables(0), "MyText")
3) The biggest problem is likely to be that for this approach to work the relevant field's data in the Access Table has ALREADY to be in rtf format. That is, if you want to bind to the rtb's .rtf property, you can't start off with plain text in the Access Table and then, via the binding, save it in .rtf format. But what you could do, as a one-off initial exercise to get the Access Table into this state, is fill your VB.NET datatable with plain text from Access then cycle through the rows loading the relevant field into the rtb as .Text and then resave to the datatable as .rtf, and then update the Access Table. If you do that - I probably don't need to say this ;-) - you ought to backup the Access Table first, in case anything goes wrong.
Roger
Sending the RTB.rtf to the db fails.
<<
It works OK for me, with a relatively simple databinding, provided the set-up is right.
1) You will probably need to use a Memo field in the Access Table, because the text that does the formatting takes up a lot of space.
2) The databinding (which has to be done in code, because the .rtf property doesn't appear in the "Advanced bindings" drop down in the RichTextBox's property window) should be on the following lines
rtb1.DataBindings.Add("rtf
3) The biggest problem is likely to be that for this approach to work the relevant field's data in the Access Table has ALREADY to be in rtf format. That is, if you want to bind to the rtb's .rtf property, you can't start off with plain text in the Access Table and then, via the binding, save it in .rtf format. But what you could do, as a one-off initial exercise to get the Access Table into this state, is fill your VB.NET datatable with plain text from Access then cycle through the rows loading the relevant field into the rtb as .Text and then resave to the datatable as .rtf, and then update the Access Table. If you do that - I probably don't need to say this ;-) - you ought to backup the Access Table first, in case anything goes wrong.
Roger
ASKER
Thanks Roger,
I am using the following:
MySQL = ("UPDATE Content SET " & _
"ContentName = '" & txtName.Text & "', " & _
"ContentText = '" & rtbNotes.Rtf & "' " & _
"WHERE ContentName = '" & AppSettings.CurrentItem & "' ")
rtbNotes.Text = Replace(rtbNotes.Text, Chr(124), Chr(34))
rtbNotes.Text = Replace(rtbNotes.Text, Chr(42), Chr(39))
rtbNotes.Text = Replace(rtbNotes.Text, Chr(17), Chr(43))
Call ExecuteQuery(MySQL)
I am using the following:
MySQL = ("UPDATE Content SET " & _
"ContentName = '" & txtName.Text & "', " & _
"ContentText = '" & rtbNotes.Rtf & "' " & _
"WHERE ContentName = '" & AppSettings.CurrentItem & "' ")
rtbNotes.Text = Replace(rtbNotes.Text, Chr(124), Chr(34))
rtbNotes.Text = Replace(rtbNotes.Text, Chr(42), Chr(39))
rtbNotes.Text = Replace(rtbNotes.Text, Chr(17), Chr(43))
Call ExecuteQuery(MySQL)
ASKER
Sorry the code above needs a little explanation.
This returns an error 'syntax error in SQL' because of characters in the rtf.
Ignore the replace function lines, as they are just putting back the commas removed before the value of rtb was passed to the SQL.
My thoughts were to, somehow, substitute all the characters (may be to their ascii values) with values which would not split the SQL, before passing it to the insert statement, so the SQL string would be 'clean'.
Before attempting this, I was looking for an easier way to do it.
I am not binding any of my controls.
This returns an error 'syntax error in SQL' because of characters in the rtf.
Ignore the replace function lines, as they are just putting back the commas removed before the value of rtb was passed to the SQL.
My thoughts were to, somehow, substitute all the characters (may be to their ascii values) with values which would not split the SQL, before passing it to the insert statement, so the SQL string would be 'clean'.
Before attempting this, I was looking for an easier way to do it.
I am not binding any of my controls.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Roger,
You code is what I started with. A simple Insert with one exception: I didn't know about the rtb's .rtf property returning a null-terminated string.
I have placed your code into my project with 1 or 2 tweaks and it works!
I had spent 45minutes this afternoon creating an encode and decode ASCII to RTF, just before your comment appeared, and that seemed to work, but was quite slow.
Anyway. Thanks for the solution
Kind regards
Jonesgj
You code is what I started with. A simple Insert with one exception: I didn't know about the rtb's .rtf property returning a null-terminated string.
I have placed your code into my project with 1 or 2 tweaks and it works!
I had spent 45minutes this afternoon creating an encode and decode ASCII to RTF, just before your comment appeared, and that seemed to work, but was quite slow.
Anyway. Thanks for the solution
Kind regards
Jonesgj
if that is what you want, why not build a regex to remove all formating other than plain text, and store the new string
if you want to save the RTF, just save the string value of the RTbox into the db