Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
Syndicate this content

Using databases with TX Text Control - Part I

This source code snippet requires TX Text Control ActiveX
Author:TX Text Control Support Department
Language:Visual Basic
Version:1.1
Released:March 20, 2001
Last modified:January 11, 2008
Requirements:TX Text Control ActiveX with Visual Basic
Download code:into_report.zip Download [5.3 KB, ZIP]

TX Text Control can be used in bound mode in Visual Basic (Delphi does not provide this feature), but this is not always the way to go. For example: You want to built an application that has a mail merge feature, but you do not want TX Text Control to replace the entire contents of the document with a field from a database. You need to replace only a certain piece of text or a marked text field with text from a database. This weeks Tip of the week shows how to do that.

  1. Private Sub GetRecord()
  2. Dim ba() As Byte, s As String
  3.  
  4. If IsNull(rs("RTF")) Then
  5. TXTextControl1 = ""
  6. TXTextControl2 = ""
  7. Else
  8. ' Load RTF data and convert it to a byte array
  9. s = rs("RTF")
  10. ba = StrConv(s, vbFromUnicode)
  11. ' Display RTF data in TX Text Control 1
  12. TXTextControl1.LoadFromMemory ba, 5, False
  13. TXTextControl1.Refresh
  14. ' Display RTF source code in TX Text Control 2
  15. TXTextControl2.Text = s
  16. End If
  17. End Sub
  18.  
  19. '
  20. ' PutRecord
  21. '
  22. ' Save contents of TX Text Control to database
  23. '
  24. Private Sub PutRecord()
  25. Dim ba() As Byte, s As String
  26.  
  27. ba = TXTextControl1.SaveToMemory(5, 0)
  28. s = StrConv(ba, vbUnicode)
  29.  
  30. rs.Edit
  31. rs("RTF") = s
  32. rs.Update
  33. End Sub

The two methods above are from the DataAccess sample, which you can download from our FTP server. The variable rs is a usual VB RecordSet. This example uses RTF rather than the internal TX format. GetRecord reads a field from the database and displays it in a TX Text Control. First the field 'RTF' is read into a string. Then StrConv is used to convert the string to a byte array (note the UniCode support) and then the byte array is loaded into the TX using LoadFromMemory.

If you do not want to replace the entire contents of the TX with the text from the database you simply have to specify 'True' for the last parameter. Then only the current selection is replaced. PutRecord works just the other way round and uses to SaveToMemory and StrConv to complete the task.

External verification page for ISO 9000:2000 certificate
ISO 9001:2000
certified