Bob,
I am using ADO connection and here is the code for connectivity.
1) Form frmstartup is being called first which in turn calls form frmDBSelect to get db information.
2) code in Form frmDBselect is...
Code
-----
Option Compare Database
Option Explicit
Private Sub cmdOK_Click()
Select Case Me!grpDBServer.Value
Case Me!optJPROD.OptionValue
Case Me!optJDEV.OptionValue
CurrentDb.Properties("DBServerName") = "CT2WD002.svr.company.net,59384\AMLMetrics"
CurrentDb.Properties("AppTitle") = Me!txtAppTitle.Value & " - DEV"
End Select
Application.RefreshTitleBar
Me.Visible = False
End Sub
Private Sub Form_Open(Cancel As Integer)
Select Case CurrentDb.Properties("DBServerName")
Case "CT2WD002.svr.company.net"
Me!grpDBServer.Value = Me!optJDEV.OptionValue
End Select
End Sub
2) Next global objects gets instantiated.
gcnnProject as New clsADOCnn (Provides global ADO connection object)
Code in Class clsADOcnn
-------------------------
Option Compare Database
Option Explicit
Private Const mcstrmod As String = "clsADOcnn"
Private mstrServer As String
Private Const mcstrDatabase As String = "AMLMetrics"
Private mcnnCurrent As New ADODB.Connection
Public Property Get Connection() As ADODB.Connection
With mcnnCurrent
If .State = adStateClosed Then
.ConnectionString = Me.ConnectionString
.Open
End If
End With
Set Connection = mcnnCurrent
End Property
Public Property Get ConnectionString() As String
ConnectionString = "DRIVER=SQL Server;SERVER=" & Me.Server & ";DATABASE=" & Me.Database & ";Trusted_Connection=Yes"
Commented code in Red (Tried all options in the Provider or Server)
-------------------
' ConnectionString = "Provider=SQLNCLI10;Data Source=" & Me.Server & ";Initial Catalog=" & Me.Database & ";Integrated Security=True"
End Property
Public Property Get Server() As String
If mstrServer = vbNullString Then
Server = CurrentDb.Properties("DBServerName")
Else
Server = mstrServer
End If
End Property
Public Property Get Database() As String
Database = mcstrDatabase
End Property
3) Then function tablereconnect() is being called from form frmstartup where linking tables and queries happens.
Code of tablereconnect()
-------------------------
Function TableReconnect(Optional strBaseMessage As String) As Boolean
Const cstrProc As String = "TableReconnect"
On Error GoTo ErrHandler
TableReconnect = True ' Default return value
Dim db As DAO.Database
Dim td As DAO.TableDef
Dim qd As DAO.QueryDef
Dim blnStartFormOpen As Boolean
Dim intTableCount As Integer
Dim intTable As Integer
Dim ctlLabel As Label
blnStartFormOpen = IsFormOpen("frmStartup")
If blnStartFormOpen = True Then
Set ctlLabel = Forms!frmStartup!lblStatus
ctlLabel.Caption = strBaseMessage & vbCrLf & "0% complete"
End If
Set db = CurrentDb()
intTableCount = db.TableDefs.Count
intTable = 0
For Each td In db.TableDefs
If Left(td.Name, 1) <> "~" Then
If Len(Nz(td.Connect, "")) > 0 Then
' td.Connect = "ODBC;" & gcnnProject.ConnectionString
td.Connect = gcnnProject.ConnectionString
td.RefreshLink
If blnStartFormOpen = True Then
intTable = intTable + 1
ctlLabel.Caption = strBaseMessage & vbCrLf & CStr(CInt(intTable * 100 / intTableCount)) & "% complete"
Forms!frmStartup.Repaint
DoEvents
Else
Debug.Print "Table " & td.Name & " refreshed at " & Time
End If
End If
End If
Next
For Each qd In db.QueryDefs
If Left(qd.Name, 1) <> "~" Then
If Len(Nz(qd.Connect, "")) > 0 Then
qd.Connect = "ODBC;" & gcnnProject.ConnectionString
' qd.Connect = gcnnProject.ConnectionString
Debug.Print "Query " & qd.Name & " refreshed at " & Time
End If
End If
Next
' Restore view indexes
If blnStartFormOpen = True Then
ctlLabel.Caption = strBaseMessage & vbCrLf & "Creating indexes: 1 of 3"
Forms!frmStartup.Repaint
DoEvents
End If
db.Execute "CREATE INDEX PrimaryIndex ON AlertAssign_UI_V (AlertSurrogateId) WITH PRIMARY"
If blnStartFormOpen = True Then
ctlLabel.Caption = strBaseMessage & vbCrLf & "Creating indexes: 2 of 3"
Forms!frmStartup.Repaint
DoEvents
End If
db.Execute "CREATE INDEX PrimaryIndex ON AlertSelect_UI_V (QCReviewId) WITH PRIMARY"
If blnStartFormOpen = True Then
ctlLabel.Caption = strBaseMessage & vbCrLf & "Creating indexes: 3 of 3"
Forms!frmStartup.Repaint
DoEvents
End If
db.Execute "CREATE INDEX PrimaryIndex ON ReviewType_V (ReviewTypeId) WITH PRIMARY"
ExitHere:
On Error Resume Next
Set ctlLabel = Nothing
Set td = Nothing
Set qd = Nothing
Set db = Nothing
Exit Function
ErrHandler:
TableReconnect = False
Call sys_ErrorHandler(mcstrmod & "." & cstrProc, Err.Number, Err.Description)
Resume ExitHere
Resume
End Function
Hope this helps...