473,508 Members | 2,233 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Modify DAO.Tabledef

I am using code provided by Mr. Steele that allows for my MDB to
dynamically connect to remote SQL server databases. The code works
fine as follows:

Type TableDetails
TableName As String
SourceTableName As String
Attributes As Long
IndexSQL As String
End Type

Sub FixConnections(ServerName As String, DatabaseName As String)
' This code was originally written by
' Doug Steele, MVP dj******@canada.com
' You are free to use it in any application
' provided the copyright notice is left unchanged.
'
' Description: This subroutine looks for any TableDef objects in the
' database which have a connection string, and changes
the
' Connect property of those TableDef objects to use a
' DSN-less connection.
' This specific routine connects to the specified SQL
Server
' database on a specified server. It assumes trusted
connection.
'
' Inputs: ServerName: Name of the SQL Server server (string)
' DatabaseName: Name of the database on that server
(string)
'

On Error GoTo Err_FixConnections

Dim dbCurrent As DAO.Database
Dim intLoop As Integer
Dim intToChange As Integer
Dim tdfCurrent As DAO.TableDef
Dim typNewTables() As TableDetails

intToChange = 0

Set dbCurrent = DBEngine.Workspaces(0).Databases(0)

' Build a list of all of the connected TableDefs and
' the tables to which they're connected.

For Each tdfCurrent In dbCurrent.TableDefs
If Len(tdfCurrent.Connect) > 0 Then
ReDim Preserve typNewTables(0 To intToChange)
typNewTables(intToChange).Attributes = tdfCurrent.Attributes
typNewTables(intToChange).TableName = tdfCurrent.Name
typNewTables(intToChange).SourceTableName =
tdfCurrent.SourceTableName
typNewTables(intToChange).IndexSQL =
GenerateIndexSQL(tdfCurrent.Name)
intToChange = intToChange + 1
End If
Next

' Loop through all of the linked tables we found

For intLoop = 0 To (intToChange - 1)

' Delete the existing TableDef object

dbCurrent.TableDefs.Delete typNewTables(intLoop).TableName

' Create a new TableDef object, using the DSN-less connection
'Press Ctrl G and type in -->> FixConnections "fulms255\fulmfg",
"ABBAccuray"
Set tdfCurrent =
dbCurrent.CreateTableDef(typNewTables(intLoop).Tab leName)
tdfCurrent.Connect = "ODBC;Driver={SQL Server}" & _
";Server=" & ServerName & _
";Database=" & DatabaseName & _
";Uid=no3pm;Pwd=dmacs"
tdfCurrent.SourceTableName = typNewTables(intLoop).SourceTableName
dbCurrent.TableDefs.Append tdfCurrent

' Where it existed, create the __UniqueIndex index on the new table.

If Len(typNewTables(intLoop).IndexSQL) > 0 Then
dbCurrent.Execute typNewTables(intLoop).IndexSQL, dbFailOnError
End If
Next

End_FixConnections:
Set tdfCurrent = Nothing
Set dbCurrent = Nothing
Exit Sub

Err_FixConnections:
' Specific error trapping added for Error 3291
' (Syntax error in CREATE INDEX statement.), since that's what many
' people were encountering with the old code.
If Err.Number = 3291 Then
MsgBox "Problem creating the Index using" & vbCrLf & _
typNewTables(intLoop).IndexSQL, _
vbOKOnly + vbCritical, "Fix Connections"
Else
MsgBox Err.Description & " (" & Err.Number & ") encountered", _
vbOKOnly + vbCritical, "Fix Connections"
End If
Resume End_FixConnections

End Sub

Function GenerateIndexSQL(TableName As String) As String
' This code was originally written by
' Doug Steele, MVP dj******@canada.com
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Description: Linked Tables should have an index __uniqueindex.
' This function looks for that index in a given
' table and creates an SQL statement which can
' recreate that index.
' (There appears to be no other way to do this!)
' If no such index exists, the function returns an
' empty string ("").
'
' Inputs: TableDefObject: Reference to a Table (TableDef object)
'
' Returns: An SQL string (or an empty string)
'

On Error GoTo Err_GenerateIndexSQL

Dim dbCurr As DAO.Database
Dim idxCurr As DAO.Index
Dim fldCurr As DAO.Field
Dim strSQL As String
Dim tdfCurr As DAO.TableDef

Set dbCurr = CurrentDb()
Set tdfCurr = dbCurr.TableDefs(TableName)

If tdfCurr.Indexes.Count > 0 Then

' Ensure that there's actually an index named
' "__UnigueIndex" in the table

On Error Resume Next
Set idxCurr = tdfCurr.Indexes("__uniqueindex")
If Err.Number = 0 Then
On Error GoTo Err_GenerateIndexSQL

' Loop through all of the fields in the index,
' adding them to the SQL statement

If idxCurr.Fields.Count > 0 Then
strSQL = "CREATE INDEX __UniqueIndex ON [" & TableName & "] ("
For Each fldCurr In idxCurr.Fields
strSQL = strSQL & "[" & fldCurr.Name & "], "
Next

' Remove the trailing comma and space

strSQL = Left$(strSQL, Len(strSQL) - 2) & ")"
End If
End If
End If

End_GenerateIndexSQL:
Set fldCurr = Nothing
Set tdfCurr = Nothing
Set dbCurr = Nothing
GenerateIndexSQL = strSQL
Exit Function

Err_GenerateIndexSQL:
' Error number 3265 is "Not found in this collection
' (in other words, either the tablename is invalid, or
' it doesn't have an index named __uniqueindex)
If Err.Number <> 3265 Then
MsgBox Err.Description & " (" & Err.Number & ") encountered", _
vbOKOnly + vbCritical, "Generate Index SQL"
End If
Resume End_GenerateIndexSQL

End Function
As you can see from the above the code requires you to supply your
server name and database.

Here's my problem:

I am attempting to connect to two different databases on the SAME
server. I want the code to be able to connect to both of these
databases. It was suggested to me that I create a table where I can
define the database, tablenames, etc. I have created a table called
tbl_DatabaseTables that has the following fields:

TableName
DatebaseName
Username
Password
ServerName

Within the table I defined the various tables, databases etc.

I am uncertain how I would need to modify this code so that the
tdfCurrent are looked up in the MDB table called tbl_DatabaseTables.

Can anyone help me with this ?

Thanks!!
Nov 13 '05 #1
4 11607
jr*****@yahoo.com (John) wrote in message news:<c1**************************@posting.google. com>...
I am using code provided by Mr. Steele that allows for my MDB to
dynamically connect to remote SQL server databases. The code works
fine as follows:

Type TableDetails
TableName As String
SourceTableName As String
Attributes As Long
IndexSQL As String
End Type

Sub FixConnections(ServerName As String, DatabaseName As String)
' This code was originally written by
' Doug Steele, MVP dj******@canada.com
' You are free to use it in any application
' provided the copyright notice is left unchanged.
'
' Description: This subroutine looks for any TableDef objects in the
' database which have a connection string, and changes
the
' Connect property of those TableDef objects to use a
' DSN-less connection.
' This specific routine connects to the specified SQL
Server
' database on a specified server. It assumes trusted
connection.
'
' Inputs: ServerName: Name of the SQL Server server (string)
' DatabaseName: Name of the database on that server
(string)
'

On Error GoTo Err_FixConnections

Dim dbCurrent As DAO.Database
Dim intLoop As Integer
Dim intToChange As Integer
Dim tdfCurrent As DAO.TableDef
Dim typNewTables() As TableDetails

intToChange = 0

Set dbCurrent = DBEngine.Workspaces(0).Databases(0)

' Build a list of all of the connected TableDefs and
' the tables to which they're connected.

For Each tdfCurrent In dbCurrent.TableDefs
If Len(tdfCurrent.Connect) > 0 Then
ReDim Preserve typNewTables(0 To intToChange)
typNewTables(intToChange).Attributes = tdfCurrent.Attributes
typNewTables(intToChange).TableName = tdfCurrent.Name
typNewTables(intToChange).SourceTableName =
tdfCurrent.SourceTableName
typNewTables(intToChange).IndexSQL =
GenerateIndexSQL(tdfCurrent.Name)
intToChange = intToChange + 1
End If
Next

' Loop through all of the linked tables we found

For intLoop = 0 To (intToChange - 1)

' Delete the existing TableDef object

dbCurrent.TableDefs.Delete typNewTables(intLoop).TableName

' Create a new TableDef object, using the DSN-less connection
'Press Ctrl G and type in -->> FixConnections "fulms255\fulmfg",
"ABBAccuray"
Set tdfCurrent =
dbCurrent.CreateTableDef(typNewTables(intLoop).Tab leName)
tdfCurrent.Connect = "ODBC;Driver={SQL Server}" & _
";Server=" & ServerName & _
";Database=" & DatabaseName & _
";Uid=no3pm;Pwd=dmacs"
tdfCurrent.SourceTableName = typNewTables(intLoop).SourceTableName
dbCurrent.TableDefs.Append tdfCurrent

' Where it existed, create the __UniqueIndex index on the new table.

If Len(typNewTables(intLoop).IndexSQL) > 0 Then
dbCurrent.Execute typNewTables(intLoop).IndexSQL, dbFailOnError
End If
Next

End_FixConnections:
Set tdfCurrent = Nothing
Set dbCurrent = Nothing
Exit Sub

Err_FixConnections:
' Specific error trapping added for Error 3291
' (Syntax error in CREATE INDEX statement.), since that's what many
' people were encountering with the old code.
If Err.Number = 3291 Then
MsgBox "Problem creating the Index using" & vbCrLf & _
typNewTables(intLoop).IndexSQL, _
vbOKOnly + vbCritical, "Fix Connections"
Else
MsgBox Err.Description & " (" & Err.Number & ") encountered", _
vbOKOnly + vbCritical, "Fix Connections"
End If
Resume End_FixConnections

End Sub

Function GenerateIndexSQL(TableName As String) As String
' This code was originally written by
' Doug Steele, MVP dj******@canada.com
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Description: Linked Tables should have an index __uniqueindex.
' This function looks for that index in a given
' table and creates an SQL statement which can
' recreate that index.
' (There appears to be no other way to do this!)
' If no such index exists, the function returns an
' empty string ("").
'
' Inputs: TableDefObject: Reference to a Table (TableDef object)
'
' Returns: An SQL string (or an empty string)
'

On Error GoTo Err_GenerateIndexSQL

Dim dbCurr As DAO.Database
Dim idxCurr As DAO.Index
Dim fldCurr As DAO.Field
Dim strSQL As String
Dim tdfCurr As DAO.TableDef

Set dbCurr = CurrentDb()
Set tdfCurr = dbCurr.TableDefs(TableName)

If tdfCurr.Indexes.Count > 0 Then

' Ensure that there's actually an index named
' "__UnigueIndex" in the table

On Error Resume Next
Set idxCurr = tdfCurr.Indexes("__uniqueindex")
If Err.Number = 0 Then
On Error GoTo Err_GenerateIndexSQL

' Loop through all of the fields in the index,
' adding them to the SQL statement

If idxCurr.Fields.Count > 0 Then
strSQL = "CREATE INDEX __UniqueIndex ON [" & TableName & "] ("
For Each fldCurr In idxCurr.Fields
strSQL = strSQL & "[" & fldCurr.Name & "], "
Next

' Remove the trailing comma and space

strSQL = Left$(strSQL, Len(strSQL) - 2) & ")"
End If
End If
End If

End_GenerateIndexSQL:
Set fldCurr = Nothing
Set tdfCurr = Nothing
Set dbCurr = Nothing GenerateIndexSQL = strSQL
Exit Function

Err_GenerateIndexSQL:
' Error number 3265 is "Not found in this collection
' (in other words, either the tablename is invalid, or
' it doesn't have an index named __uniqueindex)
If Err.Number <> 3265 Then
MsgBox Err.Description & " (" & Err.Number & ") encountered", _
vbOKOnly + vbCritical, "Generate Index SQL"
End If
Resume End_GenerateIndexSQL

End Function
As you can see from the above the code requires you to supply your
server name and database.

Here's my problem:

I am attempting to connect to two different databases on the SAME
server. I want the code to be able to connect to both of these
databases. It was suggested to me that I create a table where I can
define the database, tablenames, etc. I have created a table called
tbl_DatabaseTables that has the following fields:

TableName
DatebaseName
Username
Password
ServerName

Within the table I defined the various tables, databases etc.

I am uncertain how I would need to modify this code so that the
tdfCurrent are looked up in the MDB table called tbl_DatabaseTables.

Can anyone help me with this ?

Thanks!!


Instead of looking at the tables with the .Connect property, just read
from your table. Open a recordset based on the table and process.
Nov 13 '05 #2
pi********@hotmail.com (Pieter Linden) wrote in message news:<bf**************************@posting.google. com>...
jr*****@yahoo.com (John) wrote in message news:<c1**************************@posting.google. com>...
I am using code provided by Mr. Steele that allows for my MDB to
dynamically connect to remote SQL server databases. The code works
fine as follows:

Type TableDetails
TableName As String
SourceTableName As String
Attributes As Long
IndexSQL As String
End Type

Sub FixConnections(ServerName As String, DatabaseName As String)
' This code was originally written by
' Doug Steele, MVP dj******@canada.com
' You are free to use it in any application
' provided the copyright notice is left unchanged.
'
' Description: This subroutine looks for any TableDef objects in the
' database which have a connection string, and changes
the
' Connect property of those TableDef objects to use a
' DSN-less connection.
' This specific routine connects to the specified SQL
Server
' database on a specified server. It assumes trusted
connection.
'
' Inputs: ServerName: Name of the SQL Server server (string)
' DatabaseName: Name of the database on that server
(string)
'

On Error GoTo Err_FixConnections

Dim dbCurrent As DAO.Database
Dim intLoop As Integer
Dim intToChange As Integer
Dim tdfCurrent As DAO.TableDef
Dim typNewTables() As TableDetails

intToChange = 0

Set dbCurrent = DBEngine.Workspaces(0).Databases(0)

' Build a list of all of the connected TableDefs and
' the tables to which they're connected.

For Each tdfCurrent In dbCurrent.TableDefs
If Len(tdfCurrent.Connect) > 0 Then
ReDim Preserve typNewTables(0 To intToChange)
typNewTables(intToChange).Attributes = tdfCurrent.Attributes
typNewTables(intToChange).TableName = tdfCurrent.Name
typNewTables(intToChange).SourceTableName =
tdfCurrent.SourceTableName
typNewTables(intToChange).IndexSQL =
GenerateIndexSQL(tdfCurrent.Name)
intToChange = intToChange + 1
End If
Next

' Loop through all of the linked tables we found

For intLoop = 0 To (intToChange - 1)

' Delete the existing TableDef object

dbCurrent.TableDefs.Delete typNewTables(intLoop).TableName

' Create a new TableDef object, using the DSN-less connection
'Press Ctrl G and type in -->> FixConnections "fulms255\fulmfg",
"ABBAccuray"
Set tdfCurrent =
dbCurrent.CreateTableDef(typNewTables(intLoop).Tab leName)
tdfCurrent.Connect = "ODBC;Driver={SQL Server}" & _
";Server=" & ServerName & _
";Database=" & DatabaseName & _
";Uid=no3pm;Pwd=dmacs"
tdfCurrent.SourceTableName = typNewTables(intLoop).SourceTableName
dbCurrent.TableDefs.Append tdfCurrent

' Where it existed, create the __UniqueIndex index on the new table.

If Len(typNewTables(intLoop).IndexSQL) > 0 Then
dbCurrent.Execute typNewTables(intLoop).IndexSQL, dbFailOnError
End If
Next

End_FixConnections:
Set tdfCurrent = Nothing
Set dbCurrent = Nothing
Exit Sub

Err_FixConnections:
' Specific error trapping added for Error 3291
' (Syntax error in CREATE INDEX statement.), since that's what many
' people were encountering with the old code.
If Err.Number = 3291 Then
MsgBox "Problem creating the Index using" & vbCrLf & _
typNewTables(intLoop).IndexSQL, _
vbOKOnly + vbCritical, "Fix Connections"
Else
MsgBox Err.Description & " (" & Err.Number & ") encountered", _
vbOKOnly + vbCritical, "Fix Connections"
End If
Resume End_FixConnections

End Sub

Function GenerateIndexSQL(TableName As String) As String
' This code was originally written by
' Doug Steele, MVP dj******@canada.com
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Description: Linked Tables should have an index __uniqueindex.
' This function looks for that index in a given
' table and creates an SQL statement which can
' recreate that index.
' (There appears to be no other way to do this!)
' If no such index exists, the function returns an
' empty string ("").
'
' Inputs: TableDefObject: Reference to a Table (TableDef object)
'
' Returns: An SQL string (or an empty string)
'

On Error GoTo Err_GenerateIndexSQL

Dim dbCurr As DAO.Database
Dim idxCurr As DAO.Index
Dim fldCurr As DAO.Field
Dim strSQL As String
Dim tdfCurr As DAO.TableDef

Set dbCurr = CurrentDb()
Set tdfCurr = dbCurr.TableDefs(TableName)

If tdfCurr.Indexes.Count > 0 Then

' Ensure that there's actually an index named
' "__UnigueIndex" in the table

On Error Resume Next
Set idxCurr = tdfCurr.Indexes("__uniqueindex")
If Err.Number = 0 Then
On Error GoTo Err_GenerateIndexSQL

' Loop through all of the fields in the index,
' adding them to the SQL statement

If idxCurr.Fields.Count > 0 Then
strSQL = "CREATE INDEX __UniqueIndex ON [" & TableName & "] ("
For Each fldCurr In idxCurr.Fields
strSQL = strSQL & "[" & fldCurr.Name & "], "
Next

' Remove the trailing comma and space

strSQL = Left$(strSQL, Len(strSQL) - 2) & ")"
End If
End If
End If

End_GenerateIndexSQL:
Set fldCurr = Nothing
Set tdfCurr = Nothing
Set dbCurr = Nothing

GenerateIndexSQL = strSQL
Exit Function

Err_GenerateIndexSQL:
' Error number 3265 is "Not found in this collection
' (in other words, either the tablename is invalid, or
' it doesn't have an index named __uniqueindex)
If Err.Number <> 3265 Then
MsgBox Err.Description & " (" & Err.Number & ") encountered", _
vbOKOnly + vbCritical, "Generate Index SQL"
End If
Resume End_GenerateIndexSQL

End Function
As you can see from the above the code requires you to supply your
server name and database.

Here's my problem:

I am attempting to connect to two different databases on the SAME
server. I want the code to be able to connect to both of these
databases. It was suggested to me that I create a table where I can
define the database, tablenames, etc. I have created a table called
tbl_DatabaseTables that has the following fields:

TableName
DatebaseName
Username
Password
ServerName

Within the table I defined the various tables, databases etc.

I am uncertain how I would need to modify this code so that the
tdfCurrent are looked up in the MDB table called tbl_DatabaseTables.

Can anyone help me with this ?

Thanks!!


Instead of looking at the tables with the .Connect property, just read
from your table. Open a recordset based on the table and process.


Can you post some sample code on how I would approach this? Sorry for
my ignorance, but I am somewhat new to VBA and Access.... Thanks.
Nov 13 '05 #3
John: The answer will differ if you're connecting to tables or views in SQL
Server (since with views, you need to specify a primary key, whereas you
inherit the primary key with tables). If you're using tables, or don't care
if it's read-only, the simplest approach would be the following untested
air-code:

Sub FixConnections()
On Error GoTo Err_FixConnections

Dim dbCurrent As DAO.Database
Dim rsCurrent As DAO.Recordset
Dim tdfCurrent As DAO.TableDef

Set dbCurrent = DBEngine.Workspaces(0).Databases(0)
Set rsCurrent = dbCurrent.OpenRecordset("tbl_DatabaseTables")
With rsCurrent
Do While .EOF = False
dbCurrent.TableDefs.Delete !TableName

Set tdfCurrent = dbCurrent.CreateTableDef(!TableName)
tdfCurrent.Connect = "ODBC;Driver={SQL Server}" & _
";Server=" & !ServerName & _
";Database=" & !DatabaseName & _
";Uid=" & !Username & _
";Pwd=" & !Password

tdfCurrent.SourceTableName = !TableName
dbCurrent.TableDefs.Append tdfCurrent

.MoveNext
Loop
End With

End_FixConnections:
On Error Resume Next
rsCurrent.Close
Set rsCurrent = Nothing
Set tdfCurrent = Nothing
Set dbCurrent = Nothing
Exit Sub

Err_FixConnections:
MsgBox Err.Description & " (" & Err.Number & ") encountered", _
vbOKOnly + vbCritical, "Fix Connections"
Resume End_FixConnections

End Sub

If you do need to worry about recreating the index, the easiest way would be
to determine the appropriate SQL to do it, and store it in
tbl_DatabaseTables.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"John" <jr*****@yahoo.com> wrote in message
news:c1*************************@posting.google.co m...
Can you post some sample code on how I would approach this? Sorry for
my ignorance, but I am somewhat new to VBA and Access.... Thanks.

Nov 13 '05 #4
jr*****@yahoo.com (John) wrote in message news:<c1**************************@posting.google. com>...

After reviewing your code and spending a great deal of time
understanding what is taking place here, I think I finally understand.
It works!! Thanks!

John

I am using code provided by Mr. Steele that allows for my MDB to
dynamically connect to remote SQL server databases. The code works
fine as follows:

Type TableDetails
TableName As String
SourceTableName As String
Attributes As Long
IndexSQL As String
End Type

Sub FixConnections(ServerName As String, DatabaseName As String)
' This code was originally written by
' Doug Steele, MVP dj******@canada.com
' You are free to use it in any application
' provided the copyright notice is left unchanged.
'
' Description: This subroutine looks for any TableDef objects in the
' database which have a connection string, and changes
the
' Connect property of those TableDef objects to use a
' DSN-less connection.
' This specific routine connects to the specified SQL
Server
' database on a specified server. It assumes trusted
connection.
'
' Inputs: ServerName: Name of the SQL Server server (string)
' DatabaseName: Name of the database on that server
(string)
'

On Error GoTo Err_FixConnections

Dim dbCurrent As DAO.Database
Dim intLoop As Integer
Dim intToChange As Integer
Dim tdfCurrent As DAO.TableDef
Dim typNewTables() As TableDetails

intToChange = 0

Set dbCurrent = DBEngine.Workspaces(0).Databases(0)

' Build a list of all of the connected TableDefs and
' the tables to which they're connected.

For Each tdfCurrent In dbCurrent.TableDefs
If Len(tdfCurrent.Connect) > 0 Then
ReDim Preserve typNewTables(0 To intToChange)
typNewTables(intToChange).Attributes = tdfCurrent.Attributes
typNewTables(intToChange).TableName = tdfCurrent.Name
typNewTables(intToChange).SourceTableName =
tdfCurrent.SourceTableName
typNewTables(intToChange).IndexSQL =
GenerateIndexSQL(tdfCurrent.Name)
intToChange = intToChange + 1
End If
Next

' Loop through all of the linked tables we found

For intLoop = 0 To (intToChange - 1)

' Delete the existing TableDef object

dbCurrent.TableDefs.Delete typNewTables(intLoop).TableName

' Create a new TableDef object, using the DSN-less connection
'Press Ctrl G and type in -->> FixConnections "fulms255\fulmfg",
"ABBAccuray"
Set tdfCurrent =
dbCurrent.CreateTableDef(typNewTables(intLoop).Tab leName)
tdfCurrent.Connect = "ODBC;Driver={SQL Server}" & _
";Server=" & ServerName & _
";Database=" & DatabaseName & _
";Uid=no3pm;Pwd=dmacs"
tdfCurrent.SourceTableName = typNewTables(intLoop).SourceTableName
dbCurrent.TableDefs.Append tdfCurrent

' Where it existed, create the __UniqueIndex index on the new table.

If Len(typNewTables(intLoop).IndexSQL) > 0 Then
dbCurrent.Execute typNewTables(intLoop).IndexSQL, dbFailOnError
End If
Next

End_FixConnections:
Set tdfCurrent = Nothing
Set dbCurrent = Nothing
Exit Sub

Err_FixConnections:
' Specific error trapping added for Error 3291
' (Syntax error in CREATE INDEX statement.), since that's what many
' people were encountering with the old code.
If Err.Number = 3291 Then
MsgBox "Problem creating the Index using" & vbCrLf & _
typNewTables(intLoop).IndexSQL, _
vbOKOnly + vbCritical, "Fix Connections"
Else
MsgBox Err.Description & " (" & Err.Number & ") encountered", _
vbOKOnly + vbCritical, "Fix Connections"
End If
Resume End_FixConnections

End Sub

Function GenerateIndexSQL(TableName As String) As String
' This code was originally written by
' Doug Steele, MVP dj******@canada.com
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Description: Linked Tables should have an index __uniqueindex.
' This function looks for that index in a given
' table and creates an SQL statement which can
' recreate that index.
' (There appears to be no other way to do this!)
' If no such index exists, the function returns an
' empty string ("").
'
' Inputs: TableDefObject: Reference to a Table (TableDef object)
'
' Returns: An SQL string (or an empty string)
'

On Error GoTo Err_GenerateIndexSQL

Dim dbCurr As DAO.Database
Dim idxCurr As DAO.Index
Dim fldCurr As DAO.Field
Dim strSQL As String
Dim tdfCurr As DAO.TableDef

Set dbCurr = CurrentDb()
Set tdfCurr = dbCurr.TableDefs(TableName)

If tdfCurr.Indexes.Count > 0 Then

' Ensure that there's actually an index named
' "__UnigueIndex" in the table

On Error Resume Next
Set idxCurr = tdfCurr.Indexes("__uniqueindex")
If Err.Number = 0 Then
On Error GoTo Err_GenerateIndexSQL

' Loop through all of the fields in the index,
' adding them to the SQL statement

If idxCurr.Fields.Count > 0 Then
strSQL = "CREATE INDEX __UniqueIndex ON [" & TableName & "] ("
For Each fldCurr In idxCurr.Fields
strSQL = strSQL & "[" & fldCurr.Name & "], "
Next

' Remove the trailing comma and space

strSQL = Left$(strSQL, Len(strSQL) - 2) & ")"
End If
End If
End If

End_GenerateIndexSQL:
Set fldCurr = Nothing
Set tdfCurr = Nothing
Set dbCurr = Nothing
GenerateIndexSQL = strSQL
Exit Function

Err_GenerateIndexSQL:
' Error number 3265 is "Not found in this collection
' (in other words, either the tablename is invalid, or
' it doesn't have an index named __uniqueindex)
If Err.Number <> 3265 Then
MsgBox Err.Description & " (" & Err.Number & ") encountered", _
vbOKOnly + vbCritical, "Generate Index SQL"
End If
Resume End_GenerateIndexSQL

End Function
As you can see from the above the code requires you to supply your
server name and database.

Here's my problem:

I am attempting to connect to two different databases on the SAME
server. I want the code to be able to connect to both of these
databases. It was suggested to me that I create a table where I can
define the database, tablenames, etc. I have created a table called
tbl_DatabaseTables that has the following fields:

TableName
DatebaseName
Username
Password
ServerName

Within the table I defined the various tables, databases etc.

I am uncertain how I would need to modify this code so that the
tdfCurrent are looked up in the MDB table called tbl_DatabaseTables.

Can anyone help me with this ?

Thanks!!

Nov 13 '05 #5

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

Similar topics

1
4069
by: andrew lovelock | last post by:
I want to do some automatic searching and linking of tables within several databases that may move around or be renamed on a network system I have seen some code snippets using TableDef Connect...
1
1899
by: Eric | last post by:
Hello, I have a Existing table with three columns that have a data type of string. I want to write VBA code that will modify one of the colums to DataType of Number. Also, I'm looking for code...
3
4966
by: Bruce Dodds | last post by:
I'm trying to set a tabledef object from a form object's RecordSource in Access 97. This is the code: Public Function EditSubjectForm(ByVal frm As Form) As Boolean Dim db As Database, tbl As...
2
5335
by: shunah | last post by:
I'm building a form that lets users manage the app's lookup tables by adding and (maybe) removing values that show up in various combo boxes across the application. The original idea was to show...
3
3283
by: deko | last post by:
I have a situation where data is being imported from external tables. The Import Wizard (File > Get External Data > Import) is used to create and import tables from different sources (txt, xls,...
6
18434
by: Giuseppe Chielli | last post by:
Hi to everyone! I'm new in this NG. I'm posting this message just for learning how can I modify the type of a field in a table via VBA code. Is that possible? And is anyone so kind to post me some...
0
2812
by: colmkav | last post by:
HI, anyone know why this code doesnt work? I am trying to set up the connect property of a tabledef so that it uses a given login and password. however once I run it I still need to login when I...
4
3110
by: Bob | last post by:
Hi all, I'm trying to import data, modify the data then insert it into a new table. The code below works fine for it but it takes a really long time for 15,000 odd records. Is there a way I...
5
2098
by: terrybell105 | last post by:
I downloaded Stephan's utility from his website but can't get it to work - or maybe I'm not driving it properly! The form works OK with the existing 3 "views" - I can switch between them and they...
0
7383
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
7046
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...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
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
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
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 ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
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.