-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB関係.bas
95 lines (70 loc) · 2.12 KB
/
DB関係.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
Attribute VB_Name = "DB関係"
Option Explicit
'変数
'office2007用
Private Const strProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
'DBファイルパス
Private strDbPath As String
'関数
'DBファイルパス設定
Public Sub SetDbPath(ByVal buf As String)
strDbPath = "Data Source=" & buf & ";"
End Sub
'クエリ発行
'登録, 更新
Public Sub QueryExecute(ByVal strSQL As String)
Dim connect As New ADODB.Connection
'エラー制御ON(DEBUG用)
On Error GoTo SQLERROR
'DB接続
connect.Open strProvider & strDbPath
'クエリ発行
Call connect.Execute(strSQL)
'エラー制御OFF(DEBUG用)
On Error GoTo 0
'terminate
connect.Close
Set connect = Nothing
Exit Sub
SQLERROR:
Call ErrorInfo(connect, strSQL)
End Sub
'検索
Public Sub QuerySelect(ByVal strSQL As String, ByVal rngTarget As Range)
Dim connect As New ADODB.Connection
Dim recordset As New ADODB.recordset
'エラー制御ON(DEBUG用)
On Error GoTo SQLERROR
'DB接続
connect.Open strProvider & strDbPath
'クエリ発行
recordset.Open strSQL, connect, adLockReadOnly
'データ格納
rngTarget.CopyFromRecordset recordset
'エラー制御OFF(DEBUG用)
On Error GoTo 0
'terminate
recordset.Close
Set recordset = Nothing
connect.Close
Set connect = Nothing
Exit Sub
SQLERROR:
Call ErrorInfo(connect, strSQL)
End Sub
'エラー詳細の表示
Private Sub ErrorInfo(ByVal connect As ADODB.Connection, ByVal strSQL As String)
Debug.Print "=== ERROR ==="
'イミディエイトウィンドウへエラー詳細を出力
With connect.Errors.Item(0)
Debug.Print " Description=" & .Description
Debug.Print " HelpContext=" & .HelpContext
Debug.Print " HelpFile=" & .HelpFile
Debug.Print " NativeError=" & .NativeError
Debug.Print " Number=" & .Number
Debug.Print " Source=" & .Source
Debug.Print " SQLState=" & .SqlState
End With
Debug.Print " SQL=" & strSQL
Debug.Print "=== ERROR ==="
End Sub