処理

開始

Sub 処理_開始()
    '開始処理
    Application.StatusBar = "開始"
    Application.Cursor = xlWait    Application.DisplayAlerts = False    Application.ScreenUpdating = False        Application.CutCopyMode = False    End Sub

終了

Sub 処理_終了()
    '終了処理
    Application.CutCopyMode = False
    Application.ScreenUpdating = True    Application.DisplayAlerts = True    Application.Cursor = xlDefault        Application.StatusBar = False
End Sub

エラーメッセージ

Sub 処理_エラーメッセージ()
    'エラーメッセージ表示
    On Error Resume Next    MsgBox 100 / 0    If Err.Number <> 0 Then        MsgBox Err.Description & "(" & Err.Number & ")", vbCritical    End If    On Error GoTo 0
End Sub

エラー処理

Sub 処理_エラー処理()
    On Error Resume Next    Err.Clear    If Err.Number <> 0 Then        MsgBox "エラー"        Exit Sub    End If    On Error GoTo 0    End Sub

接続実行切断

'「Microsoft ActiveX Data Objects X.X Library」を有効にする。
'Windows認証で接続する場合Public Const PROVIDER As String = "SQLOLEDB"Public DATA_SOURCE As String                               'サーバ名Public DATABASE As String                                  'データベース名
'SQL Server認証で接続する場合Public Const USER_ID As String = "UID=user"                'ユーザIDPublic Const PASSWORD As String = "password"               'ユーザパスワード
Public cn As New ADODB.ConnectionPublic rs As New ADODB.Recordset
Public strSQL As String
Sub データベース_接続実行切断()
    '■ サーバー情報設定    DATA_SOURCE = "PC1665\SQLEXPRESS"    DATABASE = "KZSDB_xxxxx"        '--------------------------------    ' データベース接続    '--------------------------------    'Windows認証で接続する場合    cn.ConnectionString = "Provider=" & PROVIDER _                        & ";Data Source=" & DATA_SOURCE _                        & ";Initial Catalog=" & DATABASE _                        & ";Trusted_Connection=Yes"    cn.Open
'   'SQL Server認証で接続する場合'   cn.ConnectionString = "Provider=" & PROVIDER _'                       & ";Data Source=" & DATA_SOURCE _'                       & ";Initial Catalog=" & DATABASE _'                       & ";UID=" & USER_ID _'                       & ";PWD=" & PASSWORD'   cn.Open
    strSQL = "SELECT MAX([SIKIBETSUNO]) FROM TBL_JJYUKI"    Debug.Print strSQL        '--------------------------------    ' SQLの実行    '--------------------------------    If Not rs Is Nothing Then        Set rs = Nothing    End If    rs.Open strSQL, cn
    If rs.RecordCount > 0 Then        Debug.Print "⇒SIKIBETSUNO=" & rs![SIKIBETSUNO]    Else        Debug.Print "⇒s.RecordCount=" & rs.RecordCount    End If
    '--------------------------------    ' データベース切断    '--------------------------------    If Not rs Is Nothing Then        If rs.State = adStateOpen Then rs.Close        Set rs = Nothing    End If    If Not cn Is Nothing Then        If cn.State = adStateOpen Then cn.Close        Set cn = Nothing    End If
End Sub