如何在vba中创建和连接从访问表派生的多个临时表?

时间:2016-08-18 09:38:17

标签: vba excel-vba ms-access excel

我遇到的情况是我必须根据访问表中的不同子设置条件创建多个临时表,并执行内部联接以创建最终表。您能否建议我如何将信息从ADO记录集复制到临时表并执行内连接?

1 个答案:

答案 0 :(得分:0)

以下是一些可以开始的小样本:

Sub SQLCombineExample()
    Dim con As ADODB.Connection
    Dim rs As ADODB.Recordset
    Set con = New ADODB.Connection
    con.Open "Driver={Microsoft Excel Driver (*.xls)};" & _
           "DriverId=790;" & _
           "Dbq=" & ThisWorkbook.FullName & ";" & _
           "DefaultDir=" & ThisWorkbook.FullName & ";ReadOnly=False;"
    Set rs = New ADODB.Recordset
    Set rs = con.Execute("select distinct a.test1, b.test3 from [Sheet1$] as a , [Sheet1$] as b ")
    Range("f1").CopyFromRecordset rs
    Set rs = Nothing
    Set con = Nothing
End Sub