Fields in an Access database table

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2009
Posts: 40
Reputation: Arunabh Nag is an unknown quantity at this point 
Solved Threads: 1
Arunabh Nag's Avatar
Arunabh Nag Arunabh Nag is offline Offline
Light Poster

Fields in an Access database table

 
0
  #1
Aug 30th, 2009
How do i find out the number, names and datatype of fields in an Access table.....using code.....
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Fields in an Access database table

 
0
  #2
Aug 30th, 2009
DataTable class members is the answer.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 40
Reputation: Arunabh Nag is an unknown quantity at this point 
Solved Threads: 1
Arunabh Nag's Avatar
Arunabh Nag Arunabh Nag is offline Offline
Light Poster

Re: Fields in an Access database table

 
0
  #3
Aug 31st, 2009
sorry,,,....

m kinda new to this....
can u please explain...??
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 271
Reputation: GeekByChoiCe is on a distinguished road 
Solved Threads: 56
GeekByChoiCe GeekByChoiCe is offline Offline
Posting Whiz in Training

Re: Fields in an Access database table

 
1
  #4
Aug 31st, 2009
why not just google for what he told you?

i straight came to http://msdn.microsoft.com/en-us/libr...e_members.aspx
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 1
Reputation: HuubVanOers is an unknown quantity at this point 
Solved Threads: 1
HuubVanOers HuubVanOers is offline Offline
Newbie Poster

Re: Fields in an Access database table

 
0
  #5
Aug 31st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,464
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast

Re: Fields in an Access database table

 
0
  #6
Aug 31st, 2009
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

  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. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,464
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast

Re: Fields in an Access database table

 
0
  #7
Aug 31st, 2009
Also note -- you can just call connection.GetSchema() off of an existing OleDb or ODBC connection to access... its a lot less work.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 40
Reputation: Arunabh Nag is an unknown quantity at this point 
Solved Threads: 1
Arunabh Nag's Avatar
Arunabh Nag Arunabh Nag is offline Offline
Light Poster

Re: Fields in an Access database table

 
0
  #8
Sep 2nd, 2009
Using Wrong code....

  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/.....
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 40
Reputation: Arunabh Nag is an unknown quantity at this point 
Solved Threads: 1
Arunabh Nag's Avatar
Arunabh Nag Arunabh Nag is offline Offline
Light Poster

Re: Fields in an Access database table

 
0
  #9
Sep 2nd, 2009
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..??

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 40
Reputation: Arunabh Nag is an unknown quantity at this point 
Solved Threads: 1
Arunabh Nag's Avatar
Arunabh Nag Arunabh Nag is offline Offline
Light Poster

Re: Fields in an Access database table

 
0
  #10
Sep 6th, 2009
Used the following code to get the field names.

  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
untitled.JPG  
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 979 | Replies: 15
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC