Solved

Check for Table in Database

Posted on 2001-06-13
7
153 Views
Last Modified: 2010-05-02
Hello,

VB6(sp4), ADO, Access 97

Prior to my starting application, I check for the proper database and path:

If Dir$(App_Inventory) = 0 then ...do somthing

Next, I want check to see if the database that my application is about to connect to is (in fact) the correct database. To verify this, I'd like to check the first table ( tbl_KGRM ) to make sure it is the correct database.

Thanks,
-ADawn
0
Comment
Question by:ADawn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6186201
Hi, actually i don't know how to use ADO to retrieve all tables content in a database. But in the case of using Data Control from Toolbox, yes!:

Data1.RecordSource = "Select * from Lead"
    Data1.Refresh
    For i = 1 To Data1.Database.TableDefs.Count
        List2.AddItem Data1.Database.TableDefs(i - 1).Name
    Next i

'Hope can help you little.
0
 
LVL 1

Expert Comment

by:johnczimm
ID: 6186301
Here is the function I use.  It uses DAO, but it should give you an idea of how to do it.

Public Function dbTableExists(sTableName As String) As Boolean
' returns true if the table exists in the database
Dim i As Integer
Dim Db As DAO.Database
    dbTableExists = False
    Set Db = OpenDatabase(dbName$)
    For i = 0 To Db.TableDefs.Count - 1
        If Trim(UCase(Db.TableDefs(i).Name)) = Trim(UCase(sTableName)) Then dbTableExists = True
    Next i
    Db.Close
End Function
0
 
LVL 1

Expert Comment

by:manno
ID: 6186360
For i = 0 To Db.TableDefs.Count - 1
       If Trim(UCase(Db.TableDefs(i).Name)) = Trim(UCase(sTableName)) Then

            dbTableExists = True
           
            Exit Function

       endif

 Next i
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Maxim10553
ID: 6186382
anybody have it in ADO?
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 6186393
set a reference to the Microsoft ADO Ext 2.1 for DLL and Security then paste this code:


Dim SourceCat as ADOX.Catalog
Dim SrcTbl    as ADOX.Table
Dim oColumn   as ADOX.Column
Dim x as      as Integer

set SourceCat = new ADOX.Catalog
SourceCat.ActiveConnection = "Your Regular ADO connecionstring"
for x = 1 to SourceCat.Tables.Count  
List1.AddItem SourceCat.Tables(x).Name
next x
set SourceCat = nothing

Instead of adding table to the listbox you can check if it is a desired table name
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 15 total points
ID: 6186434
ADawn, you could always use the ADOX library to enumerate the tables:

Private Sub Command1_Click()
    Dim cnn As ADODB.Connection
    Dim cat As ADOX.Catalog
    Dim tblTable As ADOX.Table
    Dim blnExists As Boolean
    Set cnn = New ADODB.Connection
    Set cat = New ADOX.Catalog
    cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\testbed\cremosa2k.mdb;Persist Security Info=False"
    cnn.Open
    cat.ActiveConnection = cnn.ConnectionString
    For Each tblTable In cat.Tables
        If tblTable.Name = "MyTable" Then
            blnExists = True
            Exit For 'as we have found it
        End If
    Next
    cnn.Close
    Set cat.ActiveConnection = Nothing
    Set cat = Nothing
    Set cnn = Nothing
    MsgBox "Table : MyTable " & IIf(blnExists, "Exists", "Does Not Exist")
End Sub

Is an example of how this could be done.
0
 

Author Comment

by:ADawn
ID: 6194810
TimCottee
Works great - Thanks for the extra insight.

iboutchkine

I have posted points for you as well. Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

623 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