【问题标题】:How to convert currency into double in VBA?如何在VBA中将货币转换为double?
【发布时间】:2017-06-15 14:25:23
【问题描述】:

我有三个文本框,我得到它们的值是这样的:

Dim X, Y, W As Double
X = DLookup("Summ", "tblPlatej", "ID= " & Form_frmPlatej!ID)
Y = DLookup("Deposit_before", "tblPlatej", "ID= " & Form_frmPlatej!ID)
W = DLookup("Monthly_payment", "tblPlatej", "ID= " & Form_frmPlatej!ID)

但是当我像这样改变文本框的值时

Form_frmPlatej.Deposit_before = X - W + Y

我收到类型不匹配错误。所有文本框都是货币。如何计算新记录并将该数字放入“Deposit_before”文本框中?

Summ、Deposit_before、Monthly_payment 是我表中的货币数据类型。 deposit_before 大多为负数。

这是我点击按钮的全部代码

Private Sub Command13_Click()

a1 = DLookup("Inhabitant", "tblClient", "ID = " & Form_frmMain!ID)
B1 = DLookup("PriceTBO", "tblPrice")
c1 = DLookup("Republican", "tblClient", "ID = " & Form_frmMain!ID)
d1 = DLookup("Regional", "tblClient", "ID = " & Form_frmMain!ID)
e1 = DLookup("Local", "tblClient", "ID = " & Form_frmMain!ID)

A = DLookup("IDP", "tblPlatej", "ID= " & Form_frmPlatej!ID)
B = DLookup("Type_of_payment", "tblPlatej", "ID= " & Form_frmPlatej!ID)
C = DLookup("Year", "tblPlatej", "ID= " & Form_frmPlatej!ID)
D = DLookup("Month", "tblPlatej", "ID= " & Form_frmPlatej!ID)

Y = DLookup("Deposit_before", "tblPlatej", "ID= " & Form_frmPlatej!ID) // Problem here
W = DLookup("Monthly_payment", "tblPlatej", "ID= " & Form_frmPlatej!ID) //Problem here
X = DLookup("Summ", "tblPlatej", "ID= " & Form_frmPlatej!ID)

i = Form_frmPlatej.Month.ListIndex
j = Form_frmPlatej.Year.ListIndex
den = DLookup("Date", "tblPlatej", "IDP = " & Form_frmPlatej!IDP)

If X <> " " Then
With Me.Recordset
If Me.Recordset.BOF = False And Me.Recordset.EOF = False Then
.MoveFirst
End If
.AddNew
.Edit

Form_frmPlatej.Deposit_before = X - W + Y  //Problem here

Form_frmPlatej.IDP = A + 1
Form_frmPlatej.Type_of_payment = B
If i = 11 Then
Form_frmPlatej.Year = Year.ItemData(j + 1)
i = -1
Else
Form_frmPlatej.Year = Year.ItemData(j)
End If

Form_frmPlatej.Month = Month.ItemData(i + 1)
Form_frmPlatej.Date = DateAdd("m", 1, den)

If c1 <> 0 Then
Form_frmPlatej.Monthly_payment = (a1 * B1) - (c1 * (a1 * B1)) / 100

ElseIf d1 <> 0 Then
Form_frmPlatej.Monthly_payment = (a1 * B1) - (d1 * (a1 * B1)) / 100

ElseIf e1 <> 0 Then
Form_frmPlatej.Monthly_payment = (a1 * B1) - (e1 * (a1 * B1)) / 100
Else
Form_frmPlatej.Monthly_payment = a1 * B1
End If
.Update

End With

Else
MsgBox ("Please enter number")
End If

End Sub

我完全糊涂了。

【问题讨论】:

标签: ms-access vba ms-access-2007


【解决方案1】:

我敢打赌,您的问题如下。当你这样说时:

Dim X, Y, W As Double

认为你已经做到了:

Dim X As Double, Y As Double, W As Double

但是你真正做的是这样的:

Dim X
Dim Y
Dim W As Double

这是一个典型的 VBA 错误。大多数 VBA 程序员都做到了,这就是为什么大多数 VBA 程序员只能在每个Dim 语句中声明一个变量(即每行一个)。否则很容易犯这个错误,之后很难发现它。

因此,对于 Dim XDim Y,您已将 XY 隐式声明为 Variant 类型(等效于 Dim X As VariantDim Y As Variant)。

为什么这很重要?当你这样说时:

X = DLookup("Summ", "tblPlatej", "ID= " & Form_frmPlatej!ID)
Y = DLookup("Deposit_before", "tblPlatej", "ID= " & Form_frmPlatej!ID)

也许这两个DLookup 之一意外地返回了不是数字的东西,例如字符串。您的变体 XY 将接受这一点而不会抱怨; Variant 获取赋值右侧事物的类型。

但是,当您尝试使用这些值进行数学运算时,如果 X 和/或 Y 是字符串,X - W + Y 将引发类型不匹配错误。

另请参阅我的这个较早的答案,我从中重复使用了一些措辞:https://stackoverflow.com/a/11089684/119775

【讨论】:

  • 我试图明确声明 Dim X As Double Dim Y As Double Dim W As Double Dim Z As Double 发生同样的错误......
  • 在哪一行?出现错误时,调试窗口中XYW的值是多少?
  • Summ、Deposit_before、Monthly_payment 是我表中的货币数据类型。 deposit_before 大多为负数。此行错误 Y = DLookup("[Deposit_before]", "tblPlatej", "[ID]= " & Form_frmPlatej!ID)
  • @FirdavsKurbonov 当您遇到该错误时,切换到调试模式,并在“立即”窗口中运行:? TypeName(DLookup("[Deposit_before]", "tblPlatej", "[ID]= " &amp; Form_frmPlatej!ID)) 如果响应为 Null 并且 Y 已声明为 Double,则说明类型不匹配.
  • 嗯,这正是我昨天在回答中猜到的:“DLookup 意外返回 [...] 一个字符串。”也许你应该接受这个答案并问一个更具体针对您的新问题的新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-28
相关资源
最近更新 更多