ExcelVBAの事を知ろう。
2022年9月現在、尚需要が高いExcelVBA
コードを組める様になるととても楽しいです。
記述の方法は色々なサイトがあるので、ここでは割愛します。
実業務で使用するであろうAccessSQLExcelVBAの連携だけ簡単に記述します。
ExcelVBAでマスタ更新系のプログラムを描くには………
@画面描写更新停止
ADB接続
BSQL文の記述
CDB接続破棄
D画面描写更新復帰
の順番で記述する事になります。
接続の記述は、下記の通りになります。
Option Explicit
Public dbCon As Object
Public dbRes As Object
Const cnsADO_CONNECT1 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
Const cnsADO_CONNECT2 = "\学習用Access.accdb;"
' 上記のcnsADO_CONNECT2にはAccessファイル名を記述する。
#If VBA7 Then
' ■iniファイル読込み(API:String)
Private Declare PtrSafe Function GetPrivateProfileString _
    Lib "KERNEL32.dll" Alias "GetPrivateProfileStringA" _
    (ByVal lpApplicationName As String, _
     ByVal lpKeyName As String, _
     ByVal lpDefault As String, _
     ByVal lpReturnedString As String, _
     ByVal nSize As LongPtr, _
     ByVal lpFileName As StringAs LongPtr
'---------------------------------------------------------------------------------------------------
' ■iniファイル書き込み(API:String)
Private Declare PtrSafe Function WritePrivateProfileString _
    Lib "KERNEL32.dll" Alias "WritePrivateProfileStringA" _
    (ByVal lpApplicationName As String, _
     ByVal lpKeyName As String, _
     ByVal lpString As Any, _
     ByVal lpFileName As StringAs LongPtr
#Else
' ■iniファイル読込み(API:String)
Private Declare Function GetPrivateProfileString _
    Lib "KERNEL32.dll" Alias "GetPrivateProfileStringA" _
    (ByVal lpApplicationName As String, _
     ByVal lpKeyName As String, _
     ByVal lpDefault As String, _
     ByVal lpReturnedString As String, _
     ByVal nSize As LongPtr, _
     ByVal lpFileName As StringAs LongPtr
'---------------------------------------------------------------------------------------------------
' ■iniファイル書き込み(API:String)
Private Declare Function WritePrivateProfileString _
    Lib "KERNEL32.dll" Alias "WritePrivateProfileStringA" _
    (ByVal lpApplicationName As String, _
     ByVal lpKeyName As String, _
     ByVal lpString As Any, _
     ByVal lpFileName As StringAs LongPtr
#End If
'*******************************************************************************
'   DB接続
'*******************************************************************************
Private Sub ACS_DB_OPEN(Optional dbRes As Object)
    Dim strDB_Path As StringstrMSG As String
    Dim swERR As LongPtr
    swERR = 1
    On Error GoTo ERR1
    ' 接続を確立する
    Set dbCon = CreateObject("ADODB.Connection")
    strDB_Path = ACS_GET_INI_String("F-1", "DBPATH", ThisWorkbook.Path, _
    ThisWorkbook.Path & "\F-1.ini")
    dbCon.Open cnsADO_CONNECT1 & strDB_Path & cnsADO_CONNECT2
    Set dbRes = CreateObject("ADODB.Recordset")
    Exit Sub
ERR1:
    strMSG = Err.Number & " " & Err.Description
    MsgBox "データベースに接続できませんでした。" & vbCr & strMSG
    ThisWorkbook.Saved = True
    If Workbooks.Count <= 1 Then Application.Quit
    ThisWorkbook.Close False
End Sub
'*******************************************************************************
'   INIファイルの読み込み
'*******************************************************************************
Private Function ACS_GET_INI_String(strGroup As String, _
                                    strKoumoku As String, _
                                    strDefault As String, _
                                    strFileName As StringAs String
    Dim Str As String * 1024
    Dim lngLngs As Long
    On Error GoTo INI_String_ERR
    If GetPrivateProfileString(strGroupstrKoumokustrDefaultStr, _
        1024, strFileName) <> 0 Then
        lngLngs = InStr(StrChr(0))
        ACS_GET_INI_String = Trim(Left(Str, (lngLngs - 1)))
    Else
        GoTo INI_String_ERR
    End If
    Exit Function
INI_String_ERR:
    ACS_GET_INI_String = strDefault
End Function
'*******************************************************************************
'   画面描画更新停止
'*******************************************************************************
Private Sub ACS_StopSCUPD()
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .Cursor = xlWait
        .EnableEvents = False
    End With
End Sub
'*******************************************************************************
'   画面描画更新復帰
'*******************************************************************************
Private Sub ACS_StartSCUPD()
    With Application
        If .Calculation <> xlCalculationAutomatic Then
           .Calculation = xlCalculationAutomatic
        End If
        .Cursor = xlDefault
        .EnableCancelKey = xlInterrupt
        .EnableEvents = True
        .Interactive = True
        .StatusBar = False
        .ScreenUpdating = True
    End With
End Sub
'*******************************************************************************
'   DB接続破棄
'*******************************************************************************
Public Function ACS_DB_CLOSE(Optional dbRes As ObjectAs String
    On Error Resume Next
    If Not dbRes Is Nothing Then
        dbRes.Close
    End If
    Set dbRes = Nothing
    dbCon.Close
    Set dbCon = Nothing
End Function
'*******************************************************************************
'   マスタメンテ本番
'*******************************************************************************
Sub ACS_START()
    ThisWorkbook.Worksheets("実行").Activate
    If MsgBox("DBを接続しますか?", vbYesNo) <> vbYes Then
       Exit Sub
    End If
    Application.DisplayAlerts = False
    ' 画面描画更新停止
    Call ACS_StopSCUPD
    
    ' DB接続
    Call ACS_DB_OPEN(dbRes)
    
    'ここにSQL文を描きます。
    
    ' DB接続破棄
    Call ACS_DB_CLOSE(dbRes)
    ' 画面描画更新復帰
    Call ACS_StartSCUPD
    ThisWorkbook.Worksheets("実行").Activate
    ThisWorkbook.Save
    Application.DisplayAlerts = True
    Application.Cursor = xlDefault
    MsgBox "処理が完了しました。"
End Sub
記述終了
下の方にあるマスタメンテ本番以外は、ほぼ変えなくても大丈夫です。
Activeにしたいワークシート名及びシステムに沿ったSQLを記述して下さい。
以下の動画の様なゲームを簡単に作成出来たら初級はクリヤーでしょう。