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
| Sub test() Dim cnn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim SQL As String Dim tblName Dim dbAddr
dbAddr = ThisWorkbook.Path & "\地址信息.accdb" tblName = "地址"
With cnn .Provider = "Microsoft.ACE.OLEDB.12.0" .Open "Data Source=" & dbAddr End With
opFilds = "省份"
SQL = "Select distinct " & opFilds & " from " & tblName Set rs = cnn.Execute(SQL) Dim sht Dim fildNum Set sht = ThisWorkbook.Worksheets("示例") sht.Cells.ClearContents
fildNum = rs.Fields.Count For j = 0 To fildNum - 1 Step 1 fildName = rs.Fields(j).Name sht.Cells(1, j + 1) = fildName Next j
sht.Cells(2, 1).CopyFromRecordset rs
cnn.Close Set rs = Nothing Set cnn = NothingEnd Sub
|