VBA访问access数据库实例

最近,同事要用一个excel文件导出每天邮件的信息,邮件基本信息都存在了access数据表中了,要实现这个功能有好几种方式,例如写一个qty文件,而我直接用了excel自带的VBA功能,写了一个宏,根据输入的日期直接调用,想要哪一天的就导哪一天的!不能白瞎了我这个程序猿的称号啊,大笑大笑

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

最后的成果是:

猜你喜欢

转载自zhangshufei8001.iteye.com/blog/2377268