Sub ConnectData()
On Error GoTo Trapper
Dim strMessage As String
Const conDBNameExists As Integer = 1801
Set oSvr = CreateObject("SQLDMO.SQLServer")
'Log onto database
oSvr.Connect "(local)", "sa", "
'Attach to NorthwindCS database
strMessage = oSvr.AttachDBWithSingleFile _
("vbpjmsde", "c:\mssql7\data\vbpjmsdesql.mdf")
'Display the success or failure message
MsgBox strMessage
MyExit:
oSvr.Disconnect
Set oSvr = Nothing
Exit Sub
Trapper:
If Err.Number = conDBNameExists Then
MsgBox "Give database file a new name for " & _
"server. Update reference to name in " & _
"Data Link Properties dialog.", vbCritical
Else
Debug.Print Err.Number, Err.Description
End If
Resume MyExit
End Sub
|