3.コード紹介 (1)
以下に、役に立ちそうなコード見本集掲載します。AccessVBAからの操作集です。
<01:SQL集計関数>
・SQL集計関数などは、SQLハンドブックに掲載されてはいますが、実際にVB(A)のコードにおいてどのように実行すれば良いかわからない方も多い
のではないでしょうか。以下にいくつかのコード例を掲載しますので
参考にしてください。
(1). ***** MAX *********************************
Public Sub subErrRecord(pstrPRG$, _
pstr関$, _
plngErrNo&, _
pstrErrDescript$, _
Optional pstr備考$ = "")
Dim cnn As New ADODB.Connection
Dim strSQL$, strErr$, dblID#
Dim rst管理Err As New ADODB.Recordset
On Error GoTo LABEL_Err
'1.接続
Set cnn = CurrentProject.Connection
'2.最大ErrID数取得
strSQL$ = "SELECT MAX(ID) FROM tbl管理Err"
With rst管理Err
.Source = strSQL$
.ActiveConnection = cnn
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Open
If Not .EOF Then
dblID# = .Fields(0) + 1
End If
End With
'3.SQL
strSQL$ = "INSERT INTO tbl管理Err " & _
"(ID,Err日時,ErrPRG,Err関数,ErrNo,ErrDescript) " & _
"VALUES(" & dblID# & ",'" & pstrPRG$ & "','" & pstr関$ & _
"'," & plngErrNo& & ",'" & pstrErrDescript$ & "'" & ");"
'4.書込み
cnn.Execute strSQL$, dbFailOnError
LABEL_Exit:
On Error Resume Next
cnn.Close: Set cnn = Nothing
rst管理Err.Close: Set rst管理Err = Nothing
If strErr$ <> "" Then MsgBox strErr$
Exit Sub
LABEL_Err:
strErr$ = Err.Number & " / " & Err.Description
Resume LABEL_Exit
End Sub
(2). **** SUM ************************************
Private Sub Form_Timer()
Dim strSQL$, strErr$
Dim cnnLOCAL As New ADODB.Connection
Dim rstTEMP As New ADODB.Recordset
Dim int在庫SUM%
On Error GoTo LABEL_Err
Me.Requery
'1. 接続
Set cnnLOCAL = CurrentProject.Connection
'2. 合計
strSQL$ = "SELECT SUM(小計) FROM vewRK在庫_Rental在庫計"
rstTEMP.Source = strSQL$
rstTEMP.ActiveConnection = cnnLOCAL
rstTEMP.CursorType = adOpenStatic
rstTEMP.LockType = adLockReadOnly
rstTEMP.Open
int在庫SUM% = _
IIf(IsNull(rstTEMP.Fields(0)), 0, rstTEMP.Fields(0))
rstTEMP.Close
Me.Parent.Form!txt全数 = int在庫SUM%
LABEL_Exit:
On Error Resume Next
rstTEMP.Close: Set rstTEMP = Nothing
cnnLOCAL.Close: Set cnnLOCAL = Nothing
If strErr$ <> "" Then MsgBox strErr$
Exit Sub
LABEL_Err:
strErr$ = Err.Number & " : " & Err.Description
Resume LABEL_Exit
End Sub
<02:トランザクション処理>
トランザクションは極力短時間のうちに実行を完了させます。BeginからCommintまで時間がかかるようだと、不整合や他からの処理に
障害が生じやすいので注意しましょう。
(1). **** Transaction 実行例 ****************************
Private Sub lbl更新_DblClick(Cancel As Integer)
Dim strErr$, strSQL$(2), intRET%
Dim cnnLOCAL As New ADODB.Connection
Const cstrFormName$ = "frm共通_処理中"
On Error GoTo LABEL_Err
'1. 処理中フォーム表示(フォームがすでに開かれているか確認)
intRET% = _
SysCmd(acSysCmdGetObjectState, acForm, cstrFormName$)
If intRET% = 0 Then
DoCmd.OpenForm cstrFormName$, acNormal, "", "", _
acFormPropertySettings, acHidden
End If
Forms.Item(cstrFormName$).Form.Visible = True
'2. SQL準備
strSQL$(1) = "DELETE FROM [dbo_在庫データ-Local]"
strSQL$(2) = _
"INSERT INTO [dbo_在庫データ-Local] " & _
"SELECT * FROM [dbo_在庫データ-SQL] "
'3. Transaction
Set cnnLOCAL = CurrentProject.Connection
With cnnLOCAL
.BeginTrans
For i% = 1 To 2
.Execute strSQL$(i%), dbFailOnError
Next i%
.CommitTrans
End With
LABEL_Exit:
On Error Resume Next
cnnLOCAL.RollbackTrans
cnnLOCAL.Close: Set cnnLOCAL = Nothing
Forms.Item(cstrFormName$).Form.Visible = False
If strErr$ <> "" Then MsgBox strErr$
Exit Sub
LABEL_Err:
strErr$ = _
"Error = " & Err.Number & " : " & Err.Description
Resume LABEL_Exit
End Sub