如何使用 VBA 将 Access 表中的文本字段转换为富文本备忘录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20828561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:08:14  来源:igfitidea点击:

How to convert a text field in an Access table to a rich text memo using VBA

sqlvbams-accessaccess-vbams-access-2010

提问by ColeValleyGirl

My users have a number of backend .accdb databases (which I can't access directly). I need to code some vba to modify the structure of some of the tables in these databases to convert Text Fields to Rich Text memos. (The fields already contain text including Access "rich-text" i.e. the relevant html coding).

我的用户有许多后端 .accdb 数据库(我无法直接访问)。我需要编写一些 vba 代码来修改这些数据库中某些表的结构,以将文本字段转换为富文本备忘录。(这些字段已经包含文本,包括访问“富文本”,即相关的 html 编码)。

I need to:

我需要:

  1. Modify the field to be a rich text memo.
  2. Modify the existing contents (if applicable) to display correctly as Access rich text in forms, datasheets and reports.
  1. 将该字段修改为富文本备忘录。
  2. 修改现有内容(如果适用)以在表单、数据表和报告中正确显示为 Access 富文本。

I can write a SQl statement that will modify a field from TEXT (255) to MEMO:

我可以编写一个 SQL 语句,将一个字段从 TEXT (255) 修改为 MEMO:

ALTER TABLE tblSource ALTER COLUMN Detail1 MEMO

However, this leaves the resultant memo field as a plain text memo.

但是,这会将结果备忘录字段保留为纯文本备忘录。

I have considered creating a new Rich Text Field and then copying the contents of the old one (using a SQL CREATE TABLE statement followed by an UPDATE statement that applies the Plaintext function to the contents of the old field and then copies the result to the new field, and then further SQl to delete the old field and rename the new) but can't find out how to create a rich-text memo (default seems to be plain text).

我考虑过创建一个新的富文本字段,然后复制旧字段的内容(使用 SQL CREATE TABLE 语句后跟一个 UPDATE 语句,该语句将纯文本函数应用于旧字段的内容,然后将结果复制到新字段字段,然后进一步 SQl 删除旧字段并重命名新字段)但无法找到如何创建富文本备忘录(默认似乎是纯文本)。

Extensive web searches haven't shown up any additional techniques I can deploy. This is a process that will be run once for each file, so it doesn't need to be elegant or quick but it does need to be bomb-proof!

广泛的网络搜索没有显示我可以部署的任何其他技术。这是一个将为每个文件运行一次的过程,因此它不需要优雅或快速,但它确实需要防弹!

回答by HansUp

Since Rich Textis not a datatype and is not a field property which can be defined or modified with a SQL statement, you will need VBA to set the field's TextFormatproperty.

由于富文本不是数据类型,也不是可以用 SQL 语句定义或修改的字段属性,因此您需要 VBA 来设置字段的TextFormat属性。

You can adapt techniques from this code sample.

您可以修改此代码示例中的技术。

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Set db = CurrentDb
Set tdf = db.TableDefs("Table1")
Set fld = tdf.Fields("memo_fld")
Debug.Print "acTextFormatPlain: " & acTextFormatPlain & _
    "; acTextFormatHTMLRichText: " & acTextFormatHTMLRichText
With fld.Properties("TextFormat")
    Debug.Print "TextFormat: " & .Value
    If .Value = acTextFormatPlain Then
        .Value = acTextFormatHTMLRichText
        Debug.Print "TextFormat changed to: " & .Value
    End If
End With

Note that code is run from the database which contains the target table. If Table1was actually a link to a table in another Access db file, the code would fail.

请注意,代码是从包含目标表的数据库中运行的。如果Table1实际上是指向另一个 Access db 文件中的表的链接,则代码将失败。

Note also that only applies to a memo field. The TextFormatproperty is not created for regular text datatype fields, so this will throw error #3270, "Property not found."

另请注意,这仅适用于备注字段。该的TextFormat不是为普通的文本数据类型字段创建属性,因此这将引发错误#3270,“找不到属性”。

Debug.Print tdf.Fields("some_text").Properties("TextFormat").Value

Since you will be converting regular text fields to memo fields, that point is probably not a concern. I mentioned it only in case you stumble into it.

由于您要将常规文本字段转换为备注字段,因此这一点可能不是问题。我提到它只是以防万一你偶然发现它。

ColeValleyGirl discovered the TextFormatproperty is not always created for a new memo field.

ColeValleyGirl 发现并不总是为新的备注字段创建TextFormat属性。