1

in an Access database we can set Text Format attribute of a Long Text column to either Plain Text or Rich Text.

How can we add a Rich text column to a table programmatically in C# using OleDb API?

mohas
  • 1,911
  • 1
  • 16
  • 20
  • possible duplicate of [How to convert a text field in an Access table to a rich text memo using VBA](http://stackoverflow.com/questions/20828561/how-to-convert-a-text-field-in-an-access-table-to-a-rich-text-memo-using-vba) – Gerold Broser Aug 03 '15 at 17:37
  • edited my question to be more specific – mohas Aug 03 '15 at 17:42

2 Answers2

1

How can we add a Rich text column to a table programmatically in C# using OleDb API?

We can't do it using OleDb. We need to use Access DAO for that:

// required COM reference: Microsoft Office 14.0 Access Database Engine Object Library
//
// using Microsoft.Office.Interop.Access.Dao; ...
var dbe = new DBEngine();
Database db = dbe.OpenDatabase(@"C:\Users\Public\Database1.accdb");
TableDef tbd = db.TableDefs["MyTable"];  // existing table
var fld = new Field();
fld.Name = "MyRichMemo";
fld.Type = (short) DataTypeEnum.dbMemo;
tbd.Fields.Append(fld);
// now set "Text Format" property to "Rich Text"
fld.Properties.Append(fld.CreateProperty("TextFormat", DataTypeEnum.dbByte, 1));
db.Close();
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • Does this approach also works with access 2003 (.mdb) files? What about Microsoft OLE Provider for Jet 4.0? – mohas Aug 04 '15 at 05:14
  • 1
    @mohas - Yes, the same code will work with an Access_2003-format .mdb file. As for `Microsoft.Jet.OLEDB.4.0`, it won't help you; you need to use Access DAO, – Gord Thompson Aug 04 '15 at 14:51
-1

Answering the question: "How to add a rich text column to an Access table".

Access 2010 and above allows rich text to be saved in a memo. Below is the c# OleDB code I have used to add a memo column to an Access table.

public OleDbConnection myAccessConnection = new OleDbConnection();

myAccessConnection.ConnectionString = 
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\myAccessDb.accdb";

OleDbCommand sqlCommand = new OleDbCommand();

myAccessConnection.Open();
sqlCommand.Connection = myAccessConnection;

sqlCommand.CommandText = "ALTER TABLE [MyTableInMyAccessDb] ADD MyMemo MEMO";

sqlCommand.ExecuteNonQuery();

myAccessConnection.Close();
Bobby
  • 137
  • 11
  • Not sure why someone would down vote this answer. I created this answer from code that runs successfully in an application I developed. I'd be grateful for any helpful feedback. – Bobby Aug 04 '17 at 21:16
  • It looks as if the answer you provided wasn't relevant to the question asked. The OP wanted to know how to create a long text/memo field with the property of holding "rich text". The result of your code would simply add a long memo field of subtype "plain text". That said, your answer isn't wrong, nor does does it do any harm, so I would see no reason to actively vote it down. – Erik Midtskogen Jan 26 '22 at 19:09