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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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).
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....

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jfyfeAuthor Commented:
Thanks for the suggestions, the one I found easiest to implement without getting errors was Jeremy D's.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.