TX Text Control ActiveX

Connecting to Databases

Topic

TX Text Control can be connected to any type of database and store formatted text in any of its text formats. You can save a complete document as a record, or load text from a database into table cells and Marked Text Fields. Mail merge, report generators, formatted data entry masks and all kinds of business applications that create documents from information stored in a database are built this way.

The Visual Basic sample below saves the contents of a TXTextControl to a database and reads them back. The data is stored as an RTF formatted string in a text field. The source code of the sample program is in the Samples\vb6\DataAccess sample source directory.

The Sample Program

The sample program is a form with two TXTextControl controls, which both display the same record of a database. The upper control displays it as formatted text. The lower control shows the RTF source code of the record, for demonstration purposes.

The database sample program with the record as formatted text in the upper TXTextControl and its RTF source code in the lower one.

Clicking the Next button saves the changes you have made in the upper window and advances to the next record.

Save a Record

Two methods handle all data access: SaveToMemory and LoadFromMemory. SaveToMemory stores formatted text in a byte array, which is then passed on to the database engine with the DAO or ADO functions of Visual Basic. The following code uses DAO syntax:

Private Sub PutRecord()
   Dim ba() As Byte, s As String
   ba = TXTextControl1.SaveToMemory(5, 0)
   ' convert from ANSI to Unicode
   s = StrConv(ba, vbUnicode)
   rs.Edit
   rs("RTF") = s
   rs.Update
End Sub

The code works as follows:

  • SaveToMemory returns the document as RTF, the format 5, in a byte array.
  • StrConv converts the data from ANSI to Unicode.
  • The string is written to the RTF field of the current record of the recordset rs.

To use another text format, change the first parameter of SaveToMemory. The format identifiers are listed with the Save method.

Retrieve a Record

To retrieve a record from the database and display it, the data is converted from Unicode to ANSI and then passed on to the LoadFromMemory method of the first TXTextControl. The second TXTextControl displays the RTF source code without any formatting, so the sample loads it with the Text property:

Private Sub GetRecord()
   Dim ba() As Byte, s As String
   ' Load RTF data and convert it to a byte array
   s = rs("RTF")
   ba = StrConv(s, vbFromUnicode)
   ' Display RTF data in Text Control 1
   TXTextControl1.LoadFromMemory ba, 5, 0
   TXTextControl1.Refresh
   ' Display RTF source code in Text Control 2
   TXTextControl2.Text = s
End Sub

The code works as follows:

  • The RTF string is read from the RTF field of the record, and StrConv converts it to an ANSI byte array.
  • LoadFromMemory loads the byte array as RTF into TXTextControl1, and Refresh repaints the control.
  • The Text property of TXTextControl2 receives the RTF source code as plain text.

Choose the Text Format

The sample uses RTF, which most word processing programs can read. If that doesn't matter for your application, the binary format of TX Text Control may be a better choice: it is more compact and takes up less space in a database.

Next Steps