我正在尝试从我继承此 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