Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1598
  • Last Modified:

VB.net Count Columns in an Access table

Hi

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

Thanks
0
Murray Brown
Asked:
Murray Brown
  • 2
  • 2
1 Solution
 
Rgonzo1971Commented:
Hi,

pls try

myRst.Fields.Count 

Open in new window

Regards
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi

I connect to the database via a connection string so am looking for SQL code to do this
0
 
Rgonzo1971Commented:
Hi,

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

Open in new window

0
 
mbizupCommented:
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
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now