Loop in an arithmetic addition statement...

WindSailor

Registered User.
Local time
Yesterday, 22:33
Joined
Oct 29, 2003
Messages
239
How would I go about doing this... ?

Have a value "A", a limitation on how many times you can use "A" by the value of "AA".

Another value "B", again with a limitation on how many times you can use "B" by the value of "BB".

Use all four values to come up with a value "C".

Example1:

A=50; AA=1; B=10; BB=20; C=70
50+10+10=70

Example2:

A=50; AA=4; B=10; B=20; C=70
50+10+10=70

Example3:

A=50; AA=0; B=10; BB=20; C=70
10+10+10+10+10+10+10=70

The user has the ability to input the values for "A", "AA", "B", "BB", and "C". I can substitute those values but I would like to see how the code works and it has to handle any values given or inserted.

Finally... the left side of the equation needs to be displayed in a one text box and the right in another text box.

Thanks
 
This might work

Put this code in the place where you want to use the function:

sum_to_target Me.A, Me.AA, Me.B, Me.BB, C

Then paste the code below into a module and check it compiles

Code:
Public Function sum_to_target(x_1 As Integer, L_1 As Integer, _
                              x_2 As Integer, L_2 As Integer, _
                              int_Targ As Integer)

    Dim bo_L_1 As Boolean, bo_L_2 As Boolean
    Dim i As Integer, j As Integer
    Dim int_Sum_1 As Integer, int_Sum_2 As Integer
    Dim ar_result, s_msg, s_bit, sc_result
        
    Do Until bo_L_1
        
        bo_L_2 = False
        j = 0
        int_Sum_1 = i * x_1
        bo_L_1 = (i >= L_1)
            
        Do Until bo_L_2
        
            int_Sum_2 = int_Sum_1 + j * x_2
            bo_L_2 = (j >= L_2)
            
            If int_Sum_2 = int_Targ Then
               log_result ar_result, x_1, i, x_2, j
            End If
            
            j = j + 1
            
        Loop
        
        i = i + 1
        
    Loop

    If Not IsArray(ar_result) Then
        MsgBox "The target cannot be reached"
        Exit Function
    End If
    
    sc_result = UBound(ar_result) + 1
    
    If sc_result > 1 Then s_bit = "s"
    
    s_msg = sc_result & " way" & s_bit & " of making " & int_Targ & vbCr & vbCr
    
    For i = 0 To UBound(ar_result)
        s_msg = s_msg & ar_result(i) & vbCr
    Next i

    MsgBox s_msg

End Function

Public Function log_result(ar_Res, x_1, i, x_2, j)

    If Not IsArray(ar_Res) Then
        ReDim ar_Res(0)
    Else
        ReDim Preserve ar_Res(UBound(ar_Res) + 1)
    End If

    ar_Res(UBound(ar_Res)) = Str(i) & "*" & Str(x_1) & " + " & _
                             Str(j) & "*" & Str(x_2)
                                                        
End Function
 
Another example given to me...

Dim A As Integer
Dim AA As Integer
Dim B As Integer
Dim BB As Integer
Dim C As Integer

Dim CVal As Integer
Dim AAcounter As Integer
Dim BBcounter As Integer
Dim DisplayText As String

A = Val(Text1.Text) 'TextBox on form, Access would be TextBox1
AA = Val(Text2.Text) 'TextBox on form, Access would be TextBox2
B = Val(Text3.Text) 'TextBox on form, Access would be TextBox3
BB = Val(Text4.Text) 'TextBox on form, Access would be TextBox4
C = Val(Text5.Text) 'TextBox on form, Access would be TextBox5


Do Until CVal >= C
'AA is the Number of Times you can use the A value
'AACount is a counter for how many times you have used A value

If AA > 0 And AAcounter < AA Then​
AAcounter = AAcounter + 1 'Loop Counter to check agianst how many times 'you can use this Variable​
If A <= C Then 'Make sure A value is less than C value​
If (CVal + A) <= C Then 'Make sure Current Value and Value to add 'does not exceed Value C​
CVal = (CVal + A) 'Add Current Value with Value A​
If CVal < C Then 'For Display information​
DisplayText = DisplayText & A & "+"​
Else​
DisplayText = DisplayText & A​
End If​
End If​
End If​
Else​
Exit Do 'Exit Loop​
End If​
Loop

Do Until CVal >= C
'BB is the Number of Times you can use the B value
'BBCount is a counter for how many times you have used B value​
If BB > 0 And BBcounter < BB Then​
BBcounter = BBcounter + 1 'Loop Counter to check agianst how many times you 'can use this Variable​
If B <= C Then 'Make sure B value is less than C value​
If (CVal + B) <= C Then 'Make sure Current Value and Value to add 'does not Exceed Value C​
CVal = (CVal + B) 'Add Current Value with Value B​
If CVal < C Then 'For Display information​
DisplayText = DisplayText & B & "+"​
Else​
DisplayText = DisplayText & B​
End If​
End If​
End If​
Else​
Exit Do 'Exit Do​
End If​
Loop


Text6.Text = DisplayText & "=" & CVal 'TextBox on form, Access would be TextBox6
 

Users who are viewing this thread

Back
Top Bottom