VBA6 With SQL DMO Objects Attach the Database File
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

Listing 2 This VBA procedure uses the SQL DMO library to attach this article’s sample database file to the local MSDE on a workstation.