Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 MS Access
 Get Table structure for MS Access Database

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-04-14 : 09:39:07
vikram writes "Hello,
I have a project where in i need to create/ alter the table at runtime.
I have created an interface to do so. Now for error free alteration of table in access data base ie mdb file i need to get the names of already existing fields from a particular table.
How do i do that?
so i need a query by which i can populate recordset object with the table structure a particular table in the database.

Also I need to delete a particular column ( this will necessarily be any other column other than the primary key column.)
how do i achieve this.

Any comments would be appreciated.
Thank you
yours sincerely
Vikram"

X002548
Not Just a Number

15586 Posts

Posted - 2003-04-14 : 15:16:25
The following create an "alias" query for every table in the collection. I'm sure you can modify this to do what you want.

Good Luck


Sub createAliasQueries()
Dim wks As Workspace
Dim db As Database
Dim dbMe As Database
Dim rs As Recordset
Dim tdf As TableDef
Dim qdf As QueryDef
Dim myfield As Field

Dim intResponse As Integer
Dim strDBPath As String
Dim strField As String
Dim strSQL As String
Dim strSQLTableName As String
Dim strSQLColumnName As String

'JR
'JR1

'strDBPath = "E:\Technology Services\Projects\Tax Compliance\TaxDB_LedgerData\Development\TaxDB_LedgerData.mdb"
'JR2
strDBPath = "E:\Technology Services\Projects\Tax Compliance\TaxDB_ReviewData\Development\TaxDB_ReviewData.mdb"
strField = Chr(13)
strSQL = "SELECT "

'JR
Set wks = DBEngine.CreateWorkspace("temp", "Admin", "pwd")
'Set wks = DBEngine.CreateWorkspace("temp", "sysadmin", "pwd")
Set db = wks.OpenDatabase(strDBPath)
Set dbMe = CurrentDb

db.TableDefs.Refresh

MsgBox "Number of TableDefs: " & db.TableDefs.Count

'Loop trhough all the TableDefs in the database

For Each tdf In db.TableDefs
'Ignores system tables, which begin with "MSys" prefix
If InStr(tdf.Name, "MSys") = 0 Then

strSQLTableName = tdf.Name

'Look for table names with zero first character (not allowed in SQL Server)
If Left(strSQLTableName, 1) = "0" Then
strSQLTableName = "Z" & strSQLTableName
End If

'Look for tables names with spaces (not allowed in SQL Server)
While InStr(strSQLTableName, " ") > 0
Mid(strSQLTableName, InStr(strSQLTableName, " "), 1) = "_"
Wend

'Grab all the column names
'JR1
'Set qdf = dbMe.QueryDefs("qrySQLServerTableColumns1")
'JR2
Set qdf = dbMe.QueryDefs("qrySQLServerTableColumns2")
qdf![parTableName] = strSQLTableName
Set rs = qdf.OpenRecordset

'Skips any table names not contained in the lookup table
If rs.RecordCount > 0 Then
rs.MoveFirst

'Loop through all the fields in the TableDef and add them to the message and SQL strings
For Each myfield In tdf.Fields
strSQLColumnName = rs![FieldName]
strField = strField & Chr(13) & myfield.OrdinalPosition & " " & myfield.Name
'JR
If strSQLColumnName = myfield.Name Then
strSQL = strSQL & "[" & strSQLColumnName & "] , " & Chr(13)
Else
strSQL = strSQL & "[" & strSQLColumnName & "] AS [" & myfield.Name & "] , " & Chr(13)
End If
rs.MoveNext
Next

'Chop the last comma and space off the sql statement, then add a space before the "FROM" clause
strSQL = Left(strSQL, Len(strSQL) - 3) & " " & Chr(13)
'Complete the SQL statement
strSQL = strSQL & "FROM [dbo_" & strSQLTableName & "]"

intResponse = MsgBox(tdf.Name & " will be converted to a query named z_" & tdf.Name & "." & Chr(13) & _
"If the SQL statement below appears correct, click 'Yes.'" & Chr(13) & _
"If not, click 'No' and the query will be named zBAD_" & tdf.Name & "." & _
Chr(13) & Chr(13) & strSQL, vbYesNo)

If intResponse = vbYes Then
'Create a new query to replace the table
'JR
Set qdf = dbMe.CreateQueryDef(tdf.Name, strSQL)
'Set qdf = db.CreateQueryDef("z_" & tdf.Name, strSQL)
Else
'Create a new query to replace the table, but name it differently so user knows to edit the query
Set qdf = dbMe.CreateQueryDef("zBAD_" & tdf.Name, strSQL)
End If
End If

'Reset the two strings
strField = "" & Chr(13)
strSQL = "SELECT "
End If
Next

MsgBox "Done."

rs.Close
qdf.Close
dbMe.Close
db.Close
wks.Close
End Sub


Brett

8-)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-11-18 : 14:25:09
it is not a good design if you project requires altering tables by adding or removing columns at run-time. Seriously reconsider this approach. It is the classic symptom of a non-normalized database design.

To see what columns a table contains, add a reference to DAO and you can open up the tabledef() object for that table which has a fields collection.

- Jeff
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-11-18 : 14:32:13
I agree with Jeff...stuff like the code posted is used to aid in admin functions...

But I guess if you built AI into it, you could end up with a terminator...



Brett

8-)
Go to Top of Page
   

- Advertisement -