Debug。Print fld。Name & 〃=〃 & fld。Value & vbCr
Next
在获取第一条记录的数据后,过程使用了Close方法关闭Recordset和对Access数据库的链接:
rst。Close
con。Close
ADOOpenJetDatabase过程如下:
Sub ADOOpenJetDatabase()
Dim con As New ADODBnnection
Dim rst As New ADODB。Recordset
Dim fld As ADODB。Field
' Connect with the database
con。Open _
〃Provider=Microsoft。Jet。OLEDB。4。0;〃 _
& 〃Data Source=C:Program FilesMicrosoft Office” _
& 〃OfficeSamplesNorthWind。mdb;〃
' Open Recordset based on the SQL statement
rst。Open 〃SELECT * FROM Customers 〃 & _
〃WHERE City = 'London'〃; con; _
adOpenForwardOnly; adLockReadOnly
' Print the values for the fields in
' the first record in the debug window
For Each fld In rst。Fields
Debug。Print fld。Name & 〃=〃 & fld。Value & vbCr
Next
' Close the Recordset and connection with Access
rst。Close
con。Close
' Destroy object variables to reclaim the resources
Set rst = Nothing
Set con = Nothing
End Sub
7。从 Excel 执行 Access 任务
从Excel链接到Access后,你就可以执行Access应用软件的不同任务。本节示范如何使用VBA代
码来:
□ 创建新Access数据库
□ 打开现存在的数据库表
□ 创建全新的数据库表
□ 打开数据库报表
□ 运行Access函数
8。创建新 Access 数据库
如果你想要通过编程将Excel数据传送到一个新的Access数据库里面,那么你需要使用VBA代
码创建一数据库。下面的过程示例 示范了如何使用DAO来建立和Access的链接。Workspace对象
的CreateDatabase方法创建一个名为ExcelDump。mdb的新数据库 于C盘根目录下。然后,
Database对象的CreateTableDef方法用来创建一个名为tblStates的表。在表能够添加到数据
库之前,必 须先创建一个字段并附在该表上。该过程创建了三个文本字段(dbText),每个分
别可以储存2,25和25个字符。每个字段创建 后,使用Append方法将这些字段添加到TableDef
296
… 页面 313…
对象的Fields集合里。字段一旦创建并添加到表之后,表本身就会使用Append 方法被添加到
数据库。因为名为“C:ExcelDump。mdb”的数据库可能已经存在于该目录下,该过程包括了一
个错误处理程序,将 删除现有文件,以确保数据库创建过程继续。因为其它错误也可能发生,
Else子句包括了显示错误描述的信息,并允许退出过程
Sub NewDB_DAO()
Dim db As DAO。Database
Dim tbl As DAO。TableDef
Dim strDb As String
Dim strTbl As String
On Error GoTo Error_CreateDb_DAO
strDb = 〃C:ExcelDump。mdb〃
strTbl = 〃tblStates〃
' Create a new database named ExcelDump
Set db = CreateDatabase(strDb; dbLangGeneral)
' Create a new table named tblStates
Set tbl = db。CreateTableDef(strTbl)
' Create fields and append them to the Fields collection
With tbl
。Fields。Append 。CreateField(〃StateId〃; dbText; 2)
。Fields。Append 。CreateField(〃StateName〃; dbText; 25)
。Fields。Append 。CreateField(〃StateCapital〃; dbText; 25)
End With
' Append the table object to the TableDefs
db。TableDefs。Append tbl
' Close the database
db。Close
Set db = Nothing
MsgBox 〃There is a new database on your hard disk。 〃 & vbCrLf _
& 〃This database file contains a table 〃 & strDb & vbCrLf _
& 〃named 〃 & strTbl & 〃。〃 & vbCrLf _
& 〃Before you activate this database; close the Excel application。〃
Exit_CreateDb_DAO:
Exit Sub
Error_CreateDb_DAO:
If Err。Number = 3204 Then
' Delete the database file if it already exists
Kill 〃C:Exceldump。mdb〃
Resume
Else
MsgBox Err。Number & 〃: 〃 & Err。Description
Resume Exit_CreateDb_DAO
End If
End Sub
297
… 页面 314…
图15…7 Excel VBA过程创建的Access数据库表
9。打开 Access 窗体
你可以从Excel里打开Access窗体。你也可以创建新窗体。下述例子使用自动控制链接到
Access。一旦链接建立后,就使用 OpenCurrentDatabase方法来打开例子Northwind数据库。
接着,使用DoCmd对象的OpenForm方法打开Customers窗体。该窗 体被打开为普通视图
(acNormal)。如果要将窗体在设计视图里打开的话,那么可以使用acDesign常数代替。DoCmd
对象的 Restore方法确保该窗体显示在屏幕上而不是最小化。Access应用软件对象
(objAccess)的Visible属性必须设置为True,以确 保窗体可见。注意,Access应用软件的
对象变量(objAccess)在模块上面声明。为了让该过程运行正确,你必须建立对Access对象库
的引用。图15…8显示了被打开的Customers窗体。
‘ declare at the top of the module
Dim objAccess As Access。Application
Sub DisplayAccessForm()
Dim strDb As String
Dim strFrm As String
strDb = 〃C:Program FilesMicrosoft Office” _
& 〃OfficeSamplesNorthwind。mdb〃
strFrm = 〃Customers〃
Set objAccess = New Access。Application
With objAccess
。OpenCurrentDatabase(strDb)
。DoCmd。OpenForm strFrm; acNormal
。DoCmd。Restore
。Visible = True
End With
End Sub
298
… 页面 315…
图15…8 可以用Excel VBA过程打开的Access窗体
如果你还想在编程中再进一步的话,那么从Excel VBA过程里创建一个全新的Access窗体,如下
所示:
‘ declare at the top of the module
Dim obAccess As Access。Application (译者:原文为myAccess)
Sub CreateAccessForm()
Dim myForm As Form
Dim myDb As String
Dim myCtrl As Control
Dim strFrmName As String
On Error GoTo Error_CreateForm
myDb = 〃C:Program FilesMicrosoft Office” _
& 〃OfficeSamplesNorthwind。mdb〃
strFrmName = 〃frmCustomForm〃
Set obAccess = New Access。Application
obAccess。OpenCurrentDatabase myDb
Set myForm = obAccess。CreateForm
myForm。Caption = 〃Form created by Excel〃
myForm。RecordSource = 〃Employees〃
obAccess。DoCmd。Save ; strFrmName
' Create a label and text box on the form
Set myCtrl = CreateControl(FormName:=strFrmName; _
ControlType:=acLabel; _
Left:=1000; Top:=1000)
myCtrl。Caption = 〃Last Name:〃
myCtrl。SizeToFit
Set myCtrl = CreateControl(FormName:=strFrmName; _
ControlType:=acTextBox; _
Parent:=〃〃; _
299
… 页面 316…
ColumnName:=〃LastName〃; _
Left:=2200; Top:=1000)
With obAccess
With 。DoCmd
。Save ; strFrmName
。Close acForm; strFrmName
End With
。CloseCurrentDatabase
。Quit
End With
Set obAccess = Nothing
MsgBox 〃In the Northwind database there is now 〃 & vbCrLf _
& 〃a new form named 〃 & strFrmName & 〃。〃 & vbCrLf _
& 〃Close Excel prior to opening the Northwind 〃 & vbCrLf _
& 〃database to view this form。〃
ErrorHandler:
Exit Sub
Error_CreateForm:
MsgBox Err & 〃 :〃 & Err。Description
Resume ErrorHandler
End Sub
图15…9 Access窗体可以由Excel VBA过程创建(参见上面的
CreateAccessForm过程代码)
10。打开 Access 报表
你可以从Excel里打开Access报表。下述过程示范了如何直接从
Excel里显示已经存在的Access报表。
‘ declare at the top of the module
Dim objAccess As Access。Application
Sub DisplayAccessReport()
Dim strDb As String
Dim strRpt As String
strDb = 〃C:Program FilesMicrosoft Office” _
& 〃OfficeSamplesNorthwind。mdb〃
strRpt = 〃Products by Category〃
Set objAccess = New Access。Application
With objAccess
。OpenCurrentDatabase (strDb)
。DoCmd。OpenReport strRpt; acViewPreview
。DoCmd。Maximize
。Visible = True
End With
End Sub 下面的过程更通用,因为它允许你在任意Access数据库里显示任意Access报表。注意,
该过程�