# Connecting to Databases

> Save and load TXTextControl documents as RTF in a database text field with SaveToMemory, LoadFromMemory and DAO in Visual Basic 6, and pick a compact format.

- **Product:** TX Text Control ActiveX
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control ActiveX > Mail Merge > Connecting to Databases
- **1011 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-activex/mail-merge/databases/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-activex/mail-merge/databases/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-activex/mail-merge/databases/llms-full.txt

---

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.](https://docs.textcontrol.com/assets/img/tx-text-control-activex/database-sample-rtf-source.df32b5fe.webp)

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](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/savetomemory-method/llms-full.txt) and [LoadFromMemory](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/loadfrommemory-method/llms-full.txt). `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:

```vbscript
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](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/save-method/llms-full.txt) 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](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/text-property/llms-full.txt) property:

```vbscript
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](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/refresh-method/llms-full.txt) 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

- [Mail Merge](https://docs.textcontrol.com/tx-text-control-activex/mail-merge/llms-full.txt): insert database fields at defined positions of a template instead of storing the whole document.
- [Working with Files](https://docs.textcontrol.com/tx-text-control-activex/document-editor/load-save/llms-full.txt): load and save documents in files.
- [Dynamic Template Generation](https://docs.textcontrol.com/tx-text-control-activex/mail-merge/dynamic-templates/llms-full.txt): the last part of the mail merge sample.

---

## Nearby

- **Previous:** [Dynamic Template Generation](https://docs.textcontrol.com/tx-text-control-activex/mail-merge/dynamic-templates/llms-full.txt)
- **Next:** [PDF Export](https://docs.textcontrol.com/tx-text-control-activex/pdf/llms-full.txt)
