使用数据宏在Access Web App中“合并”表

时间:2015-06-12 15:41:30

标签: ms-access-2013 ms-access-data-macro

我已经构建了一个Access Web App(2013)来跟踪客户端的设备库存。他们希望能够使用从订单生成的Excel电子表格批量更新数据库。我已经知道无法将数据导入/更新到AWA中的现有表中,因此我尝试构建一个数据宏,将导入的表与现有表进行比较,添加新记录或编辑现有记录。

目前,我只是想让第一部分工作 - 添加新记录。但是,无论我尝试什么,我的数据宏都不断给出错误“Access无法将'N / A'的值转换为指定的数据类型。”

我在宏上运行了一个跟踪,它显示它完美地完成每一步直到最后 - 它将创建新记录,设置我正在测试的2个字段...然后尝试创建记录了。它会卡住,因为它不会返回到导入表的下一个“For Each Record”循环。我已经尝试在创建记录之后执行“停止宏”以查看是否可以让它只执行第一条记录,但它仍然尝试再次运行并失败。

我在一个页面上找到了一个说source code的人“,但我找不到任何可以在任何地方支持的内容。

遗憾的是,我没有足够的声望点来发布图片甚至链接到它们,所以我将尝试准确地代表我的宏观和跟踪(请原谅我对该网站的相对新颖性:()。

My Macro看起来像这样:

For Each Record In ImportTest
      Where Condition
   SetLocalVar
              Name IsMatch
              Expression = 0
   Look Up A Record In Inventory List
            Where Condition = [InventoryList].[SerialNumber]=[ImportTest].[Serial Number]
      SetLocalVar
                  Name IsMatch
                  Expression = 1
  If [IsMatch]=0 Then
     Create a Record In Inventory List
        Set Field
            Name InventoryList.BuildingName
            Value = 1
        Set Field
            Name InventoryList.SerialNumber
            Value = [ImportTest].[Serial Number]
  End If

所以要解释我的目标是什么,以防我做错了:“ForTest ImportTest中的每个记录”正在查看导入的表格(没有条件,因为我想要所有这些)。第一个“SetLocalVar IsMatch”是为宏提供一种方法来了解导入表中的序列号是否与现有InventoryList表中的另一个记录相匹配(这是我将更新该记录的地方,一旦我完成所有工作)。接下来是我的“If [IsMatch] = 0”,它检查是否找到了匹配的记录(如果之前找到了匹配,则会被设置为1)。由于未找到匹配项,因此它使用For Each Record循环指向的当前记录在InventoryList中创建记录。我实际上还有许多其他字段需要设置,但这两个字段仅用于我的概念验证(我的InventoryList需要这两个字段)。此时,它应该完成添加记录,然后点击循环结束,然后重新开始。但是,我的跟踪显示“CreateRecord”抛出SQL异常:

MacroName-ActionName-Operand-Output-TargetRow-RuntimeErrorMessage
ImportMacro-ForEachRecord-ImportTest;---
ImportMacro-ForEachRecord---[ID] = 1 ; [Serial Number] = TESTTEST1 ; [Building Name] = Glendaal Elementary School-
ImportMacro-SetLocalVar-IsMatch-0.000000--
ImportMacro-LookupRecord-InventoryList;WHERE [InventoryList].[SerialNumber]=[ImportTest].[Serial Number]---
ImportMacro-If-[IsMatch]=0---
ImportMacro-CreateRecord----
ImportMacro-If-[ImportTest].[Building Name]="Glendaal Elementary School"---
ImportMacro-SetField-InventoryList.BuildingName-1--
ImportMacro-SetField-InventoryList.SerialNumber-TESTTEST1--
ImportMacro-CreateRecord----SQL exception

我无法弄清楚为什么它会抛出异常的最后一个“CreateRecord”。有没有人看到我可能错过了什么,或者有关于如何进行的建议?

提前感谢所有帮助的人。 〜SetsunaMH

ImportTest数据宏的当前版本:

<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<DataMacros xmlns="http://schemas.microsoft.com/office/accessservices/2010/12/application"><DataMacro><Statements><ForEachRecord><Data><Reference>ImportTest</Reference></Data><Statements><Action Name="SetLocalVar"><Argument Name="Name">IsMatch</Argument><ExpressionArgument Name="Value"><Expression><Original>0</Original><IntegerLiteral Value="0"/></Expression></ExpressionArgument></Action><LookUpRecord><Data><Reference>InventoryList</Reference><WhereCondition><Expression><Original>[InventoryList].[SerialNumber]=[ImportTest].[Serial Number]</Original><FunctionCall Name="="><Identifier Name="InventoryList.SerialNumber" Index="0"/><Identifier Name="ImportTest.Serial Number" Index="1"/></FunctionCall></Expression></WhereCondition></Data><Statements><Action Name="SetLocalVar"><Argument Name="Name">IsMatch</Argument><ExpressionArgument Name="Value"><Expression><Original>1</Original><IntegerLiteral Value="1"/></Expression></ExpressionArgument></Action></Statements></LookUpRecord><ConditionalBlock><If><Condition><Expression><Original>[IsMatch]=0</Original><FunctionCall Name="="><Identifier Name="IsMatch" Index="0"/><IntegerLiteral Value="0" Index="1"/></FunctionCall></Expression></Condition><Statements><CreateRecord><Data><Reference>InventoryList</Reference></Data><Statements><Action Name="SetField"><Argument Name="Field">InventoryList.BuildingName</Argument><ExpressionArgument Name="Value"><Expression><Original>1</Original><IntegerLiteral Value="1"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.SerialNumber</Argument><ExpressionArgument Name="Value"><Expression><Original>[ImportTest].[Serial Number]</Original><Identifier Name="ImportTest.Serial Number"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.Model</Argument><ExpressionArgument Name="Value"><Expression><Original>8</Original><IntegerLiteral Value="8"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.RoomDescription</Argument><ExpressionArgument Name="Value"><Expression><Original>1</Original><IntegerLiteral Value="1"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.Manufacturer</Argument><ExpressionArgument Name="Value"><Expression><Original>1</Original><IntegerLiteral Value="1"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.DeviceName</Argument><ExpressionArgument Name="Value"><Expression><Original>[ImportTest].[Device Name]</Original><Identifier Name="ImportTest.Device Name"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.ImagePackage</Argument><ExpressionArgument Name="Value"><Expression><Original>[ImportTest].[Image Package]</Original><Identifier Name="ImportTest.Image Package"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.NeedsVerification</Argument><ExpressionArgument Name="Value"><Expression><Original>[ImportTest].[Needs Verification]</Original><Identifier Name="ImportTest.Needs Verification"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.NewLocation</Argument><ExpressionArgument Name="Value"><Expression><Original>[ImportTest].[New Location]</Original><Identifier Name="ImportTest.New Location"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.OneOffs</Argument><ExpressionArgument Name="Value"><Expression><Original>[ImportTest].[One Offs]</Original><Identifier Name="ImportTest.One Offs"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.PurchasedDate</Argument><ExpressionArgument Name="Value"><Expression><Original>[ImportTest].[Purchased Date]</Original><Identifier Name="ImportTest.Purchased Date"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.Retired</Argument><ExpressionArgument Name="Value"><Expression><Original>[ImportTest].[Retired]</Original><Identifier Name="ImportTest.Retired"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.Software1</Argument><ExpressionArgument Name="Value"><Expression><Original>67</Original><IntegerLiteral Value="67"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.Software2</Argument><ExpressionArgument Name="Value"><Expression><Original>67</Original><IntegerLiteral Value="67"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.Software3</Argument><ExpressionArgument Name="Value"><Expression><Original>67</Original><IntegerLiteral Value="67"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.Software4</Argument><ExpressionArgument Name="Value"><Expression><Original>67</Original><IntegerLiteral Value="67"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.Software5</Argument><ExpressionArgument Name="Value"><Expression><Original>67</Original><IntegerLiteral Value="67"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.Staff-Student</Argument><ExpressionArgument Name="Value"><Expression><Original>[ImportTest].[Staff-Student]</Original><Identifier Name="ImportTest.Staff-Student"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.Swap</Argument><ExpressionArgument Name="Value"><Expression><Original>[ImportTest].[Swap]</Original><Identifier Name="ImportTest.Swap"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.TagNumber</Argument><ExpressionArgument Name="Value"><Expression><Original>[ImportTest].[Tag Number]</Original><Identifier Name="ImportTest.Tag Number"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.TopUser</Argument><ExpressionArgument Name="Value"><Expression><Original>[ImportTest].[Top User]</Original><Identifier Name="ImportTest.Top User"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.Warranty</Argument><ExpressionArgument Name="Value"><Expression><Original>[ImportTest].[Warranty]</Original><Identifier Name="ImportTest.Warranty"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.Comments</Argument><ExpressionArgument Name="Value"><Expression><Original>"Test"</Original><StringLiteral Value="Test"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.CreatedBy</Argument><ExpressionArgument Name="Value"><Expression><Original>"Test"</Original><StringLiteral Value="Test"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">InventoryList.UpdatedBy</Argument><ExpressionArgument Name="Value"><Expression><Original>""</Original><StringLiteral Value=""/></Expression></ExpressionArgument></Action></Statements></CreateRecord></Statements></If></ConditionalBlock></Statements></ForEachRecord></Statements></DataMacro></DataMacros>

InventoryList表的当前更新宏(当前已停止,以便在所有内容都处于开发状态时不会导致问题并且不会使我的AuditTrail表超载。此宏已经过多个人一次使用数据库进行测试并且像魅力一样工作)。

<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<DataMacros xmlns="http://schemas.microsoft.com/office/accessservices/2010/12/application"><DataMacro Event="AfterUpdate"><Statements><Comment>Macro is stopped to not overload AuditTrail during changes made to initial data.</Comment><Action Name="StopMacro"/><Comment>Save the Old values</Comment><CreateRecord><Data><Reference>AuditTrail</Reference></Data><Statements><Action Name="SetField"><Argument Name="Field">AuditTrail.OldNew</Argument><ExpressionArgument Name="Value"><Expression><Original>"Old"</Original><StringLiteral Value="Old"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.RecordID</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[ID]</Original><Identifier Name="InventoryList.ID"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Timestamp</Argument><ExpressionArgument Name="Value"><Expression><Original>Now()</Original><FunctionCall Name="Now"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.ChangedBy</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[UpdatedBy]</Original><Identifier Name="InventoryList.UpdatedBy"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.BuildingName</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[BuildingName]</Original><Identifier Name="Old.BuildingName"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.RoomDescription</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[RoomDescription]</Original><Identifier Name="Old.RoomDescription"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Manufacturer</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[Manufacturer]</Original><Identifier Name="Old.Manufacturer"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Model</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[Model]</Original><Identifier Name="Old.Model"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.DeviceName</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[DeviceName]</Original><Identifier Name="Old.DeviceName"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.TopUser</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[TopUser]</Original><Identifier Name="Old.TopUser"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Staff-Student</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[Staff-Student]</Original><Identifier Name="Old.Staff-Student"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.SerialNumber</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[SerialNumber]</Original><Identifier Name="Old.SerialNumber"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.TagNumber</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[TagNumber]</Original><Identifier Name="Old.TagNumber"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.PurchasedDate</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[PurchasedDate]</Original><Identifier Name="Old.PurchasedDate"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Warranty</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[Warranty]</Original><Identifier Name="Old.Warranty"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.ImagePackage</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[ImagePackage]</Original><Identifier Name="Old.ImagePackage"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Swap</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[Swap]</Original><Identifier Name="Old.Swap"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.NewLocation</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[NewLocation]</Original><Identifier Name="Old.NewLocation"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Retired</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[Retired]</Original><Identifier Name="Old.Retired"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.OneOffs</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[OneOffs]</Original><Identifier Name="Old.OneOffs"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Comments</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[Comments]</Original><Identifier Name="Old.Comments"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.NeedsVerification</Argument><ExpressionArgument Name="Value"><Expression><Original>[Old].[NeedsVerification]</Original><Identifier Name="Old.NeedsVerification"/></Expression></ExpressionArgument></Action></Statements></CreateRecord><Comment>Save the New values</Comment><CreateRecord><Data><Reference>AuditTrail</Reference></Data><Statements><Action Name="SetField"><Argument Name="Field">AuditTrail.OldNew</Argument><ExpressionArgument Name="Value"><Expression><Original>"New"</Original><StringLiteral Value="New"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.RecordID</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[ID]</Original><Identifier Name="InventoryList.ID"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Timestamp</Argument><ExpressionArgument Name="Value"><Expression><Original>Now()</Original><FunctionCall Name="Now"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.ChangedBy</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[UpdatedBy]</Original><Identifier Name="InventoryList.UpdatedBy"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.BuildingName</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[BuildingName]</Original><Identifier Name="InventoryList.BuildingName"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.RoomDescription</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[RoomDescription]</Original><Identifier Name="InventoryList.RoomDescription"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Manufacturer</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[Manufacturer]</Original><Identifier Name="InventoryList.Manufacturer"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Model</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[Model]</Original><Identifier Name="InventoryList.Model"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.DeviceName</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[DeviceName]</Original><Identifier Name="InventoryList.DeviceName"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.TopUser</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[TopUser]</Original><Identifier Name="InventoryList.TopUser"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Staff-Student</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[Staff-Student]</Original><Identifier Name="InventoryList.Staff-Student"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.SerialNumber</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[SerialNumber]</Original><Identifier Name="InventoryList.SerialNumber"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.TagNumber</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[TagNumber]</Original><Identifier Name="InventoryList.TagNumber"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.PurchasedDate</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[PurchasedDate]</Original><Identifier Name="InventoryList.PurchasedDate"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Warranty</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[Warranty]</Original><Identifier Name="InventoryList.Warranty"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.ImagePackage</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[ImagePackage]</Original><Identifier Name="InventoryList.ImagePackage"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Swap</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[Swap]</Original><Identifier Name="InventoryList.Swap"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.NewLocation</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[NewLocation]</Original><Identifier Name="InventoryList.NewLocation"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Retired</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[Retired]</Original><Identifier Name="InventoryList.Retired"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.OneOffs</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[OneOffs]</Original><Identifier Name="InventoryList.OneOffs"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.Comments</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[Comments]</Original><Identifier Name="InventoryList.Comments"/></Expression></ExpressionArgument></Action><Action Name="SetField"><Argument Name="Field">AuditTrail.NeedsVerification</Argument><ExpressionArgument Name="Value"><Expression><Original>[InventoryList].[NeedsVerification]</Original><Identifier Name="InventoryList.NeedsVerification"/></Expression></ExpressionArgument></Action></Statements></CreateRecord></Statements></DataMacro></DataMacros>

1 个答案:

答案 0 :(得分:1)

在检查Access网络应用包后,我已确定问题所在。您当前具有分配给InventoryList表中每个软件查找字段(1-5)的默认值属性。我删除了每个字段的默认值。然后我将一些示例虚拟数据放入相关表中。列表视图上的按钮当前设置为运行ImportMacro2数据宏。当我现在运行时,宏完成没有错误,数据被插入到InventoryList中。您不能将N / A用作默认值,因为这些是仅接受ID(数字)的查找字段。