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

Connect2Database

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
rst.AddNew

'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
'Next

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

rst.MoveFirst

Screen.MousePointer = vbNormal

End Sub
jfyfeAsked:
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.
0
 
mmipsCommented:
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
      .Columns("ContactId").Properties
("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

0
 
sunilkkCommented:
go to table column and set default as 0 (zero).
0
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.

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

..Columns("FieldName").Properties("Required")=False

This will solve your problem....

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

Deepak.
0
 
jfyfeAuthor Commented:
Thanks for the suggestions, the one I found easiest to implement without getting errors was Jeremy D's.
0
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.