943,856 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 2794
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 30th, 2009
0

Fields in an Access database table

Expand Post »
How do i find out the number, names and datatype of fields in an Access table.....using code.....
Reputation Points: 10
Solved Threads: 1
Light Poster
Arunabh Nag is offline Offline
40 posts
since Apr 2009
Aug 30th, 2009
0

Re: Fields in an Access database table

DataTable class members is the answer.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 31st, 2009
0

Re: Fields in an Access database table

sorry,,,....

m kinda new to this....
can u please explain...??
Reputation Points: 10
Solved Threads: 1
Light Poster
Arunabh Nag is offline Offline
40 posts
since Apr 2009
Aug 31st, 2009
1

Re: Fields in an Access database table

why not just google for what he told you?

i straight came to http://msdn.microsoft.com/en-us/libr...e_members.aspx
Featured Poster
Reputation Points: 208
Solved Threads: 168
Practically a Master Poster
GeekByChoiCe is offline Offline
692 posts
since Jun 2009
Aug 31st, 2009
0

Re: Fields in an Access database table

Hi
the most easiest way is via DAO
- add reference to ms DAO (3.6)
- set vars for database and tabledef and field
dim db as dao.database, dim tdf as dao.tabledef
- get the current database: set db = currentdb()
- get the tabledef as object: set tdf = db.tabledefs("tblName")
- loop the fields for each fld in tdf.fields etc.
- debug.print fld.name etc.

further see the internal help: F1 on as word in the code

good luck

huub
Reputation Points: 10
Solved Threads: 1
Newbie Poster
HuubVanOers is offline Offline
1 posts
since Aug 2009
Aug 31st, 2009
0

Re: Fields in an Access database table

Arunabh Nag: Here is the C# code I use to enumerate tables and columns. Let me know if you have trouble translating it and I will get back with you tomorrow. I figure a C# answer today is better than a VB.NET answer tomorrow

VB.NET Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Data;
  3. using System.Data.Common;
  4. using System.Windows.Forms;
  5.  
  6. namespace daniweb
  7. {
  8. public partial class frmDbSearch : Form
  9. {
  10. public frmDbSearch()
  11. {
  12. InitializeComponent();
  13. }
  14.  
  15. private void button1_Click(object sender, EventArgs e)
  16. {
  17. //if (_conn.ConnectionType == ConnectionWrapperType.ODBC)
  18. //{
  19. // SetTableList(@"System.Data.Odbc");
  20. //}
  21. //else if (_conn.ConnectionType == ConnectionWrapperType.OleDb)
  22. //{
  23. // SetTableList(@"System.Data.OleDb");
  24. //}
  25. //else if (_conn.ConnectionType == ConnectionWrapperType.SQL)
  26. //{
  27. // SetTableList(@"System.Data.SqlClient");
  28. //}
  29. //else if (_conn.ConnectionType == ConnectionWrapperType.Oracle)
  30. //{
  31. // SetTableList(@"System.Data.OracleClient");
  32. //}
  33.  
  34. const string fName = @"C:\dotnetwin\dxDelete\dxSample_Q216730(1)\WebSite214\App_Data\nwind.mdb";
  35. string connStr = BuildAccessConnectionString(fName, "Admin", string.Empty, string.Empty);
  36. DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
  37. using (DbConnection connection = factory.CreateConnection())
  38. {
  39. connection.ConnectionString = connStr;
  40. connection.Open();
  41. DataTable _dtTables = connection.GetSchema("Tables");
  42. DataTable _dtColumns = connection.GetSchema("Columns");
  43. //At this point you have the tables, columns, views, etc.
  44. System.Diagnostics.Debugger.Break();
  45. }
  46.  
  47. }
  48.  
  49.  
  50. public static string BuildAccessConnectionString(string Filename, string Username, string Password, string DatabasePassword)
  51. {
  52. return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';User Id={1};Password={2};Jet OLEDB:Database Password={3};",
  53. Filename.Replace("'", "''"),
  54. Username,
  55. Password,
  56. DatabasePassword);
  57. }
  58. }
  59. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Aug 31st, 2009
0

Re: Fields in an Access database table

Also note -- you can just call connection.GetSchema() off of an existing OleDb or ODBC connection to access... its a lot less work.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 2nd, 2009
0

Re: Fields in an Access database table

Using Wrong code....

VB.NET Syntax (Toggle Plain Text)
  1. Module Module1
  2.  
  3. 'How to get a list of columns from an access database given a table name
  4. Dim oleConn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\users.mdb")
  5. Dim restrictions() As String = {"", "", "userinfo", ""}
  6. Dim SchemaTable As DataTable
  7. Public Sub connect()
  8. oleConn.Open()
  9. SchemaTable = oleConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns, _
  10. New Object() {Nothing, Nothing, Nothing, Nothing})
  11. Dim int As Integer
  12. For int = 0 To SchemaTable.Rows.Count - 1
  13. If SchemaTable.Rows(int)!TABLE_TYPE.ToString = "TABLE"
  14. 'EXCEPTION : Table is not part of table column
  15. Then
  16. 'Add items to list box
  17. Form1.comboselecttable.Items.Add(SchemaTable.Rows(int)!COLUMN_NAME.ToString())
  18. End If
  19. '// Get list of tables columns
  20. Next
  21.  
  22. 'Form1.comboselecttable.Items.Add(dbRet.Rows(0)!TABLE_NAME.ToString())
  23. '// dbRet will have a column name called "COLUMN_NAME" which will contain all the columns for the table
  24. End Sub
  25.  
  26. Public Sub endconn()
  27. oleConn.Close()
  28. End Sub
  29.  
  30. End Module

plz help/.....
Reputation Points: 10
Solved Threads: 1
Light Poster
Arunabh Nag is offline Offline
40 posts
since Apr 2009
Sep 2nd, 2009
0

Re: Fields in an Access database table

have used another variation that gives a list of the columns....

But the list also contains a set of non-existent columns (Varying with diff databases).Can anyone help..??

VB.NET Syntax (Toggle Plain Text)
  1. Module Module1
  2.  
  3. 'How to get a list of columns from an access database given a table name Jereme E. Guenther | Edit | Show History
  4. Dim oleConn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\password.mdb;Jet OLEDB:Database Password=admin")
  5. 'Dim oleConn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\chatusers.mdb;Jet OLEDB:Database Password=admin")
  6. 'Dim n As Integer
  7. Dim restrictions() As String = {"", "", "password", ""}
  8. Dim SchemaTable As DataTable
  9. Public Sub connect()
  10. oleConn.Open()
  11. SchemaTable = oleConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns, _
  12. New Object() {Nothing, Nothing, Nothing, Nothing})
  13. Dim int As Integer
  14. For int = 0 To SchemaTable.Rows.Count - 1
  15. 'If SchemaTable.Rows(int)!TABLE_TYPE.ToString = "TABLE" Then
  16. 'Add items to list box
  17. Form1.comboselecttable.Items.Add(SchemaTable.Rows(int)!COLUMN_NAME.ToString())
  18. 'End If
  19. '// Get list of tables columns
  20. Next
  21.  
  22. 'Form1.comboselecttable.Items.Add(dbRet.Rows(0)!TABLE_NAME.ToString())
  23. '// dbRet will have a column name called "COLUMN_NAME" which will contain all the columns for the table
  24. End Sub
  25.  
  26. Public Sub endconn()
  27. oleConn.Close()
  28. End Sub
  29.  
  30. End Module

some extra column names that appear are...::
Data
ID
Id
LValue
ObjectGuid
ObjectName
Property e.t.c.
Reputation Points: 10
Solved Threads: 1
Light Poster
Arunabh Nag is offline Offline
40 posts
since Apr 2009
Sep 6th, 2009
0

Re: Fields in an Access database table

Used the following code to get the field names.

VB.NET Syntax (Toggle Plain Text)
  1. Module Module1
  2.  
  3. 'How to get a list of columns from an access database given a table name Jereme E. Guenther | Edit | Show History
  4. 'Dim oleConn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\password.mdb;Jet OLEDB:Database Password=admin")
  5. 'Dim oleConn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\chatusers.mdb;Jet OLEDB:Database Password=admin")
  6. Dim oleConn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\contacts.mdb")
  7. 'Dim n As Integer
  8. 'Dim restrictions() As String = {"", "", "tablename", ""}
  9. Dim SchemaTable As DataTable
  10. Dim countcols As Integer
  11. Public Sub connect()
  12. oleConn.Open()
  13. SchemaTable = oleConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns, _
  14. New Object() {Nothing, Nothing, Nothing, Nothing})
  15. Dim int As Integer
  16. For int = 0 To SchemaTable.Rows.Count - 1
  17. 'If SchemaTable.Rows(int)!TABLE_TYPE.ToString = "TABLE" Then
  18. 'Add items to list box
  19. Form1.comboselecttable.Items.Add(SchemaTable.Rows(int)!COLUMN_NAME.ToString())
  20. 'End If
  21. '// Get list of tables columns
  22. Next
  23. countcols = SchemaTable.Rows.Count
  24. Form1.TextBox1.Text = CStr(countcols)
  25. 'Form1.comboselecttable.Items.Add(dbRet.Rows(0)!TABLE_NAME.ToString())
  26. '// dbRet will have a column name called "COLUMN_NAME" which will contain all the columns for the table
  27. End Sub
  28.  
  29. Public Sub endconn()
  30. oleConn.Close()
  31. End Sub
  32.  
  33. End Module

the problem now is that apart from the actual field names, i get 7-8 more column names that don't exist in the database.

The extra field names and numbers vary from database to database.
I have uploaded a screenshot showing the fieldnames.
only the password column actually exists
Plz help......
Attached Thumbnails
Click image for larger version

Name:	untitled.JPG
Views:	528
Size:	9.7 KB
ID:	11489  
Reputation Points: 10
Solved Threads: 1
Light Poster
Arunabh Nag is offline Offline
40 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: report in vb.net using reportviewer+ms access database
Next Thread in VB.NET Forum Timeline: Error in my code, dont know how to fix it?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC