|
' *****************************************************
' 接続
' *****************************************************
Set cn = CreateObject("ADODB.Connection")
strConnection = "Provider=SQLOLEDB;Data Source=サーバ名;"
strConnection = strConnection & "Initial Catalog=データベース名;"
strConnection = strConnection & "User ID=sa;"
strConnection = strConnection & "Password=;"
' *****************************************************
' エラー処理
' *****************************************************
On Error Resume Next
cn.Open strConnection
If Err <> 0 Then
Set cn = Nothing
MsgBox (Err.Description)
End
End If
On Error GoTo 0
' *****************************************************
' レコードセット作成
' *****************************************************
Set rs = CreateObject("ADODB.Recordset")
SqlQuery = "select * from テーブル名"
rs.Open SqlQuery, cn
' *****************************************************
' ループ
' *****************************************************
Do While Not rs.EOF
Me.Refresh
rs.Fields("列名").Value による参照
rs.MoveNext
Loop
' *****************************************************
' 終了処理
' *****************************************************
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
| |