【问题标题】:error 2501 access vba错误 2501 访问 vba
【发布时间】:2014-04-25 09:58:39
【问题描述】:

当我运行此代码时,我得到“打开表单操作已取消”,错误代码为 2501 当我调试时它被捕获的行是 DoCmd.RunSQL (Req)

Function Compare()
    Dim oDB As DAO.Database   
    Dim oRst As DAO.Recordset
    Dim nbligne As Long
    Dim Req As String
    Dim default As String
    Dim tables As String
    Dim table
    Dim i As Integer
    Dim champ As String
    Dim j As Integer

    Set oDB = CurrentDb
    Set oRst = oDB.OpenRecordset("SELECT Count(*) FROM CELLCAC;")
    nbligne = oRst.Fields(0).Value
    Set oRs = CurrentDb.OpenRecordset("CELLCAC")
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set fldr = fs.getfolder("C:\Users\Documents\Application\Application_vba\Delta")
    Set fls = fldr.files
    Set fld = CurrentDb.OpenRecordset("TABLES_A_VERIFIER_DEFAULT")

    For Each fl In fls
        If fl Like "*.txt" Then
            source = Left(fl.Name, Len(fl.Name) - 4)
            tables = source
            default = "DEFAULT_" & tables
            table = CurrentDb.OpenRecordset(default)
            For i = 0 To table.Count - 1
                champ = table(i).Name
                Req = "INSERT INTO DELTA_DEFAULT(BSCNAME, CELLNAME, MO, PARAMETRE ,DEFAULT ,RESEAU)" _
                & "select DISTINCT  [" & tables & "]![BSCNAME], [" & tables & "]![CELLNAME],('" & tables & "'), ('" & champ & "')  ,[" & default & "]![" & champ & "],[" & tables & "]![" & champ & "] " _
                & " from (" & tables & ")  INNER join (" & default & ") on ( " & default & ".Zone = " & tables & ".Zone ) " _
                & " Where [" & default & "]![" & champ & "] <> [" & tables & "]![" & champ & "];"
               DoCmd.RunSQL (Req)
            Next i
        End If
    Next fl
End Function

【问题讨论】:

  • 当你尝试 DoCmd 时发布 'Req' 中的 sql 值。
  • 您报告的错误消息引用了打开表单,但您的代码没有提及表单。建议您将 VBA 过程与任何表单事件隔离开来进行测试。至少 Access 可能会给您一条错误消息,更好地描述 INSERT 语句的问题。

标签: ms-access vba


【解决方案1】:

而不是这样做:DoCmd.RunSQL (Req),

你可以试试吗:oDB.Execute (Req)

我遇到了几个问题,这对我很有帮助!

【讨论】:

    【解决方案2】:

    req 查询的第一行到第二行,您需要添加一个空格字符。

    我相信,如果将单词 SELECT 连接到右括号,当前查询将无法识别它。

    Req = "INSERT INTO DELTA_DEFAULT(BSCNAME, CELLNAME, MO, PARAMETRE,DEFAULT ,RESEAU)" _ 
    & " select
    

    另外;据我了解,在创建INSERT INTO 语句时,我会使用VALUES 子句。也许:

    Req = "INSERT INTO DELTA_DEFAULT(BSCNAME, CELLNAME, MO, PARAMETRE ,DEFAULT ,RESEAU)" _
                    & " VALUES (select DISTINCT  [" & tables & "]![BSCNAME], [" & tables & "]![CELLNAME],('" & tables & "'), ('" & champ & "')  ,[" & default & "]![" & champ & "],[" & tables & "]![" & champ & "] " _
                    & " from (" & tables & ")  INNER join (" & default & ") on ( " & default & ".Zone = " & tables & ".Zone ) " _
                    & " Where [" & default & "]![" & champ & "] <> [" & tables & "]![" & champ & "]);"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      相关资源
      最近更新 更多