Solved

ADO / Access 97 / VB6 / "Allow Zero Length"

Posted on 2000-02-21
4
707 Views
Last Modified: 2008-03-10

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:


------------------------------START CODE
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("Description", adVarWChar, 100)
Call tbl.Columns.Append("Suburb", adVarWChar, 50)
 . . . Add other columns . . .
For n = 1 To 20
   Call tbl.Columns.Append("Field_" & n, adVarWChar, 50)
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.Append col.Name
      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
-----------------------------END CODE


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
0
Comment
Question by:bryker
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 250 total points
Comment Utility
I found this code in another question that recently ask about it.

'Additional Information
Set objNewColumn = New ADOX.Column
objNewColumn.Name = "Additional Information"
objNewColumn.Attributes = adColFixed And Not adColNullable
objNewColumn.Type = adVarWChar
objNewColumn.DefinedSize = 30
objNewTable.Columns.Append objNewColumn
objNewColumn.Properties("Jet OLEDB:Allow Zero Length").Value = True
objNewColumn.Properties("Default").Value = """"""

The question is Q.10297409 (in VB - database).
0
 
LVL 2

Expert Comment

by:p_biggelaar
Comment Utility
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.
0
 

Author Comment

by:bryker
Comment Utility

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

Author Comment

by:bryker
Comment Utility

(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.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now