Solved

Check Tables In A Database

Posted on 2002-04-24
8
154 Views
Last Modified: 2008-03-06
Hello Experts.
I open a database (set newDB=workspace.opendatabase....)
Is there any way to check how many tables this database
contain ? and their names ?
Many thanks
0
Comment
Question by:HESUS
  • 4
  • 2
  • 2
8 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6965074
Debug.print newDB.TableDefs.Count

For i = 1 To newdb.TableDefs.Count - 1
        If myTableName = newdb.TableDefs(i).Name Then
            CheckTableName = True
            MsgBox "Got Table":exit for
        End If
    Next i
0
 
LVL 3

Expert Comment

by:n_narayanan
ID: 6965097
For Access Database

See

http://www.freevbcode.com/ShowCode.Asp?ID=139


You can use SQLDMO object for SQL Server,

Dim oTable As SQLDMO.Table
Dim oColumn As SQLDMO.Column

Me.MousePointer = vbHourglass

lblInfn.Visible = False

If Trim$(cboData.Text) <> "" Then
    cboTable.Clear

For Each oTable In oSQLServer.Databases (cboData.Text).Tables
        cboTable.AddItem oTable.Name
    Next
End If
lblInfn.Visible = True

The code is taken from the link

http://www.freevbcode.com/ShowCode.Asp?ID=4160

Cheers

Narayanan
0
 

Author Comment

by:HESUS
ID: 6966511
To ryancys thank you.
I need a solution for a case that I do not know the names
of the tables.
Thanks
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 

Author Comment

by:HESUS
ID: 6966542
To Narayanan many thanks.
I tried to use the code that you refered me to(http://www.freevbcode.com/ShowCode.Asp?ID=139) and when I run it, I get an error message in the line :
                  colTables.Add td.Name
the error:Method or data member not found.
I do have the dao ref.
any sugestions?
thank you very much.
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6967572
Simple, is this what you want:

For i = 1 To newdb.TableDefs.Count - 1
       Debug.Print newdb.TableDefs(i).Name
   Next i

? or ..?
0
 
LVL 3

Accepted Solution

by:
n_narayanan earned 100 total points
ID: 6967619
Hesus

It is working fine for me.

What you do, first open the vb project then go to project / references menu to add Microsoft DAO Reference 2.5 or 3.51

Put a Command Button and Put a Listbox

in the command button click event write this,
But note that you have to give the valid database

Private Sub Command1_Click()
Dim tables As Collection, i As Integer
 Set tables = NonSystemTables("D:\mydatabase.mdb")
 For i = 1 To tables.Count
    List1.AddItem tables(i)
 Next
End Sub


Also pate this function inside,


Public Function NonSystemTables(dbPath As String) As Collection

'Input: Full Path to an Access Database

'Returns: Collection of the names
'of non-system tables in that database
'or Nothing if there is an error

'Requires: a reference to data access
'objects (DAO) in your project

On Error GoTo ErrHandler

Dim td As DAO.TableDef
Dim db As DAO.Database
Dim colTables As Collection

Set db = workspaces(0).opendatabase(dbPath)

Set colTables = New Collection

 For Each td In db.TableDefs

    If td.Attributes >= 0 And td.Attributes <> dbHiddenObject _
         And td.Attributes <> 2 Then
   
          colTables.Add td.Name
    End If
  Next
db.Close
Set NonSystemTables = colTables

Exit Function
ErrHandler:
On Error Resume Next
If Not db Is Nothing Then db.Close

Set NonSystemTables = Nothing

End Function

If you still have problem,

Give me your email id. I will mail you the project.

Regards

Narayanan


0
 

Author Comment

by:HESUS
ID: 6971483
To ryancys.
Many thanks for your efforts.
The solution you are suggesting is providing few other strings that are not relevant to my needs. I guess that I can filter them, but I cannot be sure that these are the only once.
Thanks again for the help.
0
 

Author Comment

by:HESUS
ID: 6971499
To n_narayanan thank you.
I still get the same error message (collection type do not have Collection. Add),
But maybe this is because I am still using VB6.
Anyway, instead of using Collection type, I am using A List, and I get the results I need.
Many thanks for your willing to help.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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 (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

856 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