ACC: Sample Function to Replace Special Characters

ID: Q109825

The information in this article applies to:

SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

When you import a text file that contains tabs or other special characters into Microsoft Access, the special characters are converted and displayed as boxes. You cannot use the Find and Replace commands to find these converted characters. This article describes a sample Visual Basic for Applications procedure that you can use to find and replace the converted tabs and other special characters.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x or the "Building Applications" manual in Microsoft Access version 2.0.

MORE INFORMATION

To find and replace converted special characters, follow these steps:

1. Create a module and type the following lines in the Declarations

   section:

      Option Compare Binary
         ' Otherwise, the function will replace spaces with percent signs.
      Option Explicit

2. Type the following procedures:

      '============================================================
      'The following function will:
      ' - Find the tabs in a Text or Memo field.
      ' - Call another function to replace the tabs.
      '============================================================

      Function FindTabs (WhichField As String) as String
         Dim x As Integer, Text As String
         Dim start As Integer
            start = 1
            x = 1
            Text = WhichField

            Do Until x = 0
               ' Chr(9) is the Tab character.
               ' Replace Chr(9) with the ANSI code for the character
               ' you are searching for.
               x = InStr(start, Text, Chr(9))
               start = x + 1
               If x > 0 And Not IsNull(x) Then
                  Text = ReplaceTabs(x, Text)
               End If
            Loop

            FindTabs = Text
      End Function

      '==================================================================
      ' The following function is called from the FindTabs() function. It
      ' accepts two arguments, text and start. The function replaces tabs
      ' with %. It returns the updated text.
      '==================================================================

      Function ReplaceTabs(start As Integer, text As String) As String
         ' Replace % with the character you want to substitute.
         Mid(text, start, 1) = "%"
         ReplaceTabs = text
      End Function

3. Create a query based on the table to which the text file was imported.

4. Add the field containing the special characters to the Query Design

   grid.

5. Convert the query to an update query by clicking Update Query (or
   Update in version 1.x, 2.0, or 7.0) on the Query menu.

6. Add the following to the Update To row for the field(s) containing the
   special characters

      FindTabs(<[fieldname]>)

   where <[fieldname]> is the name of the field containing the special
   characters.

7. Run the query.

REFERENCES

For more information about ANSI characters, search for "ANSI" using the Microsoft Access 97 Help Index.

Additional query words: find/replace recognize

Keywords          : kbprg kbusage
Version           : 1.0 1.1 2.0 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 20, 1998