Solved

VB.net Count Columns in an Access table

Posted on 2014-02-10
5
1,448 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 39849378
Hi,

pls try

myRst.Fields.Count 

Open in new window

Regards
0
 

Author Comment

by:Murray Brown
ID: 39849408
Hi

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

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:Murray Brown
ID: 39849874
Thanks very much
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

688 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