2012-07-11 6 views
2

나는 세 개의 텍스트 상자를 가지고 있고이 같은 자신의 가치를 얻을 :VBA에서 통화를 이중으로 변환하는 방법은 무엇입니까?

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 

나는 완전히 혼란 스러워요 클릭합니다.

+0

난 당신이 통화에서 두 배로 변환하고자하는 이유에 대해 궁금합니다 :

또한 내가 문구의 일부를 재사용있는 광산의 이전 대답을 참조하십시오? 통화 유형을 가지고있을 때 통화 유형을 사용하지 않으시겠습니까? http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – Fionnuala

답변

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 X과와 Dim Y 암시 적 (Dim X As VariantDim Y As Variant에 해당) 변형 유형으로 XY을 선언했습니다.

왜이 점이 중요합니까? 당신은이 말을 할 때 : 그 두 DLookup

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

어쩌면 하나가 예기치 않게 예를 들어, 문자열을 숫자가 아닌 무언가를 반환합니다. 변종 X 또는 Y은 불만없이이를 수락합니다. Variant는 과제의 오른쪽에있는 물건의 유형을 획득합니다.

그러나이 값을 사용하여 수학을 시도하면 X 및/또는 Y이 문자열 인 경우 X - W + Y은 유형 불일치 오류를 발생시킵니다. https://stackoverflow.com/a/11089684/119775

+0

명시 적으로 Dim X As를 선언하려고 시도했습니다. Dim Y As Double Dim W As Double Dim Z As Double 같은 오류가 발생합니다 ... –

+0

어떤 행에 있습니까?오류가 발생하면 디버그 창에서'X','Y' 및'W' 값은 무엇입니까? –

+0

Summ, Deposit_before, Monthly_payment는 테이블의 통화 데이터 유형입니다. Deposit_before은 대부분 음수입니다. 이 줄의 오류 Y = DLookup ("[Deposit_before]", "tblPlatej", "[ID] ="& Form_frmPlatej! ID) –

관련 문제