control variable

ClaraBarton

Registered User.
Local time
Today, 15:27
Joined
Oct 14, 2019
Messages
645
Code:
Public Function FldCalc(frm As Form, ctrl As Control)
On Error Resume Next
    
    Select Case ctrl.Name
        Case "txtPayment"
            If Left(frm.ctrl, 1) = "=" Then frm.ctrl = Right(frm.ctrl, Len(frm.ctrl) - 1)
            frm.Payment = 0
            frm.Payment = Eval(frm.ctrl)
            frm.ctrl = frm.Payment
            RetAmt frm, txtPayment
        Case "txtReceipt"
            If Left(frm.ctrl, 1) = "=" Then frm.ctrl = Right(frm.ctrl, Len(frm.ctrl) - 1)
            frm.Payment = 0
            frm.Payment = Eval(frm.ctrl)
            frm.ctrl = frm.Receipt
            RetAmt frm, "txtReceipt"
    End Select
    
End Function

'Payment and Receipt in AfterUpdate Event
Public Function RetAmt(frm As Form, tbx As Control)

Select Case tbx.Name
    Case txtPayment
        frm.Receipt = Null
        frm.txtReceipt = Null
        frm.Amount = -tbx.Value
    Case "txtReceipt"
        frm.Payment = Null
        frm.txtPayment = Null
        frm.Amount = tbx.Value
End Select

End Function
This is a little routine that allows calculations in the field. It worked fine in the form module but I was using it in several places so I put it in a separate module and now I seem to be stumped.
The variable for control will not compile.
RetAmt frm, txtPayment returns "variable not defined" .
All forms and subforms have the same textboxes with the same names so this seemed like the way to go. The first function does the calculating and the second one puts the results in the right field.
I guess if the controls have the same name, I wouldn't have to use a variable...
 
RetAmt frm, txtPayment the txtPayment is not dimensioned anywhere that I can see in your code. That is the part that doesn't compile for me.
 
Need quote marks? - "txtPayment"

Also: Case "txtPayment"

Since function RetAmt is not actually returning anything, why not make this a Sub instead?

Why pass control and not just control name as text since you don't actually manipulate control, just using its name?
 
Last edited:
ok. Is there a disadvantage to using a function over a sub?
On further thought... I am manipulating the control. It depends on whether it's a payment or a receipt. I need to know.
I have both a txtPayment and a txtReceipt. Thus, Case ctrl
 
Last edited:
Try this.

Code:
Public Function FldCalc(frm As Form, ctrl As Control)
On Error Resume Next
     Select Case ctrl.Name
        Case "txtPayment"
            If Left(ctrl, 1) = "=" Then frm.ctrl = Right(ctrl, Len(ctrl) - 1)
            frm.Payment = 0
            frm.Payment = Eval(ctrl)
            ctrl = frm.Payment
            RetAmt frm, ctrl
        Case "txtReceipt"
            If Left(ctrl, 1) = "=" Then ctrl = Right(ctrl, Len(ctrl) - 1)
            frm.Payment = 0
            frm.Payment = Eval(ctrl)
            ctrl = frm.Receipt
            RetAmt frm, ctrl
    End Select
    
End Function

'Payment and Receipt in AfterUpdate Event
Public Function RetAmt(frm As Form, tbx As Control)

Select Case tbx.Name
    Case "txtPayment"
        frm.Receipt = Null
        frm.txtReceipt = Null
        frm.Amount = -tbx.Value
    Case "txtReceipt"
        frm.Payment = Null
        frm.txtPayment = Null
        frm.Amount = tbx.Value
End Select

End Function
 
Here are some problems.
you have Frm which is a form object, and control which is a control object.
You can do something like
frm.Property
frm.Method
but not frm.SomeObject
frm.Ctrl will not work and no reason to do that simply ctrl.

You have
RetAmt frm, "txtReceipt
but it takes a form object and a control object so
RetAmt frm, ctrl
 
Code:
Private Sub txtPayment_AfterUpdate()
    On Error Resume Next
    If Left(txtPayment, 1) = "=" Then txtPayment = Right(txtPayment, Len(txtPayment) - 1)
    Payment = 0
    Payment = Eval(txtPayment)
    txtPayment = Payment
    RetAmt Me
End Sub


Private Sub txtReceipt_AfterUpdate()
    On Error Resume Next
    If Left(txtPayment, 1) = "=" Then txtPayment = Right(txtPayment, Len(txtPayment) - 1)
    Payment = 0
    Payment = Eval(txtPayment)
    txtPayment = Payment
    RetAmt Me
End Sub

Public Function RetAmt(frm As Form)
Dim tbx As Control
Set tbx = Screen.ActiveControl

Select Case tbx.Name
    Case "txtPayment"
        frm.Receipt = Null
        frm.txtReceipt = Null
        frm.Amount = -tbx.Value
    Case "txtReceipt"
        frm.Payment = Null
        frm.txtPayment = Null
        frm.Amount = tbx.Value
End Select

End Function
 
Majp... that compiles but when in the module I do this:
Code:
Private Sub txtPayment_AfterUpdate()
    FldCalc Me, txtPayment
End Sub
txtPayment returns the value and not the field ie 100+150
"txtPayment" returns type mismatch
txtPayment is the control name and also the field name
 
Then what are you referencing with frm.Payment and frm.Receipt?

Select Case shouldn't be needed. Pass -1 or 1 as an argument and multiply. Pull field name from passed control name or pass field name as argument.
 
Last edited:
Here is an update. This works for me fine.
Code:
Public Function FldCalc(frm As Form, ctrl As Control)
On Error Resume Next
     Select Case ctrl.Name
        Case "txtPayment"
            If Left(ctrl, 1) = "=" Then ctrl = Right(ctrl, Len(ctrl) - 1)
            frm.Payment = 0
            frm.Payment = Eval(ctrl)
            ctrl = frm.Payment
            RetAmt frm, ctrl
        Case "txtReceipt"
            If Left(ctrl, 1) = "=" Then ctrl = Right(ctrl, Len(ctrl) - 1)
            frm.Payment = 0
            frm.Payment = Eval(ctrl)
            ctrl = frm.Payment
            RetAmt frm, ctrl
    End Select
    
End Function
 

Attachments

Payment and Receipt are also fields. txtPayment and txtReceipt are fields on top of the others for calculations.
 
So txtPayment and txtReceipt are controls (textboxes) NOT fields.
 
Both. textboxes that contain fields called txtPayment. Default by Access. Would you change it?
 
Majp... I would have NEVER, NEVER have figured out those little changes. Thank you so much.
Made my day! I am so bad at this that the only reason I keep going is because I look on it like a crossword puzzle or sudoku. Tiny bit at a time!
 
not sure what your UI is, but here is a simple pop up calculator if that is what you are doing.
 

Attachments

I have another example of entering a calculation in a TextBox control with a helper class.

CalculationTextBox.gif


Code in form:
Code:
Private m_CalcTextBox As CalculationTextBox

Private Sub tbQty_GotFocus()
   Set m_CalcTextBox = CalculationTextBox(Me.tbQty)
End Sub

Private Sub tbQty_LostFocus()
   Set m_CalcTextBox = Nothing
End Sub

Class CalculationTextBox: https://github.com/AccessCodeLib/AccessCodeLib/blob/draft/usability/CalculationTextBox.cls
 

Attachments

Last edited:
Good question. If I had to choose, I usually choose a function unless I'm really sure I just need a sub. But I really don't know what the disadvantages would be.
All else being equal, that makes the most sense to me too
 

Users who are viewing this thread

Back
Top Bottom