首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

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

2013-02-17 
为什么第二次按command,就不能添加记录了本帖最后由 bcrun 于 2013-01-19 10:26:25 编辑如题,第一次按comm

为什么第二次按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


'Exit Sub

ErrHandler: '用户按“取消”按钮。
  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
  Exit Sub
  End Sub


Private Sub Adodc1_MoveComplete(ByVal adReason As ADODB.EventReasonEnum, ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
If Adodc1.Recordset.RecordCount = 0 Then
    Adodc1.Caption = "没有记录!"
  Else
    If Str(Adodc1.Recordset.AbsolutePosition) < 0 Then
        Adodc1.Caption = "    现在是第 " & Adodc1.Recordset.RecordCount & " 条记录" & " / " & "共 " & Str(Adodc1.Recordset.RecordCount) & " 条记录"
      Else
        Adodc1.Caption = "    现在是第 " & Str(Adodc1.Recordset.AbsolutePosition) & " 条记录" & " / " & "共 " & Str(Adodc1.Recordset.RecordCount) & " 条记录"
    End If
End If
End Sub

Private Sub Form_Load()
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

End Sub



[解决办法]
要想找到错误,就得先把On Error这种语句都去掉.

错误往往就是在你自己以为已经解决了错误的时候发生的. 

热点排行