解决办法: 模块中代码: Public conn, rst Sub ImportToAccess() '导入ACCESS Dim Sql$, Path$ Sheets("2").Activate ARR = Range("A1:J" & [A65536].End(xlUp).Row) arrnum = UBound(ARR) '记录个数 With rst .Open "SELECT * FROM DATA", conn, AdOpenForwardOnly, AdLockOptimistic For R = 1 To arrnum .AddNew For i = 1 To 10 '字段个数 .Fields(i).Value = ARR(R, i) Next .Update Next .Close End With End Sub 窗体中代码: Private Sub CommandButton1_Click() '导入按钮 Call ImportToAccess End Sub Private Sub CommandButton2_Click() '退出按钮 Set rst = Nothing conn.Close: Set conn = Nothing ThisWorkbook.Close False End Sub Private Sub UserForm_Initialize() '窗体初始化 Set conn = CreateObject("ADODB.Connection") Set rst = CreateObject("ADODB.Recordset") conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ThisWorkbook.Path & "\DATA.MDB;Jet OLEDB:Database Password=" & "123" End Sub Private Sub UserForm_Terminate() '窗体关闭 Set rst = Nothing conn.Close: Set conn = Nothing End Sub 原来的连接方式: "INSERT into [DATA] SELECT * FROM [Excel 8.0;DATABASE=" & ThisWorkbook.FullName & ";HDR=yes].[Excel表$]" 我认为是一种隐形连接,无法用 conn.Close: Set conn = Nothing 的形式断开连接,置空对象,改成上面的形式后,就可以做到了。
4XpoYIvK.rar
(43.3 KB, 下载次数: 44)
[此贴子已经被作者于2007-1-26 22:55:05编辑过] |