设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1686|回复: 1
打印 上一主题 下一主题

[表] operation is not allowed in this context (有实例)

[复制链接]
跳转到指定楼层
1#
发表于 2007-6-28 00:40:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
代码如下:我是要把Excel中的数据导到Table中。如果在执行 下面的代码的时候 有ERROR 就会跳转到ErrorHandler  并有 MsgBox Err.Description
然后如果我再一次执行的话  程序执行到  If rst.State = adStateOpen Then rst.Close  时就会报“下面这个错误,这是什么原因阿 ?
operation is not allowed in this context” 


Public Sub ExcelInput(strwb As String, strws As String, strtable As String, strkey As String)
On Error GoTo ErrorHandler
    Dim myData As String, myTable As String
    Dim xlApp As New Excel.Application
    Dim wb As Excel.Workbook
    Dim ws As New Excel.WorkSheet
    Dim myDataArrary As Variant
    Dim strsql As String
    Dim myDataArray As Variant
    Dim i, j, n As Integer
   
    Set wb = xlApp.Workbooks.Open(strwb)
    Set ws = wb.Sheets(1)
    myTable = strtable
   
   With ws.Cells(1, 1).CurrentRegion
        myDataArray = .Value
    End With
   
    Set Conn = CurrentProject.Connection
    n = ws.Range("A65536").End(xlUp).Row
    For i = 2 To n
        strsql = "select * from " & myTable & " where " & strkey & "='" & ws.Cells(i, 1).Value & "'"
        If rst.State = adStateOpen Then rst.Close
        rst.Open strsql, Conn, adOpenKeyset, adLockOptimistic
        If rst.RecordCount = 0 Then
            rst.AddNew
            For j = 1 To rst.Fields.count - 1
                rst.Fields(j - 1) = myDataArray(i, j)
            Next j
            rst.Fields("updatedate") = Now
            rst.Update
        Else
            For j = 1 To rst.Fields.count - 1
                rst.Fields(j - 1) = myDataArray(i, j)
            Next j
             rst.Fields("updatedate") = Now
            rst.Update
        End If
    Next i
    MsgBox "数据保存完毕!"
    If rst.State = adStateOpen Then rst.Close
    Conn.Close
    xlApp.Workbooks.Close
    xlApp.Quit
    Set wb = Nothing
    Set ws = Nothing
    Set rst = Nothing
    Set Conn = Nothing
      
    Exit Sub
ErrorHandler:
   
      xlApp.Workbooks.Close
    xlApp.Quit
    MsgBox Err.Description
End Sub
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2007-6-28 11:42:00 | 只看该作者
将 错误处理

On Error GoTo ErrorHandler 等注释,在查找问题,最好能上传实例.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-6-11 02:51 , Processed in 0.121416 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表