尽管accxp网上有很多关于定位链接表的贴子,但还是有很多的朋友询问这方面的问题。应letter网友的提议,结合alex总版主的重新定位链接表文件源码,现将这方面的具体操作介绍如下:
假设前台数据库文件名为frontbase.mdb
后台数据库文件名为backdata.mdb
frontbase当中有链接表tbl1, tbl2, tbl3, …,链接到backdata.mdb中
首先我们要在前台数据库文件的启动窗体加载事件中判断链接是否正确
方法是打开任意一个链接表,假设为tbl1,代码如下:
public function checklinks() as boolean
' 检查到后台数据库的链接;如果链接存在且正确的话,返回 true 。
dim dbs as database, rst as dao.recordset
set dbs = currentdb()
' 打开链接表查看表链接信息是否正确。
on error resume next
set rst = dbs.openrecordset(“tbl1”)
rst.close
' 如果没有错误,返回 true 。
if err = 0 then
checklinks = true
else
checklinks = false
end if
end function
启动窗体的加载事件:
private sub form_load()
if checklinks = false then
docmd.openform “frmconnect”
end if
end sub
frmconnect 连接窗体如下图
[img]f:/m.bmp[/img]
接下来的事情就是如何刷新链接表了。
上面的窗体右边的按钮是用用来调用api打开文件对话框,具体代码如下:
declare function getopenfilename lib "comdlg32.dll" alias "getopenfilenamea" (popenfilename as openfilename) as boolean
type openfilename
lstructsize as long
hwndowner as long
hinstance as long
lpstrfilter as string
lpstrcustomfilter as string
nmaxcustfilter as long
nfilterindex as long
lpstrfile as string
nmaxfile as long
lpstrfiletitle as string
nmaxfiletitle as long
lpstrinitialdir as string
lpstrtitle as string
flags as long
nfileoffset as integer
nfileextension as integer
lpstrdefext as string
lcustdata as long
lpfnhook as long
lptemplatename as string
end type
private sub fileopen_click()
dim ofn as openfilename
dim rtn as string
ofn.lstructsize = len(ofn)
ofn.hwndowner = me.hwnd
ofn.lpstrfilter = "数据库文件 (*.mdb)" & vbnullchar & "*.mdb"
ofn.lpstrfile = space(254)
ofn.nmaxfile = 255
ofn.lpstrfiletitle = space(254)
ofn.nmaxfiletitle = 255
ofn.lpstrinitialdir = currentproject.path
ofn.lpstrtitle = "后台数据文件为"
ofn.flags = 6148
rtn = getopenfilename(ofn)
filename.setfocus
if rtn = true then
filename.text = ofn.lpstrfile
filename.text = filename.text
ok.enabled = true
else
filename.text = ""
end if
end sub
连接按钮刷新链接表 ,代码如下:
private sub ok_click()
dim tabdef as tabledef
for each tabdef in currentdb.tabledefs
if len(tabdef.connect) > 0 then
tabdef.connect = ";database=" & me.filename.text & ";pwd=" + 后台数据库密码
tabdef.refreshlink
end if
next
msgbox "连接成功!"
docmd.close acform, me.name
end sub
其实很简单只有两步,判断链接是否正确和刷新链接表。
新闻热点
疑难解答