473,513 Members | 2,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to loop through SQL Server tables

Hi - I'm trying to work out a way to loop through (or just get a list
of) all of the tables in an SQL Server (2005) database from Access.
Based on the name of the table, I will then proceed to do some action
with it. I want to use a DSNless connection. I have searched but
haven't been able to sort it out just yet. Any ideas?

(Note: I tried with DAO but my OpenDatabase kept failing - I think it
prefers a DSN and I'm trying DSNless. I also can connect with ADO but
then can't seem to figure out how to get a list of available tables).

Dec 28 '06 #1
8 14699
Ka********@gmail.com wrote in news:1167267102.836756.85820
@f1g2000cwa.googlegroups.com:
Hi - I'm trying to work out a way to loop through (or just get a list
of) all of the tables in an SQL Server (2005) database from Access.
Based on the name of the table
<snips>
I also can connect with ADO but
then can't seem to figure out how to get a list of available tables).
Public Sub GetTableNames()
Dim c As ADODB.Connection
Dim r As ADODB.Recordset
Dim f As ADODB.Field
Set c = New ADODB.Connection
With c
..Provider = "sqloledb.1"
With .Properties
..Item("Data Source") = "server"
..Item("Initial Catalog") = "database"
..Item("PassWord") = "password"
..Item("User ID") = "userid"
End With
..Open
Set r = .OpenSchema(adSchemaTables, _
Array(Empty, Empty, Empty, "TABLE"))
With r
' show the field names
' information available
For Each f In .Fields
Debug.Print f.Name
Next f
Debug.Print
' show the table names
While Not .EOF
Debug.Print .Fields("TABLE_NAME"), .Fields("TABLE_TYPE")
.MoveNext
Wend
End With
End With
End Sub
--
Lyle Fairfield
Dec 28 '06 #2
On 27 Dec 2006 16:51:42 -0800, Ka********@gmail.com wrote:

I think Lyle's solution is more elegant than mine, but you could
certainly link sysobjects table to your MDB, and then select from it.
I believe tables have a Type='U'.

-Tom.

>Hi - I'm trying to work out a way to loop through (or just get a list
of) all of the tables in an SQL Server (2005) database from Access.
Based on the name of the table, I will then proceed to do some action
with it. I want to use a DSNless connection. I have searched but
haven't been able to sort it out just yet. Any ideas?

(Note: I tried with DAO but my OpenDatabase kept failing - I think it
prefers a DSN and I'm trying DSNless. I also can connect with ADO but
then can't seem to figure out how to get a list of available tables).
Dec 28 '06 #3
Great - thx! - works perfectly. I am a little unclear on how one would
determine what arguments are available in the Array( ) portion of the
OpenSchema line. I will continue to look into it, but if you feel like
providing additional info, great! Thanks again though.

On Dec 27, 8:24 pm, Lyle Fairfield <lylefairfi...@aim.comwrote:
Kandar7...@gmail.com wrote in news:1167267102.836756.85820
@f1g2000cwa.googlegroups.com:
Hi - I'm trying to work out a way to loop through (or just get a list
of) all of the tables in an SQL Server (2005) database from Access.
Based on the name of the table<snips>
I also can connect with ADO but
then can't seem to figure out how to get a list of available tables).Public Sub GetTableNames()
Dim c As ADODB.Connection
Dim r As ADODB.Recordset
Dim f As ADODB.Field
Set c = New ADODB.Connection
With c
.Provider = "sqloledb.1"
With .Properties
.Item("Data Source") = "server"
.Item("Initial Catalog") = "database"
.Item("PassWord") = "password"
.Item("User ID") = "userid"
End With
.Open
Set r = .OpenSchema(adSchemaTables, _
Array(Empty, Empty, Empty, "TABLE"))
With r
' show the field names
' information available
For Each f In .Fields
Debug.Print f.Name
Next f
Debug.Print
' show the table names
While Not .EOF
Debug.Print .Fields("TABLE_NAME"), .Fields("TABLE_TYPE")
.MoveNext
Wend
End With
End With
End Sub

--
Lyle Fairfield
Dec 28 '06 #4
Thanks! - this options works as well. I will explore this table
further...seems like it could be useful in many ways.

On Dec 27, 9:33 pm, Tom van Stiphout <no.spam.tom7...@cox.netwrote:
On 27 Dec 2006 16:51:42 -0800, Kandar7...@gmail.com wrote:

I think Lyle's solution is more elegant than mine, but you could
certainly link sysobjects table to your MDB, and then select from it.
I believe tables have a Type='U'.

-Tom.
Hi - I'm trying to work out a way to loop through (or just get a list
of) all of the tables in an SQL Server (2005) database from Access.
Based on the name of the table, I will then proceed to do some action
with it. I want to use a DSNless connection. I have searched but
haven't been able to sort it out just yet. Any ideas?
(Note: I tried with DAO but my OpenDatabase kept failing - I think it
prefers a DSN and I'm trying DSNless. I also can connect with ADO but
then can't seem to figure out how to get a list of available tables).- Hide quoted text -- Show quoted text -
Dec 28 '06 #5
Ka********@gmail.com wrote in
news:11**********************@a3g2000cwd.googlegro ups.com:
Great - thx! - works perfectly. I am a little unclear on how one
would determine what arguments are available in the Array( ) portion
of the OpenSchema line. I will continue to look into it, but if you
feel like providing additional info, great! Thanks again though.
The fields were:

1. TABLE_CATALOG
2. TABLE_SCHEMA
3. TABLE_NAME
4. TABLE_TYPE
5. TABLE_GUID
6. DESCRIPTION
7. TABLE_PROPID
8. DATE_CREATED
9. DATE_MODIFIED

The qualifier was

Array(Empty, Empty, Empty, "TABLE")

which specified

1. no criterion for TABLE_CATALOG
2. no criterion for TABLE_SCHEMA
3. no criterion for TABLE_NAME

4. TABLE_TYPE="Table" (which excluded the System Tables); We might have
specified "ALIAS", "TABLE", "SYNONYM","SYSTEM TABLE","VIEW","GLOBAL
TEMPORARY", "LOCAL TEMPORARY" or "SYSTEM VIEW"

We could have included more Emptys for
5. TABLE_GUID
6. DESCRIPTION
7. TABLE_PROPID
8. DATE_CREATED
9. DATE_MODIFIED
but that was unnecessary as ADO OpenSchema defaults to Empty after the last
Field referenced (so no Array will return all information available).

We could replace any Empty with an Equal Criterion for that Field.

If we specify
adSchemaProviderSpecific
and a
SchemaID
we may get other fields; I have not explored this.

--
Lyle Fairfield
Dec 28 '06 #6
Thanks - very interesting and useful. chrs

On Dec 28, 1:18 pm, Lyle Fairfield <lylefairfi...@aim.comwrote:
Kandar7...@gmail.com wrote innews:11**********************@a3g2000cwd.googleg roups.com:
Great - thx! - works perfectly. I am a little unclear on how one
would determine what arguments are available in the Array( ) portion
of the OpenSchema line. I will continue to look into it, but if you
feel like providing additional info, great! Thanks again though.The fields were:

1. TABLE_CATALOG
2. TABLE_SCHEMA
3. TABLE_NAME
4. TABLE_TYPE
5. TABLE_GUID
6. DESCRIPTION
7. TABLE_PROPID
8. DATE_CREATED
9. DATE_MODIFIED

The qualifier was

Array(Empty, Empty, Empty, "TABLE")

which specified

1. no criterion for TABLE_CATALOG
2. no criterion for TABLE_SCHEMA
3. no criterion for TABLE_NAME

4. TABLE_TYPE="Table" (which excluded the System Tables); We might have
specified "ALIAS", "TABLE", "SYNONYM","SYSTEM TABLE","VIEW","GLOBAL
TEMPORARY", "LOCAL TEMPORARY" or "SYSTEM VIEW"

We could have included more Emptys for
5. TABLE_GUID
6. DESCRIPTION
7. TABLE_PROPID
8. DATE_CREATED
9. DATE_MODIFIED
but that was unnecessary as ADO OpenSchema defaults to Empty after the last
Field referenced (so no Array will return all information available).

We could replace any Empty with an Equal Criterion for that Field.

If we specify
adSchemaProviderSpecific
and a
SchemaID
we may get other fields; I have not explored this.

--
Lyle Fairfield
Dec 28 '06 #7

Make a Pass-Thru query to your database and use:

Exec SP_Tables

As your query.

(If you don't already know this, be sure to set the ODBC connection
string in the Properties of the query.)

See the SQL BOL for more information on SP_Tables, and you might also
want to look at SP_Columns for a list of the columns in a table.
On 27 Dec 2006 16:51:42 -0800, Ka********@gmail.com wrote:
>Hi - I'm trying to work out a way to loop through (or just get a list
of) all of the tables in an SQL Server (2005) database from Access.
Based on the name of the table, I will then proceed to do some action
with it. I want to use a DSNless connection. I have searched but
haven't been able to sort it out just yet. Any ideas?

(Note: I tried with DAO but my OpenDatabase kept failing - I think it
prefers a DSN and I'm trying DSNless. I also can connect with ADO but
then can't seem to figure out how to get a list of available tables).

--
Drive C: Error. (A)bort (R)etry (S)mack The Darned Thing

Dec 28 '06 #8
On 28 Dec 2006 08:45:27 -0800, Ka********@gmail.com wrote:

sysobjects and the many other system tables and the several catelog
views (don't forget these; handy summary views!) are well documented
in SQL Server Books Online.

-Tom.

>Thanks! - this options works as well. I will explore this table
further...seems like it could be useful in many ways.

On Dec 27, 9:33 pm, Tom van Stiphout <no.spam.tom7...@cox.netwrote:
>On 27 Dec 2006 16:51:42 -0800, Kandar7...@gmail.com wrote:

I think Lyle's solution is more elegant than mine, but you could
certainly link sysobjects table to your MDB, and then select from it.
I believe tables have a Type='U'.

-Tom.
>Hi - I'm trying to work out a way to loop through (or just get a list
of) all of the tables in an SQL Server (2005) database from Access.
Based on the name of the table, I will then proceed to do some action
with it. I want to use a DSNless connection. I have searched but
haven't been able to sort it out just yet. Any ideas?
>(Note: I tried with DAO but my OpenDatabase kept failing - I think it
prefers a DSN and I'm trying DSNless. I also can connect with ADO but
then can't seem to figure out how to get a list of available tables).- Hide quoted text -- Show quoted text -
Dec 29 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2765
by: Sue Adams | last post by:
I actually have two issues/questions: I have an autonumber field in an access db table that I grab and later use to update a record in another table withing the same db. The code I use to get...
12
2374
by: jason | last post by:
Access 2000: I have a customer-inventory table I need to loop through and compile a list of all the inventory items the customer is tracking. The problem I am finding is that a simple loop...
3
5300
by: addi | last post by:
All, I will be eternally greatful if someone can provide snippet of code, URL or reference material that shows how to display data in a "n colums * n rows" format. I am new to ASP and have...
2
2023
by: dSchwartz | last post by:
I need help adding a column to a dataset, but its a little bit more complicated then just that. Here's the situation: I have many xml files in one directory, each which represent a newsletter. I...
0
1474
by: Mark Harrison | last post by:
HOWTO: Integrating Posgresql queries into an event loop. Mark Harrison mh@pixar.com May 27, 2004 Problem ------- The commonly used postgresql APIs will block until completed.
1
3359
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
6
2249
by: david | last post by:
I try to use "for" loop to retrieve and assign values in web form. The code is in the following. But it can not be compiled. What I want to do is: txtQ1.Text =...
7
9760
by: david | last post by:
I try to use "for" loop to assign textbox control ID to a textbox variable in server side codebehind for a web form. But I met some problem. What I want to do is solving the following-like code by...
16
6281
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataAdapter. For every record in tblA where colB = 'abc', I want to update the value in colA. In VB6, using ADO I can loop thru the recordset,set the...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.