Solved

VB.net Count Columns in an Access table

Posted on 2014-02-10
5
1,288 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How autopopulate number field with 2 different criteria 9 51
Variable Event ? 3 23
Query design issue 2 24
VB.NET Repostiory Pattern 7 14
In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

896 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

15 Experts available now in Live!

Get 1:1 Help Now