I gave it another shot. The reason it writes the expression in the new record seems to be because it's an unbound control, or so I believe. For now, I switched to use the keyup and keydown events to handle this. One version looks like this and it does not write the expression in a new record, if that was a problem for someone:
And I added another version of the form that takes care of the expression in a new record, if that is a problem. This version is reactive and it detects common operators in the expression being written. That means you don't need to type the equals sign, it will simply check if there is an operator and it will try to calculate when Enter is pressed.
Both the detail paint and form current event can take care of the expression in the new record, if that is a problem. I hope it helps someone.
Just to help others see some potential, I wrapped the first one in a class to adopt it very easily. Again, this is just a prototype, add whatever else you want. Here's the little class:
With the class, using it is as simple or complicated as this:
Code:
Option Explicit
Private Sub CalculationTextbox_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Dim calc As Double
If Len(Nz(Me.CalculationTextbox.Text, "")) <> 0 Then
calc = Eval(Me.CalculationTextbox.Text)
End If
Me.CalculationTextbox.ControlSource = "Calculation"
Me.CalculationTextbox.BorderStyle = 1
Me.CalculationTextbox = calc
Me.lblExcelMode.Visible = False
End If
End Sub
Private Sub CalculationTextbox_KeyUp(KeyCode As Integer, Shift As Integer)
If Me.CalculationTextbox.Text = "=" Then
Me.CalculationTextbox.ControlSource = ""
Me.CalculationTextbox.BorderStyle = 2
Me.lblExcelMode.Visible = True
End If
End Sub
And I added another version of the form that takes care of the expression in a new record, if that is a problem. This version is reactive and it detects common operators in the expression being written. That means you don't need to type the equals sign, it will simply check if there is an operator and it will try to calculate when Enter is pressed.
Code:
Option Explicit
Private Sub CalculationTextbox_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Dim calc As Double
If Len(Nz(Me.CalculationTextbox.Text, "")) <> 0 Then
calc = Eval(Me.CalculationTextbox.Text)
End If
Me.CalculationTextbox.ControlSource = "Calculation"
Me.CalculationTextbox.BorderStyle = 1
Me.CalculationTextbox = calc
Me.lblExcelMode.Visible = False
End If
End Sub
Private Sub CalculationTextbox_KeyUp(KeyCode As Integer, Shift As Integer)
Dim inputText As String
inputText = Me.CalculationTextbox.Text
If InStr(inputText, "/") > 0 Or _
InStr(inputText, "*") > 0 Or _
InStr(inputText, "+") > 0 Or _
InStr(inputText, "-") > 0 Or _
InStr(inputText, "(") > 0 Or _
InStr(inputText, ")") > 0 Then
Me.CalculationTextbox.ControlSource = ""
Me.CalculationTextbox.BorderStyle = 2
Me.CalculationTextbox = inputText
Me.CalculationTextbox.SelStart = Len(inputText)
Me.lblExcelMode.Visible = True
End If
End Sub
Private Sub Detail_Paint()
If IsNull(Me.SomethingID) Then
Me.CalculationTextbox.ControlSource = ""
Me.CalculationTextbox = 0
Me.CalculationTextbox.ControlSource = "Calculation"
End If
End Sub
'Private Sub Form_Current()
' If IsNull(Me.SomethingID) Then
' Me.CalculationTextbox.ControlSource = ""
' Me.CalculationTextbox = 0
' Me.CalculationTextbox.ControlSource = "Calculation"
' End If
'End Sub
Both the detail paint and form current event can take care of the expression in the new record, if that is a problem. I hope it helps someone.
Just to help others see some potential, I wrapped the first one in a class to adopt it very easily. Again, this is just a prototype, add whatever else you want. Here's the little class:
Code:
Option Explicit
Private WithEvents m_txtbox As TextBox
Public Sub Init(txtbox As TextBox)
Set m_txtbox = txtbox
m_txtbox.OnKeyDown = "[Event Procedure]"
m_txtbox.OnKeyUp = "[Event Procedure]"
End Sub
Private Sub m_txtbox_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Dim calc As Double
If Len(Nz(m_txtbox.Text, "")) <> 0 Then
calc = Eval(m_txtbox.Text)
End If
m_txtbox.ControlSource = "Calculation"
m_txtbox.BorderStyle = 1
m_txtbox.Value = calc
End If
End Sub
Private Sub m_txtbox_KeyUp(KeyCode As Integer, Shift As Integer)
If m_txtbox.Text = "=" Then
m_txtbox.ControlSource = ""
m_txtbox.BorderStyle = 2
m_txtbox.Visible = True
End If
End Sub
With the class, using it is as simple or complicated as this:
Code:
Private xlTextbox As ExcelLikeTextbox
Private Sub Form_Load()
Set xlTextbox = New ExcelLikeTextbox
xlTextbox.Init Me.CalculationTextbox
End Sub
Attachments
Last edited: