I checked in another machine with another version of MS Access and I could not reproduce the OLE error, but I did find the culprit for the Enter key acting up against this method. The problem was in
Options > Client Settings > Move after enter > Next field
After setting it to
Don't move
, the method worked on that machine
View attachment 115087
However, I understand that others may not want to change that behavior. For those of you who enjoy going to the next field after pressing the Enter key, moving the calculation and rebinding behavior to the AfterUpdate event would allow you to still get the calculation done. In fact, that behavior resembles Excel even more and it looks like this:
Code:
Option Compare Database
Option Explicit
Private calc As Double
Private Sub CalculationTextbox_AfterUpdate()
If Left(Me.CalculationTextbox, 1) = "=" Then
calc = Eval(Mid(Me.CalculationTextbox.Text, 2))
Me.CalculationTextbox.ControlSource = "Calculation"
Me.CalculationTextbox.BorderStyle = 1
Me.CalculationTextbox = calc
End If
End Sub
Private Sub CalculationTextbox_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 61 'equals
Me.CalculationTextbox.ControlSource = ""
Me.CalculationTextbox.BorderStyle = 2
End Select
End Sub
Now that we're going into further details, it will probably be beneficial to also set the Form's Cycle property to Current Record
View attachment 115089
EDIT: I added a condition so that the calculation is done only if there is an equal sign, otherwise, if the value was entered directly into the textbox, the code was going to remove the first character. Many other ways exist to handle this, I'm just doing the simplest I could think of for now.