我遇到了一个问题,我设置了一些VBA代码,以便在我的表中添加大量记录。代码编译没有问题,并且在运行时不会触发任何错误,但是当我返回并查看表中的记录时,只创建了大约一半的必要记录
这是代码
Private Sub btnAddEnt_Click()
Dim strEnt As String
Dim strRev As String
Dim strDir As String
Dim strInd As String
Dim strDep As String
Dim strARev As String
Dim strADir As String
Dim strAInd As String
Dim strADep As String
strEnt = "INSERT INTO EntList (EntityID, BusinessUnit, EntityName, Location, Client,Dept) " & _
"VALUES ('" & Me.EntityID & "', '" & Me.BusinessUnit & "', '" & Me.EntityName & "', '" & Me.Location & "', '" & Me.Client & "', '" & Me.Dept & "');"
strRev = "INSERT INTO ForcastTrans (Location, Client, Department, Account, EntityID, Description, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
"VALUES ('" & Me.Location & "', '" & Me.Client & "', '" & Me.Dept & "', '4151 SET UP', '" & Me.EntityID & "', 'Default', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);"
strDir = "INSERT INTO ForcastTrans (Location, Client, Department, Account, EntityID, Description, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
"VALUES ('" & Me.Location & "', '" & Me.Client & "', '" & Me.Dept & "', '5855 OTHER', '" & Me.EntityID & "', 'Default', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);"
strInd = "INSERT INTO ForcastTrans (Location, Client, Department, Account, EntityID, Description, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
"VALUES ('" & Me.Location & "', '" & Me.Client & "', '" & Me.Dept & "', '6055 SALARIES/WAGES - OTHER', '" & Me.EntityID & "', 'Default', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);"
strDep = "INSERT INTO ForcastTrans (Location, Client, Department, Account, EntityID, Description, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
"VALUES ('" & Me.Location & "', '" & Me.Client & "', '" & Me.Dept & "', '6950 DEPRECIATION', '" & Me.EntityID & "', 'Default', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);"
strARev = "INSERT INTO ActualTrans (Location, Client, Department, Account, EntityID, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
"VALUES ('" & Me.Location & "', '" & Me.Client & "', '" & Me.Dept & "', '4151 SET UP', '" & Me.EntityID & "', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);"
strADir = "INSERT INTO ActualTrans (Location, Client, Department, Account, EntityID, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
"VALUES ('" & Me.Location & "', '" & Me.Client & "', '" & Me.Dept & "', '5855 OTHER', '" & Me.EntityID & "', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);"
strAInd = "INSERT INTO ActualTrans (Location, Client, Department, Account, EntityID, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
"VALUES ('" & Me.Location & "', '" & Me.Client & "', '" & Me.Dept & "', '6055 SALARIES/WAGES - OTHER', '" & Me.EntityID & "', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);"
strADep = "INSERT INTO ActualTrans (Location, Client, Department, Account, EntityID, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
"VALUES ('" & Me.Location & "', '" & Me.Client & "', '" & Me.Dept & "', '6950 DEPRECIATION', '" & Me.EntityID & "', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);"
Debug.Print strEnt
Debug.Print strRev
Debug.Print strDir
Debug.Print strInd
Debug.Print strDep
Debug.Print strARev
Debug.Print strADir
Debug.Print strAInd
Debug.Print strADep
CurrentDb.Execute strEnt
CurrentDb.Execute strRev
CurrentDb.Execute strDir
CurrentDb.Execute strInd
CurrentDb.Execute strDep
CurrentDb.Execute strARev
CurrentDb.Execute strADir
CurrentDb.Execute strAInd
CurrentDb.Execute strADep
DoCmd.Close
正如您所看到的,我插入了Debug.Print行,试图弄清楚发生了什么,但所有的sql字符串都很好。但是,当这段代码完成后,我的表中不存在(strInd,strDep,strAInd和strADep)的记录。
是否有一些我缺少的步骤或者应该包含哪些步骤来稳定这一步,以便按预期创建所有记录?
答案 0 :(得分:1)
您需要在执行周围添加代码以查看错误,最有可能来自SQL
On Error GoTo Err_Execute
Currentdb.Execute strInd
On Error GoTo 0
' etc ...
Exit Sub
Err_Execute:
' Notify user of any errors that result from
' executing the query.
If DBEngine.Errors.Count > 0 Then
For Each errLoop In DBEngine.Errors
MsgBox "Error number: " & errLoop.Number & vbCr & _
errLoop.Description
Next errLoop
End If
Resume Next
End Sub
我的VBA非常生疏,但这应该让你开始,这样你就可以看到错误是什么......快速猜测,我会检查ForecastTran表中描述字段的长度。我唯一能看到的是这些价值看起来比其他价值略长。