2

3 つのテキスト ボックスがあり、次のように値を取得します。

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

私は完全に混乱しています。

4

1 に答える 1

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 の間違いです。Dimほとんどの VBA プログラマーはそれを実現しており、そのため、ほとんどの VBA プログラマーはステートメントごとに 1 つの変数 (つまり、1 行に 1 つ)のみを宣言することに頼っています。そうしないと、間違いを犯しやすくなり、後でそれを見つけるのが難しくなります。

したがって、Dim Xandをバリアント型 ( and と同等) としてDim Y暗黙的に宣言Xしました。YDim X As VariantDim Y As Variant

なぜこれが重要なのですか?次にこう言うと:

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

おそらく、これら 2 つのうちの 1 つがDLookup、文字列など、数値ではないものを予期せずに返す可能性があります。あなたのバリアントはXY文句を言わずにこれを受け入れます。Variant は、割り当ての右側にあるものの型を取得します。

ただし、これらの値を使用して計算しようとすると、 and/orが文字列のX - W + Y場合、型の不一致エラーがスローされます。XY

文言の一部を再利用した、私の以前の回答も参照してください: https://stackoverflow.com/a/11089684/119775

于 2012-07-11T06:57:48.850 に答える