Solved

How to see if a table is already there in a access database from VB 2005

Posted on 2008-10-11
20
205 Views
Last Modified: 2013-11-26
Hi guys
I would like to know how i check if a table is already there in a access database  and if its not create it, to creat it i am going to use the below code, but first i need to know if the table exists

Thanks for your help  
CREATE TABLE Job_List
(ID integer PRIMARY KEY ,Joblist char(255))
 
CREATE TABLE Update
(ID integer PRIMARY KEY ,Joblist char(255))

Open in new window

0
Comment
Question by:awolarczuk
  • 9
  • 7
  • 4
20 Comments
 
LVL 75
ID: 22696451
 Basically this :

 Dim db As DAO.Database
   Dim td As DAO.TableDef
   Set db = CurrentDb()
   On Error Resume Next
Set td = db.TableDefs("Job_List")
   If err.Number = 0 Then              ' Table exists
       CREATE TABLE Update (ID integer PRIMARY KEY ,Joblist char(255))

    Else        ' Table does Not exizst
       CREATE TABLE Job_List  (ID integer PRIMARY KEY ,Joblist char(255))
        CREATE TABLE Update (ID integer PRIMARY KEY ,Joblist char(255))
   End If


mx

0
 
LVL 6

Expert Comment

by:alex_paven
ID: 22696476
On error resume next? DAO? No offense, but yuck.
Use GetSchema - if you use OleDb, OleDbConnection.GetSchema, but it's the same instruction for every type of database. That function returns a datatable you can inspect; step through the code and look at the table to get an idea about all the info contained. Basically, since you'll want table information, use GetSchema("tables"), which returns a DataTable containing detailed information about all tables in your database, after which it's easy to check if one exists. GetSchema() without a parameter returns all the possible arguments you can give it. Look at "Working with the GetSchema Methods" page in the documentation.
0
 

Author Comment

by:awolarczuk
ID: 22696478
I think we are almost there

How it will work is this

the create code i have given you is the first two database i need but the next time i run the update it needs to check if that table is already in the database

something like

if job_list is not there then
 CREATE TABLE Job_List  (ID integer PRIMARY KEY ,Joblist char(255))
else
if update is not there then
CREATE TABLE Update (ID integer PRIMARY KEY ,Joblist char(255))
else


as i need to make changes for the database more then more updates will be run though meaning that my code will need to check if they are already there , when the update table is created i will use this as well to add a note that Joblist created

thanks

0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:awolarczuk
ID: 22696479
alex_paven

thanks mate could you please give me a code example with what i have aboce

thanks mate
0
 
LVL 6

Expert Comment

by:alex_paven
ID: 22696499
Ok then:

' assuming db is your OleDbConnection
Dim dt As DataTable = db.GetSchema("Tables")
 
  Dim results() As DataRow = dt.Select("table_name='Job_list'")
  If results.Length = 0 Then 'if not found
    'create "job_list"
  End If
 
  results = dt.Select("table_name='Update')
  If results.Length = 0 Then 'if not found
    'create "update"
  End If

Open in new window

0
 

Author Comment

by:awolarczuk
ID: 22696523
Alex this is going to sound so very nieve of me but what do you mean when you say OleDbConnection, do you mean the database connect or the recordset, as when i used DB i didn't get anything
0
 
LVL 6

Expert Comment

by:alex_paven
ID: 22696541
It depends on what method you used when connecting to the database; I assumed you used the proper .net way and used a System.Data.OleDb.OleDbConnection to connect, like in the code below.

Since you mentioned recordset, I understand you're using the old ways, something like DAO; if so, consider using the new methods, as they bring a ton of new features and ease of use.

Dim db as New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb")
db.Open()
 
'alternatively, you could use Odbc.OdbcConnection:'
 
Dim db as New System.Data.Odbc.OdbcConnection("Driver={Microsoft Access Driver (*.mdb)};DBQ=db1.mdb");
db.Open()

Open in new window

0
 

Author Comment

by:awolarczuk
ID: 22696543
ok this is the code i have and i am still having issues with the dt sorry mate
 Public Sub dbu()
        Dim rs As New ADODB.Recordset
        Dim dt As DataTable = db.GetSchema("Tables")
        Dim sql As String
        Dim results() As DataRow = dt.Select("table_name='update'")
 
        If results.Length = 0 Then 'if not found
 
            sql = "CREATE TABLE Update (ID integer PRIMARY KEY ,Update char(255))"
            Debug.Print(sql)
            rs.Open(sql, cnn1, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)
 
        End If
 
 
  results = dt.Select("table_name='Job_list')
        If results.Length = 0 Then 'if not found
            sql = "CREATE TABLE Job_List (ID integer PRIMARY KEY ,Joblist char(255))"
            Debug.Print(sql)
            rs.Open(sql, cnn1, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)
        End If

Open in new window

0
 
LVL 6

Expert Comment

by:alex_paven
ID: 22696547
The methods I suggested won't work with ADODB, sorry. If you're determined to use ADODB, I'm afraid I can't help. That's the old VB6 way of doing things and frankly it sucks.
There may be a way of doing things with ADODB: get the system tables of the database, one of which contains info on the structure of the database. But since I've pretty much forgotten everything I once new about ADODB, I'm afraid I can't help.
0
 
LVL 75
ID: 22696566
Too funny !!!!!

mx
0
 

Author Comment

by:awolarczuk
ID: 22696576
Mx
mate is there any way u can help me here i am so stuck mate all the point would be ours
0
 

Author Comment

by:awolarczuk
ID: 22696579
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.

this is the error i am getting when i run the code
0
 
LVL 75
ID: 22696598
Sorry, initially I thought you were working in Access ... but I see it's .Net.

mx
0
 
LVL 6

Expert Comment

by:alex_paven
ID: 22696783
Sigh... here's a further suggestion. In Access you have the option to show 'system tables', have a look at them - they have names starting with 'MSys' - and especially the one that contains table information, then query it (like you would query a normal table) for the tables you are looking for; that's pretty much the easiest way, I think.
0
 

Author Comment

by:awolarczuk
ID: 22696791
mate i dont them showing up in access 2007 is there a way to show then
0
 
LVL 6

Expert Comment

by:alex_paven
ID: 22696798
Right click navigation pane, display options, show hidden and system objects.
0
 
LVL 6

Expert Comment

by:alex_paven
ID: 22696799
Sorry, right click -> navigation options -> display options, show hidden and system
0
 

Author Comment

by:awolarczuk
ID: 22696844
Record(s) cannot be read; no read permission on 'MSysNavPaneGroups'.

found it mate but when i true to read from it i get the above
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
ID: 22698108
"In Access you have the option to show 'system tables',"

I gave the typical method to determine this in Access.  Of course, you could add additional error trapping to include the case where the table IS there, but some other error occurs.

mx
0
 

Author Comment

by:awolarczuk
ID: 22698404
Basic it is telling me that this is a system table and cant be read
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

825 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