VBA訪問access資料庫例項

2021-09-02 16:16:21 字數 1320 閱讀 8653

最近,同事要用乙個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

最後的成果是:

C 訪問Access資料庫

我編寫這個程式的動機是當我希望用c sharp訪問msaccess資料庫的時候我沒有辦法獲得任何資訊和參考材料.網上所能獲得的所有材料都是偏重於sql的,所以我們將分兩步來編寫這個應用程式,第一我們將展示如何連線到msaccess資料庫然後看看它有多複雜.最後,我們就這樣完成了這個程式.閒言少序,讓...

c 訪問Access資料庫

using system using system.io using system.collections using system.data using system.data.oledb namespace exam manage.dal protected static string conn...

C 訪問Access資料庫

我編寫這個程式的動機是當我希望用c sharp訪問msaccess資料庫的時候我沒有辦法獲得任何資訊和參考材料.網上所能獲得的所有材料都是偏重於sql的,所以我們將分兩步來編寫這個應用程式,第一我們將展示如何連線到msaccess資料庫然後看看它有多複雜.最後,我們就這樣完成了這個程式.閒言少序,讓...