Microsoft KB Archive/190522

= PRB: Error Setting Field Format Property of Access Tables =

Article ID: 190522

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q190522



SYMPTOMS
From Visual Basic, when trying to specify a field Format property of an Access table (for example, field type DateTime) from blank to "Long Date" format, the following error occurs:

Run-time error '3421':

Data type conversion error.



CAUSE
The property data type constant should always be "dbText." If other data type constants, such as dbDate or dbNumeric are specified, you will encounter the error described above.



STATUS
This behavior is by design.



MORE INFORMATION
The following Visual Basic 5.0 example creates a new field object MyNewDate with data type dbDate, and appends it to the Fields collection. It then specifies the Format property with the value "Long Date." Note that the Format property is specified as "dbDate," which is incorrect. To correct the problem, you should change: Set prp = fld.CreateProperty("Format", dbDate) to: Set prp = fld.CreateProperty("Format", dbText)

Steps to Reproduce Behavior
 Start a "Standard EXE" project in Visual Basic. Form1 is created by default. From the Project menu, select References, and then place a check next to Microsoft DAO 3.5 Object Library.

In Visual Basic 4.0, you can reference Microsoft DAO Object Library from Tools menu, then select References. Add a CommandButton control, Command1, to Form1.  Paste the following code in the Command1_Click code snippet: Dim db As Database Dim tdf As TableDef Dim fld As Field Dim prp As Property Set db = OpenDatabase("Nwind.mdb") Set tdf = db.TableDefs("Employees") tdf.Fields.Append tdf.CreateField("MyNewDate", dbDate) 'Error line Set fld = tdf.Fields("MyNewDate") Set prp = fld.CreateProperty("Format", dbDate) prp.Value = "Long Date" fld.Properties.Append prp 

