LoginSignup
1
0

VBA Oracle 検索クエリ

Last updated at Posted at 2024-03-28


Sub searchOracle()
Dim conn As Object
Dim rs As Object
Dim strConn As String
Dim strSql As String
Dim DATA_SOURCE As String
Dim USER_ID As String
Dim Password As String
Dim colOffset As Long
Dim retPiont As Range 'スタート セール

DATA_SOURCE = Empty '10.10.10.1:1521/AAA.BBB.COM"
USER_ID = Empty
Password = Empty

'DB情報を取得
Call getDBInfo(DATA_SOURCE, USER_ID, Password)

If DATA_SOURCE = "" Or USER_ID = "" Or Password = "" Then
     MsgBox "DB情報を取得することができません"
     Exit Sub
End If

strConn = "Driver=" & "{Oracle in Oraclient12home1}" _
                        & ";Dbq=" & DATA_SOURCE _
                        & ";Uid=" & USER_ID _
                        & ";Pwd=" & Password
                        
strSql = ""

Call clearOldRET  '古いデータを削除
Call getSql(strSql)  'クエリ文字列を取得

Set conn = CreateObject("ADODB.Connection")
On Error GoTo ErrorHandler2
conn.Open strConn '

Set rs = CreateObject("ADODB.Recordset")
rs.Open strSql, conn

'『結果』を載せる位置を取得,
Set retPiont = getStartRange("結果:", 1, 1)

colOffset = 0

'タイトル
 For Each Field In rs.Fields
    retPiont.Offset(0, colOffset).Value = Field.Name
    colOffset = colOffset + 1
 Next Field
 
 'データを読み込む
 rs.MoveFirst
 retPiont.Offset(1, 0).CopyFromRecordset rs

ErrorHandler2:
If conn.State = 1 Then
    If rs.State = 0 Then
          retPiont.Offset(-1, 0) = "SQLは間違い"
     Else
      rs.Close
    End If
    conn.Close

End If
Set conn = Nothing

End Sub

'完了

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0