自动引用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交流群 (群号:483923997) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- Access两种方式实现即时更...(03.01)
- Access隐藏与显示lacc...(01.12)
- 【Access高效办公】将每个...(12.23)
- Access21点游戏源代码(12.13)
- 【Access窗体导出Exce...(11.15)
- 【Access开发】Acces...(11.14)
- 通过Access宏录入数据到选...(11.10)
- 用DLOOKUP函数将需求表中...(10.31)
- Access日期区间段查询数据...(10.25)
学习心得
最新文章
- Access快速开发平台--在WI...(03.08)
- 使用SQL语句删除xscj表中学号...(03.08)
- Access快速开发平台进销存教程...(03.07)
- Access快速开发平台--frm...(03.06)
- 【Access删除查询】删除数字最...(03.06)
- Access快速开发平台进销存教程...(03.05)
- Access快速开发平台进销存教程...(03.04)
- Access快速开发平台--IsL...(03.02)
- Access两种方式实现即时更新月...(03.01)
- Access开发的资金管理系统;基...(02.29)