【发布时间】: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