ODBC database tilslutning
Når der 2. gang forsøges tilkoblet til en database (Assess) kommer error kode -2147418113.Måske er det en korrekt lukning af tidligere connection. ?
Jeg vedlægger koden hvis dette kunne hjælpe.
Private Sub cmd_OpenODBC_Click()
\'
On Error GoTo errorhandler
Dim MySnap As ADODB.Recordset
Dim cnn1 As ADODB.Connection
Dim Var_Sql As String
Dim Var_Antal As Long
Dim Var_Name As String
Dim Var_Address As String
Dim Var_FreeFile As Long
Var_FreeFile = FreeFile
Open \"C:\\Dokumenter\\KOBcompany\" For Output As Var_FreeFile
\' Open a connection using the Microsoft ODBC provider.
Set cnn1 = New ADODB.Connection
\'cnn1.CursorLocation = adUseClient
cnn1.ConnectionString = \"Data Source=CDS;UID=;PWD=;\" \' \"driver={SQL Server};server=LC01;uid=lc;pwd=\"
\' strCnn = \"Provider=sqloledb;Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=;\"
\' cnn1.Properties(\"Preserve On Commit\") = True
\' cnn1.Properties(\"Preserve On Abort\") = True
cnn1.Open
cnn1.DefaultDatabase = \"CDS\"
\' Set Database = Workspace.OpenDatabase(DSN = \"CDS\", True, True)
If cnn1.State = 1 Then \'\"Open\"
\'Set MySnap = cnn1.Open
Set MySnap = New ADODB.Recordset
MySnap.CursorType = adOpenKeyset \' adOpenStatic
MySnap.MoveLast
MySnap.MoveFirst
Var_Sql = \"Select * FROM company ORDER BY Name\"
MySnap.Open Var_Sql, cnn1, adOpenStatic, adLockOptimistic
If Not MySnap.EOF Then
Do While Not MySnap.EOF
Var_Name = MySnap.Fields(\"Name\")
Var_Address = MySnap.Fields(\"Address\")
Print #Var_FreeFile, Var_Name & \" \" & Var_Address
\'Debug.Print Var_Name & \" \" & Var_Address
MySnap.MoveNext
Loop
End If
MsgBox \"ODBC link opend\"
Else
MsgBox \"ODBC link closed\"
End If
MySnap.Close
cnn1.Close
Set cnn1 = Nothing
Close #Var_FreeFile
Exit_errorhandler:
Exit Sub
errorhandler:
Set cnn1 = Nothing
Select Case Err.Number
Case Is = \"-2147418113\"
Case Else
MsgBox Err.Description, , Err.Number
End Select
End Sub