How to Allow null fields in ADO

I keep getting "the field cannot contain a null value because the required property for this field is set to true..." when I try and open a newly created database.  Any way to allow for null fields? or at least empty ones?

Public Sub NewDatabase()

Dim cat As New ADOX.Catalog
Dim tbl As New ADOX.Table

Userfile = dbPath & "TEMP" & FileNo & ".MDB"

cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                       "Data Source=" & Userfile & ";" & _
                       "Jet OLEDB:Engine Type=4;"

cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Userfile

With tbl
    .Name = "OMWII"
   .Columns.Append "StockName", adVarWChar, 40
   .Columns.Append "StockSymbol", adVarWChar, 6
   .Columns.Append "StockPrice", adSingle

end with
cat.Tables.Append tbl

Set tbl = Nothing
Set cat = Nothing

Screen.MousePointer = vbNormal


End Sub

Public Sub Connect2Database()

Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset

cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& Userfile & ";Persist Security Info=False"

rst.Open "SELECT * FROM OMWII", cnn, adOpenStatic, adLockOptimistic

'For i = 0 To rst.Fields.Count - 1
'     rst.Fields(i).Value = 0
'Next i

'For Each fld In rst.Fields
'  Debug.Print fld.Name & " = " & fld.Value

        If rst.EOF Then rst.MoveLast 'if moved to EOF, go to last record so don't get error.


Screen.MousePointer = vbNormal

End Sub
Who is Participating?
Jeremy_DConnect With a Mentor Commented:
You can't do this with the Columns.Append method unless you create a correct Column object first, like so:

    Dim colTemp As New ADOX.Column

    colTemp.Name = "StockName"
    colTemp.Type = adVarWChar
    colTemp.DefinedSize = 40
    'Next line does the trick
    colTemp.Attributes = adColNullable
    'Now add it to your table
    tbl.Columns.Append colTemp

Do this for all your columns and it should work fine.
The following code uses the properties of the column object to create provider specific column types. Ihave tried used this to set the column to allow nulls.

see:ParentCatalog Property Example (Visual Basic)

Sub CreateAutoIncrColumn()

   Dim cnn As New ADODB.Connection
   Dim cat As New ADOX.Catalog
   Dim tbl As New ADOX.Table

   cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source= c:\Program Files\" & _
      "Microsoft Office\Office\Samples\Northwind.mdb;"
   Set cat.ActiveConnection = cnn

   With tbl
      .Name = "MyContacts"
      Set .ParentCatalog = cat
      ' Create fields and append them to the new Table object.
      .Columns.Append "ContactId", adInteger

      ' Make the ContactId column and auto incrementing column
("AutoIncrement") = True

      .Columns.Append "CustomerID", adVarWChar
      .Columns.Append "FirstName", adVarWChar
      .Columns.Append "LastName", adVarWChar
      .Columns.Append "Phone", adVarWChar, 20
      .Columns.Append "Notes", adLongVarWChar
   End With

   cat.Tables.Append tbl

   Set cat = Nothing

End Sub

go to table column and set default as 0 (zero).
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

When you are creating the Table (OMWII) dynamically, set the 'Required' property of the field (that should allow nulls) to False like this:


This will solve your problem....

Just remembered,
Also set the 'Allow Zero Length' property to False, if you want to allow Nulls (Just a caution)

jfyfeAuthor Commented:
Thanks for the suggestions, the one I found easiest to implement without getting errors was Jeremy D's.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.