Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting Db field properties with vb code at runtime

Status
Not open for further replies.

VisualLite

Programmer
Sep 5, 2001
7
US
I need to set some field properties for the table i created, such as allowing zero length and alowing duplicates. Have searched everywhere.
 
Some snippets from working code

Set fld7 = tdf.CreateField("RID_Text", dbText, 255)
fld7.AllowZeroLength = True


fldQuantity.ValidationRule ' ">=4"
fldQuantity.ValidationText = "Qty must be 4 or more items."


and from Access help:

This example creates a new table called MyTable with two text fields, a Date/Time field, and a unique index made up of all three fields.

Sub CreateTableX2()


Dim dbs As Database


' Modify this line to include the path to Northwind

' on your computer.

Set dbs = OpenDatabase("Northwind.mdb")


' Create a table with three fields and a unique

' index made up of all three fields.

dbs.Execute "CREATE TABLE MyTable " _

& "(FirstName CHAR, LastName CHAR, " _

& "DateOfBirth DATETIME, " _

& "CONSTRAINT MyTableConstraint UNIQUE " _

& "(FirstName, LastName, DateOfBirth));"


dbs.Close


End Sub

Hope this gets you started.
 
I'm using ADO. Here is a line from my code..

.CommandText = "CREATE TABLE tblInventoryList (InventoryListID Counter, InventoryItem Text(254), Category Text(50), QuantityLeft Single, Price Currency, Constraint constr PRIMARY KEY (InventoryListID))"
.Execute

For example say I wanted the field QuantityLeft to allow zerolength and allow duplicates. How can I achieve this in this situation?
 
I don't believe zero length is a property of a number field only on text.

Here is an example using the ADOX library. You can probably extract what you need out of this function. Iterate through the properties collection to see the name of the property.


Function catalogInfo()
'-- set reference to ADOX library
'- Microsoft ADO Ext. 2.6 for DDL and Security
'-- Microsoft ActiveX data objects 2.6 library also needed for ADO

Dim cg As New ADOX.Catalog
Dim tb As New ADOX.Table
Dim cn As ADODB.Connection
Dim cl As ADOX.Column
Set cg.ActiveConnection = CurrentProject.Connection

''tb.Name = "Test"
''tb.Columns.Append "col1", adInteger
''tb.Columns.Append "col2", adVarWChar, 50
''Debug.Print "table = "; cg.Tables("Test").Name
''cg.Tables.Append tb
''Exit Function
'-rename a column
Set tb = cg("test")
Set cl = tb("col2aa")
Debug.Print cl
cl.Properties("Jet OLEDB:Allow Zero Length") = True
''cl.Name = "col2aa"
Exit Function

Set cg.ActiveConnection = CurrentProject.Connection
Set tb = cg.Tables("dbo_categories")
Debug.Print "table name = "; tb.Name
'''Set cg.Procedures("myproc") = "select * from customer"
Dim pp As Property
''Debug.Print "column = "; tb.Columns("Description").Properties("default").Value
''Exit Function
For Each cl In tb.Columns
Debug.Print "name = "; cl.Name
Debug.Print "type = "; cl.Type
For Each pp In cl.Properties
Debug.Print "property name = "; pp.Name
Debug.Print "property value = "; pp.Value

Next
Next

End Function
 
Did you maybe want ADOX's objColumn.Properties("Nullable") instead?
 
Thanks for the information everyone. I believe this will work. I will get busy on it and let you all know how it is going.
Thanks again.
VisualLite
 
It works, but one problem. All the tables that need to be created and added to are on our clients machines. The program that uses the database does not have reference to ADOX. We would have to create all new installables to add those files to our clients' PC's, but thanks anyway its always good to learn something new.
VisaualLite
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top