Solved

VB.net Count Columns in an Access table

Posted on 2014-02-10
5
1,323 Views
Last Modified: 2014-02-11
Hi

What VB.net code would I use to count the number of columns in an Access table?

Thanks
0
Comment
Question by:murbro
  • 2
  • 2
5 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39849378
Hi,

pls try

myRst.Fields.Count 

Open in new window

Regards
0
 

Author Comment

by:murbro
ID: 39849408
Hi

I connect to the database via a connection string so am looking for SQL code to do this
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39849427
Hi,

pls try
SELECT COUNT(*) AS ColumnCount
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='tablename';

Open in new window

0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39849842
For an Access table from VB.Net, I believe you would have to use a method such as filling a dataadapter/datareader/etc and getting the column count.

This is using a datareader.  You'd need Imports System.Data.SQLClient at the top of your code module, and I'm assuming that you have already defined your connection (cn in the code below):


    Using cn
        Dim cmd As SqlCommand = New SqlCommand( "SELECT * FROM YourTable", cn)
        cn.Open()

        Dim dr As SqlDataReader = cmd.ExecuteReader()
        Me.txtSomeTextbox.Text = dr.FieldCount

        dr.Close
        cn.Close

    end Using

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 39849874
Thanks very much
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How can you open the FORM2 2 32
FrontEnd tools to create web database application 7 61
Help with exporting to excel 4 28
RLDC Reporting in Visual studio 11 16
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

809 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