Connect To Server
acCmdConnection
The following example is from NorthwindCS.adp sample database that comes with Access 2000. It is used to check that the database is connected to a Server.
'***************** Code Start *******************
' Code from Microsoft Sample Database
Function OpenStartup() As Boolean
' Displays Startup form
' Used in OnOpen property of Startup form.
On Error GoTo OpenStartup_Err
Dim Response, Msg, Title, Style
' Set the value of HideStartupForm check box using the value of
' StartupForm property of database (as set in Display Form box
' in Startup dialog box).
If InStr(1, CurrentProject.Properties("StartupForm"), StartupFormName) > 0 Then
' StartupForm property is set to Startup, so clear HideStartupForm
' check box.
Forms(StartupFormName).HideStartupForm = False
Else
' StartupForm property is not set to Startup, so check HideStartupForm
' checkbox.
Forms(StartupFormName).HideStartupForm = True
End If
'Hide custom menu bar until we're done with the Startup form code, or user sees double
DoCmd.ShowToolbar "NorthwindCustomMenuBar", acToolbarNo
'Check if we're already connected
If CurrentProject.IsConnected Then
'need code to set project to unconnected.
Response = vbNo
Msg = UnconnectedPrompt
Style = vbYesNo + vbQuestion + vbDefaultButton1
Title = ""
If (fUnconnectedPrompt) Then
Response = MsgBox(Msg, Style, Title)
End If
If Response = vbYes Then
'write blank string for testing
CurrentProject.OpenConnection "Provider="
Else
'check if we are connected to the expected db
CheckConnectedServer
End If
Else
'attempt to autostart server
If (StartSQLServer) Then
CheckConnectedServer
Else
Msg = SelectPrompt1 + Chr$(10) + Chr$(13) + Chr$(10) + Chr$(13) + SelectPrompt2
Style = vbOKCancel + vbInformation
Title = SelectTitle
Response = MsgBox(Msg, Style, Title)
If Response <> vbCancel Then
DoCmd.RunCommand acCmdConnection
If CurrentProject.IsConnected Then CheckConnectedServer
End If
End If
End If
OpenStartup_Exit:
'If we're not connected, hide the HideStartupForm checkbox and label so that the user
'won't disable the startup code from running next time
If Not (CurrentProject.IsConnected) Then
Forms(StartupFormName).HideStartupForm.Visible = False
Forms(StartupFormName).HideStartupFormLabel.Visible = False
End If
'Now display custom menu bar. The Startup property will make it replace standard one.
DoCmd.ShowToolbar "NorthwindCustomMenuBar", acToolbarYes
Exit Function
OpenStartup_Err:
Const conPropertyNotFound = 3270
If Err = conPropertyNotFound Then
Forms(StartupFormName).HideStartupForm = True
Resume OpenStartup_Exit
End If
MsgBox Err.Description
GoTo OpenStartup_Exit
End Function
'****************** Code End ********************