当前位置: 代码迷 >> VB >> 为啥第二次按command,就不能添加记录了
  详细解决方案

为啥第二次按command,就不能添加记录了

热度:4519   发布时间:2013-02-26 00:00:00.0
为什么第二次按command,就不能添加记录了
本帖最后由 bcrun 于 2013-01-19 10:26:25 编辑
如题,第一次按command,从外部的EXCEL添加进了access记录,第二次按,就不添加记录了。重启程序才能添加,但也只能按一次。但在进程中,却有EXCEL.EXE存在。

Option Explicit
Dim xlApp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim RowCount As Integer, j As Integer

Dim i As Integer, w As Integer, n As Integer, m As Integer, sFileName As String
Dim strTemp As String
Dim C As Variant
Dim A() As Integer, R As Integer, P As Integer, H As Integer, q As Integer, k As Integer, t As Integer, e As Integer


Private Sub Command11_Click()‘删除全部记录
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\人事档案.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select * from 部门档案"
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1
DataGrid1.Refresh

Adodc1.Recordset.MoveFirst
For t = 1 To Adodc1.Recordset.RecordCount
  Adodc1.Recordset.Delete
  Adodc1.Recordset.MoveNext
  If Adodc1.Recordset.BOF = True Then Adodc1.Recordset.MoveFirst
Next t

End Sub

Private Sub Command6_Click()‘从EXCEL导入
On Error GoTo ErrHandler
CommonDialog1.Filter = "Excel文件 (*.xls)"
CommonDialog1.FilterIndex = 2
CommonDialog1.ShowOpen

If Len(CommonDialog1.FileName) = 0 Then Exit Sub

Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\人事档案.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select * from 部门档案"
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1
DataGrid1.Refresh

Set xlApp = New Excel.Application
xlApp.Visible = False
Set xlbook = xlApp.Workbooks.Open(CommonDialog1.FileName)
Set xlsheet = xlbook.Worksheets(1)
RowCount = Worksheets("Sheet1").Range("A65536").End(xlUp).Row '总行数
For t = 1 To Worksheets("Sheet1").Range("A65536").End(xlUp).Row - 1
  Adodc1.Recordset.AddNew
  Adodc1.Recordset.Fields("单位或个人名称") = Sheets(1).Cells(t + 1, 1)
  Adodc1.Recordset.Fields("地址") = Sheets(1).Cells(t + 1, 2)
  Adodc1.Recordset.Update
Next t

Adodc1.Recordset.MoveFirst
For t = 1 To Adodc1.Recordset.RecordCount
  Adodc1.Recordset.Fields("序号") = t
  Adodc1.Recordset.MoveNext
  If Adodc1.Recordset.BOF = True Then Adodc1.Recordset.MoveFirst
Next t


'If Not xlbook Is Nothing Then xlbook.Close
'If Not xlApp Is Nothing Then xlApp.Quit
'If Not xlsheet Is Nothing Then Set xlsheet = Nothing 'Set xlsheet = Nothing
'If Not xlbook Is Nothing Then Set xlbook = Nothing 'Set xlbook = Nothing
'If Not xlApp Is Nothing Then Set xlApp = Nothing 'Set xlApp = Nothing
  相关解决方案