0

2 つの列 (value1 と value2) を持つテーブルがあり、値は最低から最高に並べ替えられています。例:

Value1
20
40
43
90
100
122

Value2
4
5
9
10
15
18

ユーザーに入力値を入力してもらい、次のいずれかで計算できる CalcFinalValue の値を計算します。

  1. ユーザー入力値がすでに value1 フィールドに存在する場合は、フィールド value2 の対応する値を返します。たとえば、ユーザー入力が 100 の場合、CalcFinalValue は 15 になります。

  2. ユーザー入力値が value1 フィールドに存在しない場合は、value1 フィールドで入力値が間にある 2 つの値を見つけます (たとえば、入力値が 42 の場合、value1 フィールドから 40 と 43 を見つけたい)。CalcFinalValue を次のように計算します: CalcFinalValue=(40*9)+(43*5)/42

つまり、数式は次のようになります。 CalcFinalValue=(中間値の LowerValue *中間値の HigherValue のルックアップ値)+(中間値の HigherValue *中間値の LowerValue のルックアップ値)/(ユーザー入力value) これを Access 2007 VBA で実行したいと考えています。

これが明確であることを願っています。事前にご協力いただきありがとうございます。

4

1 に答える 1

0
Dim rs AS DAO.Recordset
Set rs = CurrentDb.OpenRecordset("TableName", dbOpenTable)

' inp stores the user input value; i counts the number of records that have been accessed
Dim inp, i as Integer
' r2c1 and r2c2 store the data of the second row in case the two-row calculation is needed 
Dim r2c1, r2c2 as integer

' Since I mostly use forms for I/O that's what I've used here
'   Change this to whatever method you use to get the user input
inp = Forms!FormName.InputTextBoxName.Value
i = 0
rs.MoveFirst
Do While (Not rs.EOF)
    i = i + 1
    ' Check if the user input exists in the table
    If (rs.Fields("Value1") = inp) Then
        ' Again use whatever output method you want
        Set Forms!FormName.OutputTextBoxName.Value = rs.Fields("Value1")
        End Do
    ' Otherwise, check if the current value in column 1 is greater than the input
    Else If (rs.Fields("Value1") > inp) Then
        ' If this is true for the first row then the input is less than the lowest number.
        ' I assume this is out-of-bounds, but use whatever boundary condition you want
        If (i = 1) Then
            MsgBox("Out of Bounds", vbOkOnly)
        Else 
            ' Save the required values from this row
            r2c2 = rs.Fields("Value2")
            r2c1 = rs.Fields("Value1")
            ' Goto previous row which and calculate according to your formula
            rs.MoveLast
            Set Forms!FormName.OutputTextBoxName.Value = (r2c1*r2c2 + rs.Fields("Value1")*rs.Fields("Value2"))/inp
        End If 
    End If 
    rs.MoveNext
Loop
' If nothing was found, the input was larger than all values in 'Value1'
If (rs.EOF) Then
    MsgBox("Out of Bounds", vbOkOnly)
End If

Value1 と Value2 を、使用する列名に置き換えます

于 2012-06-26T10:05:59.737 に答える