cells(y,x).value=recset.Fields(0).nameでループでまわして1行目を出力することは可能です。しかし、2行目からのデータが上手に出力できません。何かうまい方法があったら教えてください。
DAOによるデータベース接続のようですね。
データベース接続のコードは定型部分が多いですから、今までの一連の質問からサンプルを作ってみました。
DAOに参照設定をしています。
http://www.accessclub.jp/actips/tips_32.htm
Sub Macro() Dim db As DAO.Database Dim rscset As DAO.Recordset Dim mySQL As String Dim mySource As String Dim i As Long Dim j As Integer Dim fc As Integer 'Accessのファイルのパスを指定 mySource = ThisWorkbook.Path & "\sample.mdb" 'テーブルの名前を指定 Const tableName As String = "テーブル名" '開始行 Const StartRow As Long = 5 Set db = DBEngine.Workspaces(0).OpenDatabase(mySource) Set rscset = db.OpenRecordset(tableName) fc = rscset.Fields.Count i = StartRow '見出し行の書き出し For j = 1 To fc Cells(i, j).Value = rscset.Fields(j - 1).Name Next j i = i + 1 'レコードセットの書き出し While rscset.EOF = False For j = 1 To fc Cells(i, j).Value = rscset.Fields(j - 1).Value Next i = i + 1 rscset.MoveNext Wend rscset.Close Set rscset = Nothing '表の整形 With Range(Cells(StartRow, 1), Cells(i - 1, fc)) .Borders(xlEdgeLeft).Weight = xlThin .Borders(xlEdgeTop).Weight = xlThin .Borders(xlEdgeBottom).Weight = xlThin .Borders(xlEdgeRight).Weight = xlThin .Borders(xlInsideVertical).Weight = xlThin .Borders(xlInsideHorizontal).Weight = xlThin End With 'A列をセンタリング With Range(Cells(StartRow, 1), Cells(i - 1, 1)) .HorizontalAlignment = xlCenter End With db.Close Set db = Nothing End Sub
ただ、DAOよりもADOの方が後発なので覚えるとしたらいいと思いますが。
因みに、同じコードのADOのサンプルです。
Sub Macro1() Dim myCon As ADODB.Connection Dim myRS As ADODB.Recordset Dim mySQL As String Dim mySource As String Dim i As Long Dim j As Integer Dim fc As Integer 'Accessのファイルのパスを指定 mySource = ThisWorkbook.Path & "\sample.mdb" 'テーブルの名前を指定 Const tableName As String = "テーブル名" '開始行 Const StartRow As Long = 5 Set myCon = New ADODB.Connection myCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mySource Set myRS = New ADODB.Recordset mySQL = "SELECT * FROM " & tableName myRS.Open mySQL, myCon fc = myRS.Fields.Count i = StartRow '見出し行の書き出し For j = 1 To fc Cells(i, j).Value = myRS.Fields(j - 1).Name Next j i = i + 1 'レコードセットの書き出し While myRS.EOF = False For j = 1 To fc Cells(i, j).Value = myRS.Fields(j - 1).Value Next i = i + 1 myRS.MoveNext Wend myRS.Close Set myRS = Nothing '表の整形 With Range(Cells(StartRow, 1), Cells(i - 1, fc)) .Borders(xlEdgeLeft).Weight = xlThin .Borders(xlEdgeTop).Weight = xlThin .Borders(xlEdgeBottom).Weight = xlThin .Borders(xlEdgeRight).Weight = xlThin .Borders(xlInsideVertical).Weight = xlThin .Borders(xlInsideHorizontal).Weight = xlThin End With 'A列をセンタリング With Range(Cells(StartRow, 1), Cells(i - 1, 1)) .HorizontalAlignment = xlCenter End With myCon.Close Set myCon = Nothing End Sub
http://www.geocities.jp/cbc_vbnet/DAO/database.html
cells(y,1).value=recset!項目名1
cells(y,2).value=recset!項目名2
cells(y,3).value=recset!項目名3
cells(y,4).value=recset!項目名4
recset.MoveNext '次のレコードに移動
というように できないでしょうか?
項目をそれぞれ出すときは、xは ループにしないで それぞれ記述したほうが わかりやすいですね。
ありがとうございます!
DAOによるデータベース接続のようですね。
データベース接続のコードは定型部分が多いですから、今までの一連の質問からサンプルを作ってみました。
DAOに参照設定をしています。
http://www.accessclub.jp/actips/tips_32.htm
Sub Macro() Dim db As DAO.Database Dim rscset As DAO.Recordset Dim mySQL As String Dim mySource As String Dim i As Long Dim j As Integer Dim fc As Integer 'Accessのファイルのパスを指定 mySource = ThisWorkbook.Path & "\sample.mdb" 'テーブルの名前を指定 Const tableName As String = "テーブル名" '開始行 Const StartRow As Long = 5 Set db = DBEngine.Workspaces(0).OpenDatabase(mySource) Set rscset = db.OpenRecordset(tableName) fc = rscset.Fields.Count i = StartRow '見出し行の書き出し For j = 1 To fc Cells(i, j).Value = rscset.Fields(j - 1).Name Next j i = i + 1 'レコードセットの書き出し While rscset.EOF = False For j = 1 To fc Cells(i, j).Value = rscset.Fields(j - 1).Value Next i = i + 1 rscset.MoveNext Wend rscset.Close Set rscset = Nothing '表の整形 With Range(Cells(StartRow, 1), Cells(i - 1, fc)) .Borders(xlEdgeLeft).Weight = xlThin .Borders(xlEdgeTop).Weight = xlThin .Borders(xlEdgeBottom).Weight = xlThin .Borders(xlEdgeRight).Weight = xlThin .Borders(xlInsideVertical).Weight = xlThin .Borders(xlInsideHorizontal).Weight = xlThin End With 'A列をセンタリング With Range(Cells(StartRow, 1), Cells(i - 1, 1)) .HorizontalAlignment = xlCenter End With db.Close Set db = Nothing End Sub
ただ、DAOよりもADOの方が後発なので覚えるとしたらいいと思いますが。
因みに、同じコードのADOのサンプルです。
Sub Macro1() Dim myCon As ADODB.Connection Dim myRS As ADODB.Recordset Dim mySQL As String Dim mySource As String Dim i As Long Dim j As Integer Dim fc As Integer 'Accessのファイルのパスを指定 mySource = ThisWorkbook.Path & "\sample.mdb" 'テーブルの名前を指定 Const tableName As String = "テーブル名" '開始行 Const StartRow As Long = 5 Set myCon = New ADODB.Connection myCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mySource Set myRS = New ADODB.Recordset mySQL = "SELECT * FROM " & tableName myRS.Open mySQL, myCon fc = myRS.Fields.Count i = StartRow '見出し行の書き出し For j = 1 To fc Cells(i, j).Value = myRS.Fields(j - 1).Name Next j i = i + 1 'レコードセットの書き出し While myRS.EOF = False For j = 1 To fc Cells(i, j).Value = myRS.Fields(j - 1).Value Next i = i + 1 myRS.MoveNext Wend myRS.Close Set myRS = Nothing '表の整形 With Range(Cells(StartRow, 1), Cells(i - 1, fc)) .Borders(xlEdgeLeft).Weight = xlThin .Borders(xlEdgeTop).Weight = xlThin .Borders(xlEdgeBottom).Weight = xlThin .Borders(xlEdgeRight).Weight = xlThin .Borders(xlInsideVertical).Weight = xlThin .Borders(xlInsideHorizontal).Weight = xlThin End With 'A列をセンタリング With Range(Cells(StartRow, 1), Cells(i - 1, 1)) .HorizontalAlignment = xlCenter End With myCon.Close Set myCon = Nothing End Sub
具体的にコードを書いていただき理解しやすいです。ありがとうございます!!
具体的にコードを書いていただき理解しやすいです。ありがとうございます!!