From Recordset to Excel - Heeyoung-Ahn/Excel_VBA GitHub Wiki
์ปฌ๋ผ์ด๋ฆ ๋ฐํ
For i = 0 To rs.Fields.Count - 1
Cells(1, 1).Offset(0, i).Value = rs.Fields(i).Name
Next i
๋ ์ฝ๋ ๋ฐํ
- ๋ฐฉ๋ฒ1) Range.CopyFromRecordset Method ์ฌ์ฉ
Cells(2, 1).CopyFromRecordset rs
- ๋ฐฉ๋ฒ2) RecordSet.GetRows Method ์ฌ์ฉ
Cells(2, 1).Resize(rs.RecordCount, rs.Fields.Count) = Application.WorksheetFunction.Transpose(rs.GetRows)
- GetRows: ๋ ์ฝ๋ ์งํฉ ๊ฐ์ฒด์ ์ฌ๋ฌ ๋ ์ฝ๋๋ฅผ 2์ฐจ์ ๋ฐฐ์ด๋ก ๋ฐํ(Variant)
- ์ด๋, GetRows ๋ฐฐ์ด์ ์ด X ํ์ผ๋ก ๋์ด ์์ด์ ์์ ์์ ๋ฐ์ ๋ Tranposeํด์ ๋ฐ์์ผ ํจ
UserForm์ ListBox์ ๋ฐํ1
DB โ RecordSet โ UserForm ListBox
With Me.lst1
.ColumnCount = rs.Fields.Count
.ColumnHeads = False
.ColumnWidths = "0,100,50,100,50,70,100,0,50"
.Width = 530
.TextAlign = fmTextAlignLeft
.Font = "๋ง์ ๊ณ ๋"
.Column = rs.GetRows
End With
UserForm์ ListBox์ ๋ฐํ2
DB์ Null๊ฐ ์ ์ด ํ์์ ์ฌ์ฉ
DB โ RecordSet โ Array โ UserForm ListBox
With Me.lst1
.ColumnCount = rs.Fields.Count
.ColumnHeads = False
.ColumnWidths = "0,100,50,100,50,70,100,0,50"
.Width = 530
.TextAlign = fmTextAlignLeft
.Font = "๋ง์ ๊ณ ๋"
End With
If Not rs.EOF Then
ReDim listData(0 To rs.RecordCount - 1, 0 To rs.Fields.Count - 1)
rs.MoveFirst
For i = 0 To rs.RecordCount - 1
For j = 0 To rs.Fields.Count - 1
If IsNull(rs.Fields(j).Value) = True Then
listData(i, j) = ""
Else
listData(i, j) = rs.Fields(j).Value
End If
Next j
rs.MoveNext
Next i
End If
On Error Resume Next
cntRecord = UBound(listData) - LBound(listData) + 1
On Error GoTo 0
If cntRecord = 0 Then
MsgBox "๋ฆฌ์คํธ์ ๋ฐํํ DB ๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.", vbInformation, Banner
argListBox.Clear
Me.txt_count.Value = argListBox.ListCount
Exit Sub
End If
argListBox.List = listData
Me.txt_count.Value = argListBox.ListCount