473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Query to get Table schema

Thanks for taking time to read my questions.

I'm looking for a way in Access that I could run a query that would
return table schema information. Specifically I need to get the
column name, data type and size of the column. In SQL there is a
stored procedure that does this 'SP_Help <table Name>. This command
gives me more than I need but I hope the example will help.

I'm writing an ASP search page. I'm hoping to send the page a
parameter (table Name) and it will dynamically build the search page
from the schema data. I have this working with a modified Stored
Procedure in MS SQL.

Thanks Again!

NG
Mar 11 '06 #1
13 26362

"N. Graves" <ng*****@REMOVEyahoo.com> wrote:
Thanks for taking time to read my questions.

I'm looking for a way in Access that I could run a query that would
return table schema information. Specifically I need to get the
column name, data type and size of the column. In SQL there is a
stored procedure that does this 'SP_Help <table Name>. This command
gives me more than I need but I hope the example will help.


You could write a procedure in VBA and DAO to emulate the SQL
Server SP_HELP procedure. Here's something to start with:

Public Function sp_help() As Boolean
sp_help = True
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim sStmt As String

Set db = CurrentDb()
For Each tbl In db.TableDefs
Debug.Print ""
Debug.Print tbl.Name
For Each fld In tbl.Fields
Debug.Print " " & fld.Name & " " & fld.Type
Next
Next
Set db = Nothing
End Function
Have fun!

-Mark
Mar 11 '06 #2
Mark thank you for you response and your quick posting.

I'm not very familiar with DAO I using ADO to access my database. I
will continue to research you suggestion but to you have an ADO
example? I could not find and information on the CurrentDB()
This typically the way I access the DB.

sDBName = "driver={Microsoft Access Driver (*.mdb)};dbq=" &
Request.ServerVariables("APPL_PHYSICAL_PATH") & "\fpdb\sayings.mdb"

Set objDB = Server.CreateObject("ADODB.Connection")
objDB.Open sDBName

SQLQuery = "select * from sayings"
objDB.execute(SQLQuery)

If you have anything else to help I'm very much appreciative

On Fri, 10 Mar 2006 21:45:14 -0800, "Mark" <no****@thanksanyway.org>
wrote:

"N. Graves" <ng*****@REMOVEyahoo.com> wrote:
Thanks for taking time to read my questions.

I'm looking for a way in Access that I could run a query that would
return table schema information. Specifically I need to get the
column name, data type and size of the column. In SQL there is a
stored procedure that does this 'SP_Help <table Name>. This command
gives me more than I need but I hope the example will help.


You could write a procedure in VBA and DAO to emulate the SQL
Server SP_HELP procedure. Here's something to start with:

Public Function sp_help() As Boolean
sp_help = True
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim sStmt As String

Set db = CurrentDb()
For Each tbl In db.TableDefs
Debug.Print ""
Debug.Print tbl.Name
For Each fld In tbl.Fields
Debug.Print " " & fld.Name & " " & fld.Type
Next
Next
Set db = Nothing
End Function
Have fun!

-Mark

Mar 11 '06 #3
N. Graves wrote in message <0s********************************@4ax.com>
:
Mark thank you for you response and your quick posting.

I'm not very familiar with DAO I using ADO to access my database. I
will continue to research you suggestion but to you have an ADO
example? I could not find and information on the CurrentDB()
This typically the way I access the DB.

sDBName = "driver={Microsoft Access Driver (*.mdb)};dbq=" &
Request.ServerVariables("APPL_PHYSICAL_PATH") & "\fpdb\sayings.mdb"

Set objDB = Server.CreateObject("ADODB.Connection")
objDB.Open sDBName

SQLQuery = "select * from sayings"
objDB.execute(SQLQuery)

If you have anything else to help I'm very much appreciative

On Fri, 10 Mar 2006 21:45:14 -0800, "Mark" <no****@thanksanyway.org>
wrote:

"N. Graves" <ng*****@REMOVEyahoo.com> wrote:
Thanks for taking time to read my questions.

I'm looking for a way in Access that I could run a query that would
return table schema information. Specifically I need to get the
column name, data type and size of the column. In SQL there is a
stored procedure that does this 'SP_Help <table Name>. This command
gives me more than I need but I hope the example will help.


You could write a procedure in VBA and DAO to emulate the SQL
Server SP_HELP procedure. Here's something to start with:

Public Function sp_help() As Boolean
sp_help = True
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim sStmt As String

Set db = CurrentDb()
For Each tbl In db.TableDefs
Debug.Print ""
Debug.Print tbl.Name
For Each fld In tbl.Fields
Debug.Print " " & fld.Name & " " & fld.Type
Next
Next
Set db = Nothing
End Function
Have fun!

-Mark


I don't think you'll find table schema information of Access tables in
system tables, so other ways of enquiring for them is probably needed.

There's the .openschema method, which could be called for instance like
this

set rs = objDB.openschema( _
adschemacolumns, array(empty, empty, "yourtable"))

Check the returning field names and contents of this.

--
Roy-Vidar
Mar 11 '06 #4

"N. Graves" <ng*****@REMOVEyahoo.com> wrote:
Mark thank you for you response and your quick posting.

I'm not very familiar with DAO I using ADO to access my database. I
will continue to research you suggestion but to you have an ADO
example? I could not find and information on the CurrentDB()


DAO is officially "obsolete," although it is still the preferred
object model if you are working strictly with a Jet (MDB)
database. To use it, you will need to add a reference to DAO
to your project. To do this, press <ALT><F11> to bring up
the VBA dev environment. Then select Tools/References from
the menu, and check "Microsoft DAO 3.6 Object Library."

Here is a fancier version of what I posted yesterday.
This should give you a pretty good idea of how the
DAO object library works. Output from this function is
written to the Debug window (Ctrl + G) in the VBA
dev environment.

-Mark

=================================
Public Function sp_help() As Boolean
sp_help = True
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim sStmt As String
Const constPad01 = 25
Const constPad02 = 15

Set db = CurrentDb()
For Each tbl In db.TableDefs
Debug.Print ""
Debug.Print tbl.Name
For Each fld In tbl.Fields
sStmt = " " & fld.Name & String(constPad01 - Len(fld.Name), " ")
Select Case fld.Type
Case dbLong:
sStmt = sStmt & "long" & " (" & fld.Size & ")"
Case dbText:
sStmt = sStmt & "text" & " (" & fld.Size & ")"
Case dbMemo:
sStmt = sStmt & "memo" & " (" & fld.Size & ")"
Case dbDate:
sStmt = sStmt & "date" & " (" & fld.Size & ")"
Case dbTime:
sStmt = sStmt & "time" & " (" & fld.Size & ")"
Case dbTimeStamp:
sStmt = sStmt & "timestamp" & " (" & fld.Size & ")"
Case dbLongBinary:
sStmt = sStmt & "long binary" & " (" & fld.Size & ")"
Case dbBigInt:
sStmt = sStmt & "BigInt" & " (" & fld.Size & ")"
Case dbBinary:
sStmt = sStmt & "Binary" & " (" & fld.Size & ")"
Case dbVarBinary:
sStmt = sStmt & "VarBinary" & " (" & fld.Size & ")"
Case dbBoolean:
sStmt = sStmt & "Boolean" & " (" & fld.Size & ")"
Case dbByte:
sStmt = sStmt & "Byte" & " (" & fld.Size & ")"
Case dbChar:
sStmt = sStmt & "Char" & " (" & fld.Size & ")"
Case dbDouble:
sStmt = sStmt & "Double" & " (" & fld.Size & ")"
Case dbFloat:
sStmt = sStmt & "Float" & " (" & fld.Size & ")"
Case dbInteger:
sStmt = sStmt & "Integer" & " (" & fld.Size & ")"
Case Else:
sStmt = sStmt & fld.Type & " (" & fld.Size & ")"
End Select

Debug.Print sStmt
Next
Next
Set db = Nothing
End Function
Mar 11 '06 #5
Thanks Mark obviously you know a lot more about this than I do.

But I think I'm confused. Are you saying that I need to add the
reference to DAO to the database even if I'm try to access the mdb via
ASP VB coding?

Norris


On Sat, 11 Mar 2006 13:20:49 -0800, "Mark" <no****@thanksanyway.org>
wrote:

"N. Graves" <ng*****@REMOVEyahoo.com> wrote:
Mark thank you for you response and your quick posting.

I'm not very familiar with DAO I using ADO to access my database. I
will continue to research you suggestion but to you have an ADO
example? I could not find and information on the CurrentDB()


DAO is officially "obsolete," although it is still the preferred
object model if you are working strictly with a Jet (MDB)
database. To use it, you will need to add a reference to DAO
to your project. To do this, press <ALT><F11> to bring up
the VBA dev environment. Then select Tools/References from
the menu, and check "Microsoft DAO 3.6 Object Library."

Here is a fancier version of what I posted yesterday.
This should give you a pretty good idea of how the
DAO object library works. Output from this function is
written to the Debug window (Ctrl + G) in the VBA
dev environment.

-Mark

=================================
Public Function sp_help() As Boolean
sp_help = True
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim sStmt As String
Const constPad01 = 25
Const constPad02 = 15

Set db = CurrentDb()
For Each tbl In db.TableDefs
Debug.Print ""
Debug.Print tbl.Name
For Each fld In tbl.Fields
sStmt = " " & fld.Name & String(constPad01 - Len(fld.Name), " ")
Select Case fld.Type
Case dbLong:
sStmt = sStmt & "long" & " (" & fld.Size & ")"
Case dbText:
sStmt = sStmt & "text" & " (" & fld.Size & ")"
Case dbMemo:
sStmt = sStmt & "memo" & " (" & fld.Size & ")"
Case dbDate:
sStmt = sStmt & "date" & " (" & fld.Size & ")"
Case dbTime:
sStmt = sStmt & "time" & " (" & fld.Size & ")"
Case dbTimeStamp:
sStmt = sStmt & "timestamp" & " (" & fld.Size & ")"
Case dbLongBinary:
sStmt = sStmt & "long binary" & " (" & fld.Size & ")"
Case dbBigInt:
sStmt = sStmt & "BigInt" & " (" & fld.Size & ")"
Case dbBinary:
sStmt = sStmt & "Binary" & " (" & fld.Size & ")"
Case dbVarBinary:
sStmt = sStmt & "VarBinary" & " (" & fld.Size & ")"
Case dbBoolean:
sStmt = sStmt & "Boolean" & " (" & fld.Size & ")"
Case dbByte:
sStmt = sStmt & "Byte" & " (" & fld.Size & ")"
Case dbChar:
sStmt = sStmt & "Char" & " (" & fld.Size & ")"
Case dbDouble:
sStmt = sStmt & "Double" & " (" & fld.Size & ")"
Case dbFloat:
sStmt = sStmt & "Float" & " (" & fld.Size & ")"
Case dbInteger:
sStmt = sStmt & "Integer" & " (" & fld.Size & ")"
Case Else:
sStmt = sStmt & fld.Type & " (" & fld.Size & ")"
End Select

Debug.Print sStmt
Next
Next
Set db = Nothing
End Function

Mar 11 '06 #6
On Sat, 11 Mar 2006 21:29:22 +0100, RoyVidar
<ro*************@yahoo.no> wrote:
N. Graves wrote in message <0s********************************@4ax.com>
:
Mark thank you for you response and your quick posting.

I'm not very familiar with DAO I using ADO to access my database. I
will continue to research you suggestion but to you have an ADO
example? I could not find and information on the CurrentDB()
This typically the way I access the DB.

sDBName = "driver={Microsoft Access Driver (*.mdb)};dbq=" &
Request.ServerVariables("APPL_PHYSICAL_PATH") & "\fpdb\sayings.mdb"

Set objDB = Server.CreateObject("ADODB.Connection")
objDB.Open sDBName

SQLQuery = "select * from sayings"
objDB.execute(SQLQuery)

If you have anything else to help I'm very much appreciative

On Fri, 10 Mar 2006 21:45:14 -0800, "Mark" <no****@thanksanyway.org>
wrote:

"N. Graves" <ng*****@REMOVEyahoo.com> wrote:
Thanks for taking time to read my questions.

I'm looking for a way in Access that I could run a query that would
return table schema information. Specifically I need to get the
column name, data type and size of the column. In SQL there is a
stored procedure that does this 'SP_Help <table Name>. This command
gives me more than I need but I hope the example will help.

You could write a procedure in VBA and DAO to emulate the SQL
Server SP_HELP procedure. Here's something to start with:

Public Function sp_help() As Boolean
sp_help = True
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim sStmt As String

Set db = CurrentDb()
For Each tbl In db.TableDefs
Debug.Print ""
Debug.Print tbl.Name
For Each fld In tbl.Fields
Debug.Print " " & fld.Name & " " & fld.Type
Next
Next
Set db = Nothing
End Function
Have fun!

-Mark


I don't think you'll find table schema information of Access tables in
system tables, so other ways of enquiring for them is probably needed.

There's the .openschema method, which could be called for instance like
this

set rs = objDB.openschema( _
adschemacolumns, array(empty, empty, "yourtable"))

Check the returning field names and contents of this.

Yes I had just been playing with it when I saw your reply. It will
give me the column names but I still need the column data type.

Thank you for your assistance!

Norris
Mar 11 '06 #7
N. Graves wrote in message <oq********************************@4ax.com>
:
On Sat, 11 Mar 2006 21:29:22 +0100, RoyVidar
<ro*************@yahoo.no> wrote:
N. Graves wrote in message <0s********************************@4ax.com>

Mark thank you for you response and your quick posting.

I'm not very familiar with DAO I using ADO to access my database. I
will continue to research you suggestion but to you have an ADO
example? I could not find and information on the CurrentDB()
This typically the way I access the DB.

sDBName = "driver={Microsoft Access Driver (*.mdb)};dbq=" &
Request.ServerVariables("APPL_PHYSICAL_PATH") & "\fpdb\sayings.mdb"

Set objDB = Server.CreateObject("ADODB.Connection")
objDB.Open sDBName

SQLQuery = "select * from sayings"
objDB.execute(SQLQuery)

If you have anything else to help I'm very much appreciative

On Fri, 10 Mar 2006 21:45:14 -0800, "Mark" <no****@thanksanyway.org>
wrote:

"N. Graves" <ng*****@REMOVEyahoo.com> wrote:
> Thanks for taking time to read my questions.
>
> I'm looking for a way in Access that I could run a query that would
> return table schema information. Specifically I need to get the
> column name, data type and size of the column. In SQL there is a
> stored procedure that does this 'SP_Help <table Name>. This command
> gives me more than I need but I hope the example will help.

You could write a procedure in VBA and DAO to emulate the SQL
Server SP_HELP procedure. Here's something to start with:

Public Function sp_help() As Boolean
sp_help = True
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim sStmt As String

Set db = CurrentDb()
For Each tbl In db.TableDefs
Debug.Print ""
Debug.Print tbl.Name
For Each fld In tbl.Fields
Debug.Print " " & fld.Name & " " & fld.Type
Next
Next
Set db = Nothing
End Function
Have fun!

-Mark


I don't think you'll find table schema information of Access tables in
system tables, so other ways of enquiring for them is probably needed.

There's the .openschema method, which could be called for instance like
this

set rs = objDB.openschema( _
adschemacolumns, array(empty, empty, "yourtable"))

Check the returning field names and contents of this.

Yes I had just been playing with it when I saw your reply. It will
give me the column names but I still need the column data type.

Thank you for your assistance!

Norris


I believe the 12'th column returns the type (rs.fields(11).name /
rs.fields(11).value)

--
Roy-Vidar
Mar 11 '06 #8
"N. Graves" <ng*****@REMOVEyahoo.com> wrote:
Thanks Mark obviously you know a lot more about this than I do.

But I think I'm confused. Are you saying that I need to add the
reference to DAO to the database even if I'm try to access the mdb via
ASP VB coding?


If you are working in Visual Basic, then you will need to add the
DAO reference to your VB project. In the VB6 IDE, you do
this via the Project/References menu.

-Mark
Mar 11 '06 #9
In ASP we usually connect to JET(Access) db's with ADO. Roy's advice is
consistent with this. IMO, you would do well to base your soution on
that advice. The results of a similar solution can be seen at
http://ffdba.com/columnsofffdbaaccounts.asp
..

Mar 12 '06 #10
On Sat, 11 Mar 2006 23:05:25 +0100, RoyVidar
<ro*************@yahoo.no> wrote:
objDB.openschema( _
adschemacolumns, array(empty, empty, "yourtable"))

At first I thought you where joking with me. I did not understand
what you where saying. That was the right answer I put what you said
and Lyle and now I have the answer that I was looking for. I have
been looking for this answer actually for a couple of years on and
off.

Thanks you for you help!
Mar 12 '06 #11
On 11 Mar 2006 17:27:05 -0800, "Lyle Fairfield"
<ly***********@aim.com> wrote:
In ASP we usually connect to JET(Access) db's with ADO. Roy's advice is
consistent with this. IMO, you would do well to base your soution on
that advice. The results of a similar solution can be seen at
http://ffdba.com/columnsofffdbaaccounts.asp
.

Thanks everyone. With this information, seeing the page and using
Roy's advice I was able to achieve what I was looking for.

Thanks again!!!

Norris
Mar 12 '06 #12
Great; to allow for the very small chance that someone else is looking
at this I note that I have changed the page address to:
http://ffdba.com/columns.asp

Mar 12 '06 #13
On 12 Mar 2006 07:54:47 -0800, "Lyle Fairfield"
<ly***********@aim.com> wrote:
Great; to allow for the very small chance that someone else is looking
at this I note that I have changed the page address to:
http://ffdba.com/columns.asp

Page looks great... I'll share my code too:

<%@Language=VBScript %>
<!-- #include file="adovbs.inc"-->
<%
Set objDB = Server.CreateObject ("ADODB.connection")
sDBName = "driver={Microsoft Access Driver (*.mdb)};dbq=" &
Request.ServerVariables("APPL_PHYSICAL_PATH") & "Y O U R M D B"

objDB.Open sDBName

'Set objRS = objDB.Execute(SQLQuery)
set objRS = objDB.openschema(adschemacolumns, array(empty,
empty, "Y O U R T A B L E N A M E "))
If not objRS.EOF Then
Response.Write "Number of columns in table = " &
objRS.fields.count & "<BR>"
Response.Write "<table border=1 align=center><tr>"
for count = 0 to objRS.fields.count-1
Response.Write "<th>" &
objRS.fields(count).name & "</th>"
next
Response.Write "</tr>"
objRS.moveFirst
Do While Not objRS.EOF
for count = 0 to objRS.fields.count-1
Response.Write "<td>" &
objRS(objRS.fields(count).name) & "</td>"
next
Response.Write "</tr>"
objRS.Movenext
Loop
Response.Write "</table>"
End If
objRS.Close
objDB.Close
Set objRS = Nothing
Set objDB = Nothing
%>
Mar 12 '06 #14

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

Similar topics

1
by: Ralph Freshour | last post by:
I'm not sure the follow multiple table query is the right way to do what I need to do although it seems to be working: $php_SQL = "SELECT * ". "FROM basics, personal, photos ". "WHERE...
10
by: Chris Vinall | last post by:
I'm an SQL beginner and this is driving me nuts as it seems simple enough but I can't figure it out. I have a table that looks like: ID: int MajorVersion: int MinorVersion: int Content:...
2
by: Robert Stearns | last post by:
I am currently using the following query to select rows for a report. It takes forever, even though there are < 100 rows in the result set; The problem is that animals, epd and ent_herdid are all...
6
by: windandwaves | last post by:
Hi Folk I have a query: SELECT COUNT( `SIS`.`ID` ) c, D FROM `SIS` , `SID` WHERE `SID_ID` = `SID`.`ID` AND `BRO` <> "bot" GROUP BY SID.ID
12
by: zwasdl | last post by:
Hi, I'm using MS Access to query against Oracle DB via ODBC. Is it possible to use HINT in Access? Thanks, Wei
4
by: Hemant Shah | last post by:
Folks, I am having problem with an application that uses static SQL, the application basically browses through the table given start and end key most of the time it is processed from begining to...
6
by: Hemant Shah | last post by:
Folks, I am having trouble with a query. DB2 does not use index, it does relation scan of the table. I am running DB2 UDB 8.2 on Fedora Core release 4 (Stentz) # db2level DB21085I ...
2
by: pankaj_wolfhunter | last post by:
Greetings, Now this query is making me crazy. Working on it for almost 2 hrs but no output Query: SELECT ACTIONEMAILTRANSID \ ,ITEMTRANSID \ ,COMPANYID \ ,UPDATEDDATE \
1
by: Rudolf Bargholz | last post by:
Hi, We have created triggers to log modifications to tables in our application. The triggers work fine, just on one of the tables in our database the triggers fail with the error message...
3
by: nitinpatel1117 | last post by:
Hi, I've got a slight issue which i'm trying to resolve and was wondering if anyone would help. Basically, I was building a web application, and while it was being built I was connecting to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.