Microsoft KB Archive/148531

= ACC95: How to Bind a RichTextBox Control to a Field in a Table =

Article ID: 148531

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 95 Standard Edition

-



This article was previously published under Q148531



Advanced: Requires expert coding, interoperability, and multiuser skills.



SUMMARY
The Rich TextBox ActiveX control which is available with the Microsoft Access Developer's Toolkit version 7.0 has no built-in property to bind its contents to a field in the form's record source. However, you can use Visual Basic for Applications to achieve the same result.

NOTE: The RichTextBox ActiveX control supplied with Microsoft Office 97, Developer Edition has a ControlSource property which can be used to directly bind its contents to a field in the form's record source. Although the technique described in this article will work with the Microsoft Office 97, Developer Edition Rich TextBox ActiveX control, we recommend that you use its ControlSource property instead.

As you enter text in a RichTextBox ActiveX control, the formatting and the text you enter is stored in the control's TextRTF property. This information is stored in Rich Text Format (RTF). Using Visual Basic for Applications, you can store the contents of the TextRTF property in a Memo field in the underlying table.

NOTE: It is important to remember that you cannot view the form in Datasheet view because the contents of the TextRTF property is stored in the table. Viewing the data sheet of the form will generate a run-time error.

This article shows the procedures that you can use to store the contents of the Rich TextBox ActiveX control in the form's underlying record. This simulates the ControlSource property that other controls have. The procedures shown deal specifically with saving the contents of the Rich TextBox ActiveX control, and does not touch on other Rich Text Format issues covered in other articles (including controls that are used to manipulate the Rich Text Format settings).



MORE INFORMATION
To store the contents of the Rich TextBox ActiveX control in the form's underlying record, follow these steps:  Create the following table:

Table: Table1

-

Field Name: MemoForRTF

Data Type : Memo

 Create the following form:

Form: Test1

---

RecordSource: Table1

KeyPreview: YES

Rich Textbox ActiveX Control:

Name: OLERichTextbox

Text Box:

Name: MemoForRTF

ControlSource: MemoForRTF

  On the View menu, click Code, and type the following statements in the Declarations section of the form module if they are not already there: Option Compare Database Option Explicit

' Constant that stores the name of the Memo control on the form. Const strMemoName As String = "MemoForRTF"

' Constant that stores the name of the RTF control on the form. Const strRTFName As String = "OLERichTextbox"

' Variable to note if the RTFText was just loaded (no changes made). Dim intJustLoaded As Integer

' Variable to note if the RTFText has been changed by the user. Dim intRTFChanged As Integer   Type the following procedures in the form module: Private Sub Form_BeforeUpdate(Cancel As Integer) ' Before record is saved, put RTF control's text in the Memo ' field. Me(strMemoName) = Me(strRTFName).TextRTF End Sub

Private Sub Form_Current ' When arriving at a record, fill RTF control with Memo field ' text. Call PlaceMemoInRTF End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) ' This checks that the any changes to the RTF text is not lost ' accidentally when the user presses the ESC key. ' Make sure that the form's Key Preview event is set to Yes. If KeyCode = 27 And Shift = 0 Then If Not Me.Dirty And intRTFChanged Then Call AskToRestoreFromMemo End If        End If      End Sub

Private Sub OLERichTextbox_Change ' When RTF text is changed, dirty record so it will be saved. If intJustLoaded Then intJustLoaded = False Exit Sub End If     intRTFChanged = True If Me.Dirty = True Then Exit Sub End If     ' Otherwise, dirty the record to save changes. Me(strMemoName) = Me(strMemoName) End Sub

Public Sub PlaceMemoInRTF ' Place saved RTF in Memo field into RTF control. intRTFChanged = False If Me.NewRecord Then Me(strRTFName).TextRTF = "" ElseIf IsNull(Me(strMemoName)) Then Me(strRTFName).TextRTF = "" Else ' There is saved RTF text in the Memo field. intJustLoaded = True Me(strRTFName).TextRTF = Me(strMemoName) ' The RTF control's Change event fires here; that is where the ' intJustLoaded comes in. The flag tells the change event ' not to dirty the record. The RTF was just loaded, and no          ' changes have been made. intJustLoaded = False End If     End Sub

Private Sub OLERichTextbox_KeyDown(KeyCode As Integer, _        ByVal Shift As Integer) ' If user pressed ESC, then ask if user wants to discard changes. If KeyCode = 27 And Shift = 0 Then If intRTFChanged Then Call AskToRestoreFromMemo End If        End If      End Sub

Public Sub AskToRestoreFromMemo ' Ask if the user wants to discard the changes to the RTF text. ' If so, overwrite the RTF text in the RTF control with what is in     ' the Memo field. Dim intResponse As Integer Dim strMessage As String strMessage = Left(Me(strRTFName).Text, 20) If strMessage = "" Then strMessage = "Do you want to discard changes to the text." Else strMessage = "Do you want to discard changes to the text " & _ """" & strMessage & "..." & """"        End If      intResponse = MsgBox(strMessage, 4, "Undo your work?") If intResponse = 6 Then Call PlaceMemoInRTF Else Me(strMemoName) = Me(strMemoName) 'Dirty record so text will 'save. End If     End Sub  Compile and save all modules. If you receive any errors, troubleshoot them. Open the form in Form view. Test the RichTextBox Control by entering text. The Rich TextBox ActiveX control should save your text in each record.</li></ol>

<div class="references_section">