【问题标题】:13Type Mismatch Access 200713类型不匹配访问 2007
【发布时间】:2013-11-12 03:23:12
【问题描述】:

我正在尝试从我继承此 Access 数据库的人那里修复一些拙劣的 VBA。除了 VBA 中几乎没有用的注释之外,没有任何文档,所以我试图弄清楚一切都是做什么的,以及它是否正确。当我单击按钮以将单位或值添加到贡献表时,我继续收到 13Type Mismatch 错误。我认为这是一个简单的解决方法,例如搞砸了变量声明,但是我已将它们更改为 Double,它似乎并没有纠正我的错误。有没有人看到他们可能认为抛出这个错误的任何东西?提前感谢您的努力。

    Private Sub AddContributionBtn_Click()
    On Error GoTo Err_AddContributionBtn

  Dim Cancel As Integer
  Dim CurrentNAVDate As Date
  Dim CurrentNAV As Double
  Dim ConfirmAddCont As Double
  Dim CalcContUnits As Double
  Dim CalcContValue As Double
  Dim StringSQL As String

    'get current NAV
    CurrentNAVDate = Format(DateAdd("s", -1, DateAdd("q", DateDiff("q", "1/1/1900", Date),             "1/1/1900")), "Short Date")
    CurrentNAV = Format(DLookup("NetAssetValue", "NAV_Tbl", "Format(NAV_Date, ""mmddyyyy"") = " & Format(CurrentNAVDate, "mmddyyyy")), "Currency")

    'validation to require either contribution units or value is entered, not both
    If IsNull(Me.ContValueTxt) = True And IsNull(Me.ContUnitsTxt) = True Then
        MsgBox "Please enter contribution units or value."
        Me.ContUnitsTxt.SetFocus
        Cancel = True
        Exit Sub
    ElseIf IsNull(Me.ContValueTxt) = False And IsNull(Me.ContUnitsTxt) = False Then
        MsgBox "Both contribution units and value may not be entered."
        Me.ContUnitsTxt.SetFocus
        Cancel = True
        Exit Sub
    Else:
        If IsNull(Me.ContValueTxt) = True And IsNull(Me.ContUnitsTxt) = False Then
            'calculate contribution value from units
            CalcContUnits = Me.ContUnitsTxt
            CalcContValue = CalcContUnits * CurrentNAV

            GoTo ConfirmAppend

        ElseIf IsNull(Me.ContValueTxt) = False And IsNull(Me.ContUnitsTxt) = True Then
            'calculate contribution units from value
            CalcContValue = Me.ContValueTxt
            CalcContUnits = CalcContValue / CurrentNAV

            GoTo ConfirmAppend
        End If
    End If

 ConfirmAppend:
    'confirm contribution value and units, run append query
    ConfirmAddCont = MsgBox("Add " & Format(CalcContUnits, "fixed") & " units for a      contribution value of " & Format(CalcContValue, "currency") & "?", _
    vbOKCancel, "Add Contribution")
    If ConfirmAddCont = vbOK Then
        DoCmd.Hourglass True
        DoCmd.SetWarnings False
            StringSQL = "INSERT INTO ContributionTbl(ContDate, ContUnits, ContNAV,   ContType) VALUES (#" & Date & "#, " & CalcContUnits & ", #" & CurrentNAVDate & "#, " & 1 & ");"
            DoCmd.RunSQL (StringSQL)
        DoCmd.SetWarnings True
        DoCmd.Hourglass False

        Me.ContUnitsTxt = Null
        Me.ContValueTxt = Null
        Forms!PlanFrm![PlanContributedUnitsFrm].Requery
    Else
        Cancel = True
        Exit Sub
    End If

 Exit_AddContributionBtn:
    Exit Sub
 Err_AddContributionBtn:
    MsgBox Err.Number & Err.Description
    Resume Exit_AddContributionBtn
 End Sub

【问题讨论】:

  • 哪一行出现错误?
  • 我真的很抱歉,但我不确定。这只是我在单击按钮后发现的 VBA 代码,我收到 13Type Mismatch 错误。一些数字变量是 Integer 和 Double,所以我将它们全部更改为 Double 假设这会有所帮助,但它没有......
  • CurrentNAVDate = Format( 行设置断点(使用 F9)。然后使用 F8 一次单步执行一行代码。这应该允许您确定哪条线触发了错误。祝你好运,真的。
  • HansUp,感谢您的回复。它查明了 CurrentNAVDate 和 CurrentNAV 线。这是我应该查看变量声明的某种问题的地方吗?
  • 错误可能在这里: CurrentNAV = Format(DLookup("NetAssetValue", "NAV_Tbl", "Format(NAV_Date, ""mmddyyyy"") = " & Format(CurrentNAVDate, "mmddyyyy")) , "Currency"), 因为 DLookup() 得到 NULL, Format(NULL, "Currency") 得到 13 Type Mismatch, 正如我在 Access 2007 中重现的那样?

标签: vba ms-access-2007


【解决方案1】:

如讨论中所示,我在这个临时回复中让我们的猜测更清楚:

错误可能在这里:

CurrentNAV = Format(DLookup("NetAssetValue", "NAV_Tbl", "Format(NAV_Date, ""mmddyyyy"") = " & Format(CurrentNAVDate, "mmddyyyy")), "Currency")

因为 DLookup("NetAssetValue",...) 得到 NULL,

Format(NULL, "Currency") 得到 13 种类型不匹配,正如我在 Access 2007 中重现的那样。

这可以解释: 由于表字段 NAV_Tbl.NetAssetValue 中没有最近的日期,因此我们得到的日期 CurrentNAVDate = 09/30/2013(上个季度的最后日期)。

所以你可以尝试这样的代码,通过引入 varCurrency 变量来处理这种 NULL 值情况:

Private Sub AddContributionBtn_Click()

  On Error GoTo Err_AddContributionBtn

  Dim Cancel As Integer
  Dim CurrentNAVDate As Date
  Dim CurrentNAV As Double
  Dim ConfirmAddCont As Double
  Dim CalcContUnits As Double
  Dim CalcContValue As Double
  Dim StringSQL As String

  Dim varCurrency

  'get current NAV
  CurrentNAVDate = Format(DateAdd("s", -1, DateAdd("q", DateDiff("q", "1/1/1900", Date),             "1/1/1900")), "Short Date")
  varCurrency = DLookup("NetAssetValue", "NAV_Tbl", "Format(NAV_Date, ""mmddyyyy"") = " & Format(CurrentNAVDate, "mmddyyyy"))
  If(IsNull(varCurrency) then
    CurrentNAV = 0
  Else
    CurrentNAV = Format(varCurrency, "Currency")
  End If

  'validation to require either contribution units or value is entered, not both
  If IsNull(Me.ContValueTxt) = True And IsNull(Me.ContUnitsTxt) = True Then
    MsgBox "Please enter contribution units or value."
    Me.ContUnitsTxt.SetFocus
    Cancel = True
    Exit Sub
  ElseIf IsNull(Me.ContValueTxt) = False And IsNull(Me.ContUnitsTxt) = False Then
    MsgBox "Both contribution units and value may not be entered."
    Me.ContUnitsTxt.SetFocus
    Cancel = True
    Exit Sub
  Else:
    If IsNull(Me.ContValueTxt) = True And IsNull(Me.ContUnitsTxt) = False Then
      'calculate contribution value from units
      CalcContUnits = Me.ContUnitsTxt
      CalcContValue = CalcContUnits * CurrentNAV

      GoTo ConfirmAppend

    ElseIf IsNull(Me.ContValueTxt) = False And IsNull(Me.ContUnitsTxt) = True Then
      'calculate contribution units from value
      CalcContValue = Me.ContValueTxt
      CalcContUnits = CalcContValue / CurrentNAV

      GoTo ConfirmAppend
    End If
  End If

ConfirmAppend:
  'confirm contribution value and units, run append query
  ConfirmAddCont = MsgBox("Add " & Format(CalcContUnits, "fixed") & " units for a      contribution value of " & Format(CalcContValue, "currency") & "?", _
  vbOKCancel, "Add Contribution")
  If ConfirmAddCont = vbOK Then
    DoCmd.Hourglass True
    DoCmd.SetWarnings False
    StringSQL = "INSERT INTO ContributionTbl(ContDate, ContUnits, ContNAV,   ContType) VALUES (#" & Date & "#, " & CalcContUnits & ", #" & CurrentNAVDate & "#, " & 1 & ");"
    DoCmd.RunSQL (StringSQL)
    DoCmd.SetWarnings True
    DoCmd.Hourglass False

    Me.ContUnitsTxt = Null
    Me.ContValueTxt = Null
    Forms!PlanFrm![PlanContributedUnitsFrm].Requery
  Else
    Cancel = True
    Exit Sub
  End If

Exit_AddContributionBtn:
  Exit Sub
Err_AddContributionBtn:
  MsgBox Err.Number & Err.Description
  Resume Exit_AddContributionBtn
End Sub

对于 DLookup():

varCurrency = DLookup("NetAssetValue", "NAV_Tbl", "NAV_Date >= #" & Format(CurrentNAVDate, "yyyy-mm-dd") & "#")

【讨论】:

  • CurrentNAV / CurrentNAVDate 不工作是否有可能的原因?我知道在查询设计中,要获取最新的 NAV 日期,我只需按降序排序。我不太确定这两行 NAV 代码在做什么,因此我不知道是什么引发了错误。如何确保它检索到正确的信息?
  • 您应将数据输入到最近日期的 NAV_Tbl 表中。 CurrentNAV 是金额,我猜。
  • 不,不。你完全正确。当前日期是一个季度更新的日期字段。 CurrentNAV 是一个美元金额,也每季度更新一次。我不确定 CurrentNAVDate 是否成功找到具有最近日期的行,然后找到与该日期关联的 CurrentNAV...这可以在 VBA 中完成吗?
  • 是的,varCurrency 的 DLookup 不起作用。没有进行计算。我不确定如何解决这个问题......
  • 很有可能。您应该使用像“NAV_Date = #2013-09-30#”这样的过滤器,但这取决于 NAV_Tbl.NAV_Date、DateTime 或简单字符串的类型,...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多