错误2501访问vba

时间:2014-04-25 09:58:39

标签: vba ms-access access-vba

当我运行此代码时,我得到“Open Form操作被取消”,错误代码为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

2 个答案:

答案 0 :(得分:0)

而不是:DoCmd.RunSQL(Req),

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

我有几个问题,这对我有很大的帮助!

答案 1 :(得分:0)

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

我相信,如果将SELECT连接到封闭式括号,则该查询目前无法识别该字。{/ p>

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

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

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 & "]);"