'***************************************************************************************************
' ADOでWorksheetに接続する Module1(Module)
'
' 作成者:井上治 URL:https://www.ne.jp/asahi/excel/inoue/ [Excelでお仕事!]
'***************************************************************************************************
' [参照設定]
' ・Microsoft ActiveX Data Objects 2.x Library
' ・Microsoft ADO Ext, 2.x for DDL and Security ←ADO_WS_TEST3でのみ使用
' ・Microsoft Scripting Runtime
'***************************************************************************************************
'変更日付 Rev 変更履歴内容------------------------------------------------------------------------>
'06/02/13(1.00)新規作成
'07/10/07(1.01)初回修正
'20/02/29(1.10)*.xlsm化、他
'21/07/16(1.20)データファイルを*.xlsx化
'***************************************************************************************************
Option Explicit
'===================================================================================================
Private Const g_cnsProvider As String = "Microsoft.ACE.OLEDB.12.0"
Private Const g_cnsExtProp As String = "Extended Properties"
Private Const g_cnsExcel As String = "Excel 12.0"
Private Const g_cnsDBName As String = "SAMPLE_DB.xlsx"
'***************************************************************************************************
' ■■■ ワークシート側からの呼び出し処理 ■■■
'***************************************************************************************************
'* 処理名 :ADO_WS_TEST1
'* 機能 :シートをテーブルとして内容を受け取る
'---------------------------------------------------------------------------------------------------
'* 返り値 :(なし)
'* 引数 :(なし)
'---------------------------------------------------------------------------------------------------
'* 作成日 :2006年02月13日
'* 作成者 :井上 治
'* 更新日 :2020年02月29日
'* 更新者 :井上 治
'* 機能説明:
'* 注意事項:
'***************************************************************************************************
Sub ADO_WS_TEST1()
'-----------------------------------------------------------------------------------------------
Dim dbCon As ADODB.Connection ' ADODB.Connection
Dim dbRes As ADODB.Recordset ' ADODB.Recordset
Dim objFso As FileSystemObject ' FileSystemObject
Dim lngRow As Long ' 行INDEX
Dim lngCol As Long ' 列INDEX
Dim lngIx As Long ' テーブルINDEX
Dim strFullname As String ' フルパス名
Dim strSQL As String ' SQL文
'-----------------------------------------------------------------
' DBファイル名取得
Set objFso = New FileSystemObject
strFullname = objFso.BuildPath(ThisWorkbook.Path, g_cnsDBName)
Set objFso = Nothing
' Connection生成
Set dbCon = New ADODB.Connection
With dbCon
.Provider = g_cnsProvider
.Properties(g_cnsExtProp) = g_cnsExcel
' データベースとしてOPEN
.Open strFullname
End With
'-----------------------------------------------------------------
' SQL文作成
'strSQL = "SELECT * FROM [Sheet1$]" ' ←単純に全件転記ならこの記述
strSQL = "SELECT * FROM [Sheet1$] WHERE 小分類='分類B' ORDER BY 大分類;"
' RecordSet取得(表示用)
Set dbRes = New ADODB.Recordset
dbRes.CursorLocation = adUseClient
dbRes.Open strSQL, dbCon, adOpenDynamic, adLockOptimistic, adCmdText
' シートクリア
Cells.ClearContents
'-----------------------------------------------------------------
' 見出し作成(普通は要らない!)
lngIx = 0
For lngCol = 1 To dbRes.Fields.Count
Cells(1, lngCol).Value = dbRes.Fields(lngIx).Name
' 次へ
lngIx = lngIx + 1
Next lngCol
'-----------------------------------------------------------------
' 明細のセット
'Cells(2, 1).CopyFromRecordset dbRes ' ←単純に全件転記ならこの記述でOK!
lngRow = 1
Do Until dbRes.EOF
lngRow = lngRow + 1
lngIx = 0
For lngCol = 1 To dbRes.Fields.Count
Cells(lngRow, lngCol).Value = dbRes.Fields(lngIx).Value
lngIx = lngIx + 1
Next lngCol
' 次へ
dbRes.MoveNext
Loop
'-----------------------------------------------------------------
' 終了
dbRes.Close
dbCon.Close
Set dbRes = Nothing
Set dbCon = Nothing
End Sub
'***************************************************************************************************
'* 処理名 :ADO_WS_TEST2
'* 機能 :シートをテーブルとして内容を更新する
'---------------------------------------------------------------------------------------------------
'* 返り値 :(なし)
'* 引数 :(なし)
'---------------------------------------------------------------------------------------------------
'* 作成日 :2006年02月13日
'* 作成者 :井上 治
'* 更新日 :2020年02月29日
'* 更新者 :井上 治
'* 機能説明:
'* 注意事項:
'***************************************************************************************************
Sub ADO_WS_TEST2()
'-----------------------------------------------------------------------------------------------
Dim dbCon As ADODB.Connection ' ADODB.Connection
Dim dbRes As ADODB.Recordset ' ADODB.Recordset
Dim objFso As FileSystemObject ' FileSystemObject
Dim lngRow As Long ' 行INDEX
Dim lngCol As Long ' 列INDEX
Dim lngIx As Long ' テーブルINDEX
Dim crnGaku As Currency ' 金額
Dim strFullname As String ' フルパス名
Dim strSQL As String ' SQL文
Dim strMSG As String ' メッセージ
'-----------------------------------------------------------------
' DBファイル名取得
Set objFso = New FileSystemObject
strFullname = objFso.BuildPath(ThisWorkbook.Path, g_cnsDBName)
Set objFso = Nothing
' Connection生成
Set dbCon = New ADODB.Connection
With dbCon
.Provider = g_cnsProvider
.Properties(g_cnsExtProp) = g_cnsExcel
' データベースとしてOPEN
.Open strFullname
End With
'-----------------------------------------------------------------
' SQL文作成(11~21行目のデータを対象とする)
strSQL = "[Sheet1$] WHERE 部門='部門1' AND 大分類='分類2' ORDER BY 小分類;"
' RecordSet取得(更新用)
Set dbRes = New ADODB.Recordset
dbRes.Open strSQL, dbCon, adOpenKeyset, adLockOptimistic, adCmdTable
' シートクリア
Cells.ClearContents
'-----------------------------------------------------------------
' 見出し作成(普通は要らない!)
lngIx = 0
For lngCol = 1 To dbRes.Fields.Count
Cells(1, lngCol).Value = dbRes.Fields(lngIx).Name
' 次へ
lngIx = lngIx + 1
Next lngCol
'-----------------------------------------------------------------
Cells(1, lngCol).Value = "変更後"
lngRow = 1
' 読み出して更新させる
Do Until dbRes.EOF
' 現状をシートに書き出す
lngRow = lngRow + 1
lngIx = 0
For lngCol = 1 To dbRes.Fields.Count
Cells(lngRow, lngCol).Value = dbRes.Fields(lngIx).Value
lngIx = lngIx + 1
Next lngCol
' 金額の受け取り
crnGaku = dbRes.Fields("金額").Value
' 金額を変更(受け取った額を2倍にしている)
crnGaku = crnGaku * 2
Cells(lngRow, lngCol).Value = crnGaku
' 金額を更新
dbRes.Fields("金額").Value = crnGaku
dbRes.Update
' 次へ
dbRes.MoveNext
Loop
'-----------------------------------------------------------------
' 終了
dbRes.Close
dbCon.Close
Set dbRes = Nothing
Set dbCon = Nothing
End Sub
'***************************************************************************************************
'* 処理名 :ADO_WS_TEST3
'* 機能 :シートをテーブルとして内容を受け取る(シート名不定対応)
'---------------------------------------------------------------------------------------------------
'* 返り値 :(なし)
'* 引数 :(なし)
'---------------------------------------------------------------------------------------------------
'* 作成日 :2006年02月13日
'* 作成者 :井上 治
'* 更新日 :2020年02月29日
'* 更新者 :井上 治
'* 機能説明:
'* 注意事項:
'***************************************************************************************************
Sub ADO_WS_TEST3()
'-----------------------------------------------------------------------------------------------
Dim dbCon As ADODB.Connection ' ADODB.Connection
Dim dbRes As ADODB.Recordset ' ADODB.Recordset
Dim adoCatalog As ADOX.Catalog ' ADOX.Catalog
Dim adoTbl As ADOX.Table ' ADOX.Table
Dim objFso As FileSystemObject ' FileSystemObject
Dim lngRow As Long ' 行INDEX
Dim lngCol As Long ' 列INDEX
Dim lngIx As Long ' テーブルINDEX
Dim strFullname As String ' フルパス名
Dim strSQL As String ' SQL文
Dim strShName As String ' シート名
'-----------------------------------------------------------------
' DBファイル名取得
Set objFso = New FileSystemObject
strFullname = objFso.BuildPath(ThisWorkbook.Path, g_cnsDBName)
Set objFso = Nothing
' Connection生成
Set dbCon = New ADODB.Connection
With dbCon
.Provider = g_cnsProvider
.Properties(g_cnsExtProp) = g_cnsExcel
' データベースとしてOPEN
.Open strFullname
End With
'-----------------------------------------------------------------
' ADOXで接続して先頭のシート名を得る
Set adoCatalog = New ADOX.Catalog
Set adoCatalog.ActiveConnection = dbCon
For Each adoTbl In adoCatalog.Tables
If Left(adoTbl.Name, 4) <> "MSys" Then
strShName = adoTbl.Name
Exit For
End If
Next adoTbl
Set adoCatalog.ActiveConnection = Nothing
'-----------------------------------------------------------------
' SQL文作成
'strSQL = "SELECT * FROM [Sheet1$]" ' ←単純に全件転記ならこの記述
strSQL = "SELECT * FROM [" & strShName & "] WHERE 小分類='分類B' ORDER BY 大分類;"
' RecordSet取得(表示用)
Set dbRes = New ADODB.Recordset
dbRes.CursorLocation = adUseClient
dbRes.Open strSQL, dbCon, adOpenDynamic, adLockOptimistic, adCmdText
' シートクリア
Cells.ClearContents
'-----------------------------------------------------------------
' 見出し作成(普通は要らない!)
lngIx = 0
For lngCol = 1 To dbRes.Fields.Count
Cells(1, lngCol).Value = dbRes.Fields(lngIx).Name
' 次へ
lngIx = lngIx + 1
Next lngCol
'-----------------------------------------------------------------
' 明細のセット
'Cells(2, 1).CopyFromRecordset dbRes ' ←単純に全件転記ならこの記述でOK!
lngRow = 1
Do Until dbRes.EOF
lngRow = lngRow + 1
lngIx = 0
For lngCol = 1 To dbRes.Fields.Count
Cells(lngRow, lngCol).Value = dbRes.Fields(lngIx).Value
lngIx = lngIx + 1
Next lngCol
' 次へ
dbRes.MoveNext
Loop
'-----------------------------------------------------------------
' 終了
dbRes.Close
dbCon.Close
Set dbRes = Nothing
Set dbCon = Nothing
End Sub
'----------------------------------------<< End of Source >>----------------------------------------