bryker
asked on
ADO / Access 97 / VB6 / "Allow Zero Length"
ADO Experts!
I need to create a table in an Access 97 .MDB, and append the fields I want. That's easy enough. But when I view the table's design in Access, I want the 'Allow Zero Length' property to be "YES", not "NO". In short, these newly-created fields must allow zero-length values.
As you can see from the code snippet below, I'm trying to set the col's Attributes property to 'adColNullable', with no effect. The 'Allow Zero Length' property in the resultant table is still 'No'.
These fields are text type fields, and they will not be part of the table's key. Here is the current code I am using:
--------------------------
Dim n As Long
Dim tbl As ADOX.Table
Dim col As ADOX.Column
Dim key1 As ADOX.Key
Dim cat As ADOX.Catalog
. . .
Set cat = New ADOX.Catalog
cat.ActiveConnection = "Provider=" & MSJ_OLEDB_40 & ";" & "Data Source=" & sDBPath
. . .
Set tbl = New ADOX.Table
tbl.Name = "Test1"
'** Add columns to the new table
Call tbl.Columns.Append("ID", adDouble)
Call tbl.Columns.Append("Descri
Call tbl.Columns.Append("Suburb
. . . Add other columns . . .
For n = 1 To 20
Call tbl.Columns.Append("Field_
Next
'** Set keys, attributes on all columns just added.
For Each col In tbl.Columns
Select Case (UCase(col.Name))
Case "ID", "DESCRIPTION"
'** For these fields only, create a Primary Key
Set key1 = tbl.Keys("MyKey")
If (key1 Is Nothing) Then
tbl.Keys.Append "MyKey", adKeyPrimary, col.Name
Else
tbl.Keys("MyKey").Columns.
End If
Case Else
'** THESE FIELDS (say, 20 or 30) MUST ALLOW ZERO-LENGTH VALUES!
'** This isn't working...
col.Attributes = adColNullable
End Select
Next
'** Add this new table to the Catalog. Works fine.
cat.Tables.Append tbl
--------------------------
How can I do this, using ADO v2.1, VB6, and Access 97? The right answer can't be far off from what I've got here--by which I mean, I shouldn't have to turn back to DAO or something else for a solution. ADO should be able to do this.
Thanks in advance for a helpful solution.
Breck Ryker
breck_ryker@mpsisys.com
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Emoreau's code should work. Despite this I still wanted to post this comment because there is a difference between a column that accepts empty strings and a column that accepts NULL values. VB clearly distinguishes an empty string ("") from a string that has no value (NULL). In other words: your code works perfectly, though you tell it to create a Nullable column, which is not the same as a column that accepts strings with zero length.
ASKER
I had about given up on getting this to work, when I realized that my Table had not yet been appended to my Catalog object. I had been frustrated by the fact that my Column object had NO properties available to be set. After appending the table to the cat, properties were available--which only makes sense, now that I think about it.
Thanks for your help and research into the other similar question.
ASKER
(Aside to p_biggelaar:)
I see how that looks now. When I wrote:
>I'm trying to set the col's Attributes property to 'adColNullable', with no effect
I should have said something like "I'm finagling the only property I see available, and that's not helping me." Yes, ADO duly made my field nullable, as you indicate, but I hadn't yet begun to address its 'zero-lengthability'.
Thanks for the input.