locked
Cannot create a field with type dbChar in CDaoTableDef RRS feed

  • Question

  • Hi,

    I try to create a field with type dbChar, as below:

    CDaoDatabase MyDB;

    MyDB.Create(_T("E:\\Temp\\Test.mdb"));

    CDaoTableDef TableDef(&MyDB);

    TableDef.Create(_T("TestTable"));

    TableDef.CreateField(_T("Field1"), dbChar, 20);

    TableDef.Append();

    TableDef.Close();

    MyDB.Close();

    However, the codeline

            TableDef.CreateField(_T("Field1"), dbChar, 20);

    will raise an exception of error code 3259, the description said "Invalid field data type". Why?

    Sunday, July 28, 2019 7:56 AM

All replies

  • As far as I know, ACCESS does not allow custom Data Types in tables, so it gives you an error telling you that. You need to select a valid Data Type ACCESS provides.
    Sunday, July 28, 2019 1:08 PM
  • As far as I know, ACCESS does not allow custom Data Types in tables, so it gives you an error telling you that. You need to select a valid Data Type ACCESS provides.

    Thank you for your reply.

    Sorry but dbChar is not a custom data type, but a built-in data type in Access.

    Sunday, July 28, 2019 11:24 PM
  • Although dbChar ist listed in the DAO.DataTypeEnum, you cannot create a field with this type. Access data types can be very confusing. There is a good explanation in this bytecomb blog article.  

    It looks like you are trying to create a fixed-width text field. You could try it like this in VBA (you'll have to translate this to C/C++ yourself...)

    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Dim fld  As DAO.Field
    
    Set db = CurrentDb
    
    Set tdf = db.CreateTableDef("ztbl_Test")
    ' Set fld = tdf.CreateField("TestChar", dbChar, 20) does not work
    Set fld = tdf.CreateField("TestChar", dbText, 20)
    fld.Attributes = fld.Attributes Or dbFixedField
    Call tdf.Fields.Append(fld)
    Call tdf.Fields.Refresh
    
    Call db.TableDefs.Append(tdf)
    Call db.TableDefs.Refresh
    


    Matthias Kläy, Kläy Computing AG




    • Edited by mklaey Monday, July 29, 2019 8:11 AM expanded
    Monday, July 29, 2019 7:30 AM
  • Hi tempc,

    To add more, "dbChar" according to others, it will work with Linked-Tables but not Access Tables (not sure about it, most likely SQL Database Linked-Tables rather than Access Linked-Tables).

    "dbChar" as far as I know, is use for SQL Database Servers. "dbText" is the correct type to use in Access Tables.

    Tuesday, July 30, 2019 1:00 AM