2014-04-25 22 views
0

当我运行这段代码,我碰到一个“开放形式操作被取消”与2501错误代码行就被逮住时,我debbug是DoCmd.RunSQL(要求)错误2501 VBA存取权限

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

当您尝试DoCmd时,发布'Req'中的sql值。 –

+2

您报告了错误消息参考打开窗体,但您的代码没有提及窗体。建议您将VBA过程与任何表单事件隔离开来。至少Access可能会给你一个错误信息,它更好地描述了INSERT语句的问题。 – HansUp

回答

0

而不是做的:DoCmd.RunSQL(要求),

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

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

0

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