【发布时间】: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语句的问题。