Article ID: 148531
Article Last Modified on 10/11/2006
Table: Table1
---------------------
Field Name: MemoForRTF
Data Type : Memo
Form: Test1
-----------------------
RecordSource: Table1
KeyPreview: YES
Rich Textbox ActiveX Control:
Name: OLERichTextbox
Text Box:
Name: MemoForRTF
ControlSource: MemoForRTF
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
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
Additional query words: ADT, store, bound
Keywords: kbhowto kbinterop kbprogramming KB148531