站长中国
设为首页 | 站长论坛

站长论坛 站长下载
您所在的位置: 站长中国 > 站长学院 > 数 据 库 > MSAccess > 正文

如何在Access中编写代码完成自动清空数据及导入数据?
  2007年10月15日21:43:06  评论(0条) 字体:[ ]
相关热点: 导入 自动 完成 编写 代码 strTDef strPath

Sample Code to Import All Database Objects

Summary
This article describes how you to use Data Access Objects (DAO) to import all objects from one Microsoft Access database into the current Access database. In some situations, this code can be used to recover database objects from a corrupted or damaged database that can be opened but cannot be compacted successfully. This code does not import the following elements: 

References 


Import/Export specifications 


Security information (user and group permissions) 


The Current User (usually the Administrator) becomes the owner of all imported objects. 



More Information
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: 

http://www.microsoft.com/partner/referral/
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: 
http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
To import all the objects from another database into the current database, follow these steps: 
Start Access, and then open the database into which you want to import objects.

This may be a new blank database. 


In the Database window, click Modules, and then click New. 


On the Tools menu, click References. Make sure that Microsoft DAO 3.0 Object Library or later is selected in the list of references, and then click OK. 


Type or paste the following code in the module window: 


Option Compare Database

Option Explicit

Public Function ImportDb(strPath As String) As Boolean

On Error Resume Next

Dim db As Database 'Database to import
Dim td As TableDef 'Tabledefs in db
Dim strTDef As String 'Name of table or query to import
Dim qd As QueryDef 'Querydefs in db
Dim doc As Document 'Documents in db
Dim strCntName As String 'Document container name
Dim x As Integer 'For looping
Dim cntContainer As Container 'Containers in db
Dim strDocName As String 'Name of document
Dim intConst As Integer
Dim cdb As Database 'Current Database
Dim rel As Relation 'Relation to copy
Dim nrel As Relation 'Relation to create
Dim strRName As String 'Copied relation's name
Dim strTName As String 'Relation Table name
Dim strFTName As String 'Relation Foreign Table name
Dim varAtt As Variant 'Attributes of relation
Dim fld As Field 'Field(s) in relation to copy
Dim strFName As String 'Name of field to append
Dim strFFName As String 'Foreign name of field to append

'Open database which contains objects to import

Set db = DBEngine.Workspaces(0).OpenDatabase(strPath, True)


'Import tables from specified Access database

For Each td In db.TableDefs

strTDef = td.Name

If Left(strTDef, 4) <> "MSys" Then

DoCmd.TransferDatabase acImport, "Microsoft Access", strPath, acTable, _
strTDef, strTDef, False

End If

Next


'Import queries

For Each qd In db.QueryDefs

strTDef = qd.Name

DoCmd.TransferDatabase acImport, "Microsoft Access", strPath, acQuery, _
strTDef, strTDef, False

Next


'Copy relationships to current database

Set cdb = CurrentDb

For Each rel In db.Relations

With rel

'Get properties of relation to copy

strRName = .Name
strTNam



收藏本文 打印 打印本文  推荐本文 告诉好友 投稿 投稿邮箱

站长排行

学院

新闻

专栏

盈利

[揭密网络黄链]中国留学生买凶专破日本
windows 2008中文版iis7+asp+php+Acces
贴吧发帖机使用教程(绝对原创)
超强弹出窗口代码,什么都挡不住
Windows Vista局域网网络连接设置
JSP语法(6)
FLASH视觉特效实例之地震效果
关于数据分页(转自www.codeproject.co
教你如何在Vista中设置麦克风
不用Photoshop 在线恶搞大头像
全国哀悼日网友自发QQ隐身上线 向死难
淘宝网卖家公然叫卖“艳照门”照片集
抗震救灾/鱼目混珠?
Google绿色专家质疑黑色背景网页节省资
阿里妈妈广告卖主全攻略
电影小说类相关关键字搜索量列表
爆笑 阿里就这样把亿告征服!
大脚:实战单页面优化关键词之——淘宝
国内各IT企业办公环境揭秘(多图)
心系灾区,点燃希望——手机QQ2008祈福
ECSHOP模板制作参考文档
悬挂阿里妈妈会否被百度惩罚
ECSHOP模板下载
阿里妈妈是否是中小站长的救世主?
最强网店ECShop发新版 众多酷炫功能给
土豆网,优酷网,爆米花等视频网站采集
DedeCms模板安装/制作概述
网上商店系统巅峰对决 ECShop vs ShopE
编程中国全站采集规则
http://www.mt86.com全站采集规则
性福联盟 一个不尊重站长的联盟
百度网络广告利润增长将会持续多久
大脚:日赚100元—揭露最新firefox欺骗
分享经验:个人网站如何月赚1万!
大脚:垃圾站超级赚钱法之二—突破“站
迅雷联盟、快车联盟收入对比
大脚:垃圾站超级赚钱法之——前言
Google Adsense的秘密 第二版
风险投资公司vc联系方式名录大全(一)
经理人必看的十个管理网站
站长学院  网页设计 建站教程 图形图象 网络编程

制定完整的网站
制定完整的网站
中国企业网站的
中国企业网站的

网站运营之怎么样来粘住你的用
网麒:网站建设创意
怎样提高网页快照更新
两种方法,教你如何通过微软正
英文网站优化细节
利用SSL加密增强FTP服务器的安
网页设计常用Javascript经典代

新闻线索

如果你有站长界人事变动、重组并购、变革技术出现,以及产品投诉等重要新闻线索,请告诉我们,我们会给予特别关注。
0631-3653338
站长中国编辑部
站长中国24小时新闻热线: 13156089008