TheAntiGates
11-06-2007, 06:28 PM
I am trying to set defaultvalue to prevent null from existing in a table. I can't get it to work. I tried variation 1., 2. and the final code shown below. Why does the code fail as the comments indicate, and is this (including setting .required) the way to prevent importing nulls into my table?Sub foo()
Dim i As Long, td As TableDef, db As Database
Set db = CurrentDb
Set td = db.TableDefs("mytable")
For i = 0 To td.Fields.Count - 1
td.Fields(i).Required = True
td.Fields(i).AllowZeroLength = True
'td.Fields(i).DefaultValue = " " '1. bombs out - hmm
'td.Fields(i).DefaultValue = "" '2. doesn't work alone, but okay if preceded by "x" line see below - hmmm
td.Fields(i).DefaultValue = "x"
td.Fields(i).DefaultValue = "" 'this bombs in a more creative way
Next i
Set td = Nothing: Set db = Nothing
End SubRunning as is doesn't bomb, and it's hard to verify that "" is actually the default value. Since .Required is true for each field, you'd think that a zero length string would be the default. But that's not how it works when I import. When I import I get an error message box:
"Microsoft Office Access was unable to append all the data to the table.
The contents of fields in 0 record(s) were deleted, and 0 record(s) were lost due to key violations."
If I click Yes to proceed anyway, nothing imports.
There is no primary key, no indices, and no key violation circumstances, other than the .Required that is on each field.
Could it be that setting .Required and .DefaultValue is not usable for importing data, and that its mission is simply to patrol Form entry?
EDIT: I'm on Access 03, if it wasn't evident. I thought there was an AllowNulls Property in the past!
Dim i As Long, td As TableDef, db As Database
Set db = CurrentDb
Set td = db.TableDefs("mytable")
For i = 0 To td.Fields.Count - 1
td.Fields(i).Required = True
td.Fields(i).AllowZeroLength = True
'td.Fields(i).DefaultValue = " " '1. bombs out - hmm
'td.Fields(i).DefaultValue = "" '2. doesn't work alone, but okay if preceded by "x" line see below - hmmm
td.Fields(i).DefaultValue = "x"
td.Fields(i).DefaultValue = "" 'this bombs in a more creative way
Next i
Set td = Nothing: Set db = Nothing
End SubRunning as is doesn't bomb, and it's hard to verify that "" is actually the default value. Since .Required is true for each field, you'd think that a zero length string would be the default. But that's not how it works when I import. When I import I get an error message box:
"Microsoft Office Access was unable to append all the data to the table.
The contents of fields in 0 record(s) were deleted, and 0 record(s) were lost due to key violations."
If I click Yes to proceed anyway, nothing imports.
There is no primary key, no indices, and no key violation circumstances, other than the .Required that is on each field.
Could it be that setting .Required and .DefaultValue is not usable for importing data, and that its mission is simply to patrol Form entry?
EDIT: I'm on Access 03, if it wasn't evident. I thought there was an AllowNulls Property in the past!