2012-07-11 180 views
2

我有三个文本框和我得到他们的价值是这样的:如何在VBA中将货币转换为double?

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”文本框中?

汇总,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 

我完全糊涂了。

+0

我很好奇,为什么你想转换为货币从一倍?为什么不使用货币类型时,你有什么?请参阅http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – Fionnuala 2012-07-11 10:55:16

回答

3

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

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

+0

我试图明确声明Dim X As Double Dim Y As Double Dim W As Double Dim Z As Double同样的错误发生... – 2012-07-11 07:16:20

+0

在什么行上?发生错误时,调试窗口中的“X”,“Y”和“W”的值是多少? – 2012-07-11 07:54:33

+0

汇总,Deposit_before,Monthly_payment是我表格中的货币数据类型。 Deposit_before大多是负面的。错误在这一行Y = DLookup(“[Deposit_before]”,“tblPlatej”,“[ID] =”和Form_frmPlatej!ID) – 2012-07-11 08:12:42