自动引用ADO、DAO、Excel
时 间:2017-12-18 15:39:13
作 者:KevinFan ID:47553 城市:东莞
摘 要:由于不同电脑的系统环境(操作系统、Office版本)不一致,需要根据系统环境,在程序打开时自动重新引用ADO、DAO、Excel等。
正 文:
自学了Access一年多,参考了论坛很多大神的作品,自己也陆陆续续开发了一些软件,如进销存、生产统计、财务管理、文件管理,体验了Access快速开发的效率,也为一些兼容性问题头疼,例如开发的系统在不同的电脑上运行,由于不同电脑的系统环境(操作系统、Office版本)不一致,导致用户难于直接运行(有时候未必能满足全部电脑使用相同版本的操作系统和office)。所以根据系统环境,在程序打开时自动重新引用ADO和DAO等就很有必要,论坛也有这样的示例,但较少以系统位数和Office版本来判断,特别是Excel的引用。
我根据论坛的示例,加上自己的实践,重新整合了ADO、DAO、Excel自动引用,附件我使用2.2的平台添加了自动引用的功能,可以下载参考。
附 件:
1、新建一个模块,将文章末尾的代码复制粘贴,然后保存:
2、设计视图打开宏AutoExec,按下图添加执行代码:
'自动重新引用代码:
Option Compare Database
Option Explicit
Public Function ReAddADO()
On Error Resume Next
Dim strMDE As String: strMDE = CurrentDb.Properties("MDE")
If strMDE = "T" Then Exit Function
Dim REFE As References '声明REFE为引用
Dim strFilePath As String '声明strFilePath为文本型变量
Dim rf
Set REFE = Application.References
For Each rf In REFE '在引用中循环查找
If rf.Name = "ado" Then '如果名字为ADO就移动ADO的引用
Application.References.Remove rf '移除
Exit For '退出循环
End If
Next
If FolderExists("C:\Windows\SysWOW64") Then '如果是64位操作系统
strFilePath = "C:\Program Files (x86)\Common Files\System\ado\msado15.dll"
Else '如果是32位操作系统
strFilePath = "C:\Program Files\Common Files\System\ado\msado15.dll"
End If
Set rf = Application.References.AddFromFile(strFilePath) '重新引用ADO
End Function
Public Function ReAddDAO()
On Error Resume Next
Dim strMDE As String: strMDE = CurrentDb.Properties("MDE")
If strMDE = "T" Then Exit Function
Dim REFE As References '声明REFE为引用
Dim strFilePath As String '声明strFilePath为文本型变量
Dim rf
Set REFE = Application.References
For Each rf In REFE '在引用中循环查找
If rf.Name = "DAO" Then '如果名字为DAO就移动DAO的引用
Application.References.Remove rf '移除
Exit For '退出循环
End If
Next
If FolderExists("C:\Windows\SysWOW64") Then '如果是64位操作系统
strFilePath = "C:\Program Files (x86)\Common Files\microsoft shared\DAO\dao360.dll"
Else '如果是32位操作系统
strFilePath = "C:\Program Files\Common Files\Microsoft Shared\DAO\dao360.dll"
End If
Set rf = Application.References.AddFromFile(strFilePath) '重新引用DAO
End Function
Public Function ReAddExcel()
On Error Resume Next
Dim strMDE As String: strMDE = CurrentDb.Properties("MDE")
If strMDE = "T" Then Exit Function
Dim REFE As References '声明REFE为引用
Dim strFilePath As String '声明strFilePath为文本型变量
Dim rf
Set REFE = Application.References
For Each rf In REFE '在引用中循环查找
If rf.Name = "Excel" Then '如果名字为Excel就移动Excel的引用
Application.References.Remove rf '移除
Exit For '退出循环
End If
Next
'这里的Excel启动路径为您电脑上Excel所在的Office文件夹盘符路径,2003版本为Office11、
'2007版本为Office12、2010版本为Office14、2013版本为Office15、2016版本为Office16
'如果是Office2003:
If FolderExists("C:\Program Files (x86)\Microsoft Office\Office11") Then
strFilePath = "C:\Program Files (x86)\Microsoft Office\Office11\EXCEL.EXE"
ElseIf FolderExists("C:\Program Files\Microsoft Office\Office11") Then
strFilePath = "C:\Program Files\Microsoft Office\Office11\EXCEL.EXE"
'如果是Office2007:
ElseIf FolderExists("C:\Program Files (x86)\Microsoft Office\Office12") Then
strFilePath = "C:\Program Files (x86)\Microsoft Office\Office12\EXCEL.EXE"
ElseIf FolderExists("C:\Program Files\Microsoft Office\Office12") Then
strFilePath = "C:\Program Files\Microsoft Office\Office12\EXCEL.EXE"
'如果是Office2010:
ElseIf FolderExists("C:\Program Files (x86)\Microsoft Office\Office14") Then
strFilePath = "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE"
ElseIf FolderExists("C:\Program Files\Microsoft Office\Office14") Then
strFilePath = "C:\Program Files\Microsoft Office\Office14\EXCEL.EXE"
'如果是Office2013:
ElseIf FolderExists("C:\Program Files (x86)\Microsoft Office\Office15") Then
strFilePath = "C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE"
ElseIf FolderExists("C:\Program Files\Microsoft Office\Office15") Then
strFilePath = "C:\Program Files\Microsoft Office\Office15\EXCEL.EXE"
'如果是Office2016或Office365:
ElseIf FolderExists("C:\Program Files (x86)\Microsoft Office\Office16") Then
strFilePath = "C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE"
ElseIf FolderExists("C:\Program Files\Microsoft Office\Office16") Then
strFilePath = "C:\Program Files\Microsoft Office\Office16\EXCEL.EXE"
End If
Set rf = Application.References.AddFromFile(strFilePath) '自动引用Excel
End Function
Function FileExists(ByVal strFile As String, Optional bFindFolders As Boolean) As Boolean
Dim lngAttributes As Long
lngAttributes = (vbReadOnly or vbHidden or vbSystem)
If bFindFolders Then
lngAttributes = (lngAttributes or vbDirectory)
Else
Do While Right$(strFile, 1) = "\"
strFile = Left$(strFile, Len(strFile) - 1)
Loop
End If
On Error Resume Next
FileExists = (Len(Dir(strFile, lngAttributes)) > 0)
End Function
Function FolderExists(strPath As String) As Boolean
On Error Resume Next
FolderExists = ((GetAttr(strPath) And vbDirectory) = vbDirectory)
End Function
Access软件网QQ交流群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- 统计当月之前(不含当月)的记录...(03.11)
- 【Access Inputbo...(03.03)
- 按回车键后光标移动到下一条记录...(02.12)
- 【Access Dsum示例】...(02.07)
- Access对子窗体的数据进行...(02.05)
- 【Access高效办公】上月累...(01.09)
- 【Access高效办公】上月累...(01.06)
- 【Access Inputbo...(12.23)
- 【Access Dsum示例】...(12.16)

学习心得
最新文章
- 仓库管理实战课程(11)-人性化操...(04.15)
- 32位的Access软件转化为64...(04.12)
- 【Access高效办公】如何让vb...(04.11)
- 仓库管理实战课程(10)-入库功能...(04.08)
- Access快速开发平台--Fun...(04.07)
- 仓库管理实战课程(9)-开发往来单...(04.02)
- 仓库管理实战课程(8)-商品信息功...(04.01)
- 仓库管理实战课程(7)-链接表(03.31)
- 仓库管理实战课程(6)-创建查询(03.29)
- 仓库管理实战课程(5)-字段属性(03.27)