自动引用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)
- 【Access选项卡示例】Ac...(09.09)
- 【Access源码示例】按输入...(09.02)
- 【Access日期区间段查询】...(08.29)
- 【Access日期区间段查询】...(08.27)
- Access怎样才能实现日期时...(08.21)
- 【Access定时打开查询】A...(08.19)
- Access生成固定数量的记录...(08.13)
- Access怎样才能实现日期时...(08.12)
- Access利用导航窗体控件对...(08.03)
学习心得
最新文章
- Access表中的字段名、字段标题...(09.19)
- Access快速开发平台--更改“...(09.18)
- 【中秋及国庆优惠】Access培训...(09.15)
- Access如何将日期型的数值转换...(09.14)
- 英文输入法输入数据中存在单引号引起...(09.11)
- 【Access选项卡示例】Acce...(09.09)
- 让Access光标停留在指定的控件...(09.07)
- 关于Access查询条件里使用通配...(09.06)
- Access报表偷懒制作法--Ac...(09.05)
- Access快速开发平台--窗体数...(09.04)