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

前端 未结 1 1592
猫巷女王i
猫巷女王i 2020-12-07 05:29

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

相关标签:
1条回答
  • 2020-12-07 05:52

    Since Rich Text is 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 TextFormat property.

    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 Table1 was actually a link to a table in another Access db file, the code would fail.

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

    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 TextFormat property is not always created for a new memo field.

    0 讨论(0)
提交回复
热议问题