This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Private Sub btn_edit_Click() '## 「登録」ボタンクリック時 '必須項目チェック If Me.txb_prdId.Value = "" Or _ Me.txb_prdName.Value = "" Or _ Me.txb_prdPrice.Value = "" Then MsgBox "必須項目が入力されていません", vbOKOnly + vbExclamation, "注意" 'メッセージボックスを出力 Exit Sub '終了 End If '値チェック If Not isAcceptNum(Me.txb_prdPrice) Then Exit Sub '確認メッセージ Dim msgText As String If Me.btn_edit.Caption = "登録" Then msgText = "商品ID " & Me.txb_prdId.Value & " を新規に登録します。" & vbNewLine & "よろしいですか?" Else msgText = "商品ID " & Me.txb_prdId.Value & " の内容を更新します。" & vbNewLine & "よろしいですか?" End If If MsgBox(msgText, vbOKCancel + vbQuestion, "確認") = vbCancel Then Exit Sub 'キャンセルなら終了 '値の書き込み Dim tgtRow As Long '変数宣言 tgtRow = Me.txb_tgtRow.Value '代入 With S_Mst_Product
略 End With '終了メッセージ MsgBox Me.btn_edit.Caption & "しました", vbOKOnly + vbInformation, "終了" 'フォームを閉じる
Public Function getOdrSrc() As Variant '## 見積一覧のリストボックスのソースを返す Dim i As Long '繰り返し用変数の宣言 'シートから配列へ格納 Dim wsData As Variant wsData = getWsData(S_Estimates1) 「見積データ」シートを元にする 'データがなかったら終了 If IsEmpty(wsData) Then getOdrSrc = Array() '空の配列を返す Exit Function End If 'データの要素数を取得 Dim maxRow As Long maxRow = UBound(wsData, 1) '元配列の最大行数 'ソースとなる配列の作成 Dim srcArray() As Variant '配列の宣言 ReDim srcArray(1 To maxRow, 1 To 5) '要素数を変数で再定義 For i = 1 To maxRow '要素の数だけ繰り返す srcArray(i, 1) = wsData(i, 1) 見積 ID srcArray(i, 2) = wsData(i, 2) 見積日 srcArray(i, 3) = wsData(i, 3) 顧客 ID srcArray(i, 4) = wsData(i, 5) 見積書発行日 srcArray(i, 5) = wsData(i, 6) 受注 ID Next i '配列を返す getOdrSrc = srcArrayEnd Function
Public Sub connectDB() 追加 '## 接続 Set m_cn = CreateObject("ADODB.connection") 'ADOコネクションオブジェクトを作成 m_cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _ "Data Source=" & ThisWorkbook.FullName & "; " & _ "Extended Properties='Excel 12.0; HDR=YES; IMEX=1';" 'コネクションを開くEnd Sub
Public Sub disconnectDB() 追加 '## 接続解除 m_cn.Close Set m_cn = NothingEnd Sub Public Function getRecordSet(ByVal sql As String) As Variant 追加 '## SQLからレコードセットを返す
On Error GoTo Err_Handler 'エラーが起きたら"Err_Handler"へ 'レコードセットオープン Const adOpenKeyset = 1 'カーソルタイプの定数 Dim rs As Object 'ADOレコードセットオブジェクト Set rs = CreateObject("ADODB.RecordSet") 'ADOレコードセットオブジェクトを作成 rs.Open sql, m_cn, adOpenKeyset 'カーソルタイプを指定して実行(レコード数が取得できる形) '中身のチェック If rs Is Nothing Then 'レコードセットがなかったら Set getRecordSet = Nothing