1. 程式人生 > >VBA訪問access資料庫例項

VBA訪問access資料庫例項

Sub DaoChuExl()
    
    Dim conn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim rowxh As Integer, vhx As Integer, rowzs As Integer, connStr As String, sqlStr As String
    On Error Resume Next
 
    connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ThisWorkbook.Path & "\maildb.mdb;"
    'MsgBox connStr
    Dim ws As Worksheet
    
    Set ws = Sheets("Sheet1")
    conn.Open connStr
    
    vhx = 1
    
    sqlStr = "SELECT Fax_Name, Fax_ModiTime FROM Fax_ReciveInfo where Fax_Update='" & ws.Cells(1, 6).Value & "';"
    
    'MsgBox sqlStr
    rs.Open sqlStr, conn, adOpenStatic, adLockBatchOptimistic


    ws.Range("A2:K65535").ClearContents


    Do While Not rs.EOF
        With ws
            .Range("C2").CopyFromRecordset rs, 65535
        End With
        
        rs.MoveNext
    Loop
    
    
    rowzs = rs.RecordCount + 1
    For rowxh = 2 To rowzs
        ws.Cells(rowxh, 1) = vhx
        ws.Cells(rowxh, 4).NumberFormatLocal = "yyyy-m-d hh:mm:ss"
        vhx = vhx + 1
    Next
    
    
    rs.Close
    Set rs = Nothing
End Sub