最大的網站源碼資源下載站,
用access做應用程序,往往做成"數據"和"程序"兩個數據庫,"數據"數據庫中只存放數據表,"程序"數據庫中存放所有的form,query,report,module等,通過表聯接"程序"數據庫可以訪問所有"數據"數據庫中的表。當我們的應用程序更換存儲位置的時候往往失去正確的表聯接,造成運行錯誤。下面的一段程序可以自動找回表聯接。在我的應用中"程序"數據庫名字是stockmgr.mdb,"數據"數據庫名字是stock-data.mdb
注意: 該程序僅限于"程序"和"數據"存放在同一個目錄而且是對應單個"數據"文件的情況。
function reattachtable()
dim mydb as database, mytbl as tabledef
dim cpath as string
dim datafiles as string, i as integer
on error resume next
set mydb = currentdb
cpath = trimfilename(currentdb.name)
datafiles = "stock-data.mdb"
docmd.hourglass true
for i = 0 to mydb.tabledefs.count - 1
set mytbl = mydb.tabledefs(i)
if mytbl.attributes = db_attachedtable and left(mytbl.connect, 1) = ";" then
mytbl.connect = ";database=" & cpath & datafiles
mytbl.refreshlink
if err then
if vbno = msgbox(err.description & ",繼續嗎?", vbyesno) then exit for
end if
end if
next i
docmd.hourglass false
msgbox "tables relink finish."
end function
'絕對路徑中去掉文件名,返回路徑
function trimfilename(fullname as string) as string
dim slen as long, i as long
slen = len(fullname)
for i = slen to 1 step -1
if mid(fullname, i, 1) = "/" then
exit for
end if
next
trimfilename = left(fullname, i)
end function
在程序啟動或者按鈕動作中調用即可。
新聞熱點
疑難解答