首页
/
每日頭條
/
科技
/
vba如何将excel數據導入access
vba如何将excel數據導入access
更新时间:2024-12-28 17:28:29

從Access查詢(Select)數據導入到Excel,主要步驟如下:

I 首先使用ADO連接到數據庫;

II 根據需要,設置不同的查詢條件創建查詢記錄集;

III 将記錄集中的數據複制到指定工作表中;

IV 關閉數據庫連接。

代碼如下:

Sub 查詢客戶信息()

Dim cnn As New Connection, rs As New Recordset

Dim strSql As String, i As Long, sh As Worksheet

On Error Resume Next

cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _

& "Data Source=" & ThisWorkbook.Path & "\Northwind.mdb"

strSql = "Select * FROM [客戶] where 客戶ID='GROSR' " '從“客戶”表中獲取數據,如果沒有where子句,則獲取全部

rs.Open strSql, cnn, adOpenStatic '打開記錄集

Set sh = Worksheets.Add '添加工作表

sh.Name = "客戶信息" '設置工作表名稱

With sh

For i = 0 To rs.Fields.Count - 1 '用字段名作為表頭

.Cells(1, i 1) = rs.Fields(i).Name

Next

.Range("A2").CopyFromRecordset rs '複制記錄集中的數據

.Columns.AutoFit '設置列寬為自動适應

End With

rs.Close

cnn.Close

Set rs = Nothing

Set cnn = Nothing

End Sub

vba如何将excel數據導入access(VBA數據庫操作03從Access查詢數據導入到Excel)1

-End-

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
Copyright 2023-2024 - www.tftnews.com All Rights Reserved