Reading field properties from an Access database

G

Guest

I'm finally getting somewhere with this damn module; I've got the whole thing
with data adapters etc. figured out. Now that I can access the data, I need
to figure out how to do something with it.

I have a table (tblCategories) in my Access database, which includes the
field strCategory; its field size is set at 15 characters. I want the app to
read its field size for validation purposes, rather than hard-coding it. Is
this possible, and if so, how?
 
W

W.G. Ryan eMVP

You can use the GetOleDbSchemaTable method of the connection or you can let
the adapter fill your datatable and read the schema info from it.
 
G

Guest

No joy. This is the code I have so far:

Dim dc As New OleDbConnection
Dim dbLocation As String = "E:\NITLC\VB.NET\VideoClub\VideoClub.mdb"
Dim daCategorySelectText As String = "SELECT * FROM tblCategories"
dc.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" _
& dbLocation
Dim daCategory As OleDbDataAdapter = New OleDbDataAdapter _
(daCategorySelectText, dc.ConnectionString)
Dim dcCategory As New DataColumn
Dim dtCategories As DataTable = New DataTable("tblCategories")
Dim ds As New DataSet
dc.Open()
daCategory.FillSchema(ds, SchemaType.Mapped, "tblCategories")
daCategory.Fill(ds, "tblCategories")
ListBox1.DataSource() = ds.Tables("tblCategories")
ListBox1.DisplayMember = "strCategory"
dtCategories.Columns.Add(dcCategory)
Dim len1 As Integer
len1 = dcCategory.MaxLength

len1 is there just so I can test the result; it should be 15, the field size
set in Access 2003, but it's -1 - which is the default value if no max length
is set.

Have I missed something?
 
G

Guest

After further research, I've found out how to use a Data Reader to get the
schema info, using GetSchemaTable() - and the app now correctly reports 15 as
the column size for strCategory. I tried it with a different table,
tblCertificates, to read the column size of strCertificate, and it correctly
read that value as 3.

I'm getting there. God knows when I'll actually GET there at this rate, but
I AM getting there! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top