Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

VBA rewrite textbox with former value

Status
Not open for further replies.

atferrari

Well-Known Member
Most Helpful Member
VBA Excel

In a user form, the spin button brings a value up and down between 1 and 32. Value is shown in the textbox.

The user is given the possibility of typing the value directly in the textbox.

The code rejects it, if out of range, but fails to rewrite the old (good) one in the textbox to make evident that the last typed was not accepted.


Code:
Option Explicit
   Const TTR_val_max As Byte = 32
   Const TTR_val_min As Byte = 1

Private Sub TTR_control_enter()
   Dim TTR_val As Byte
   Dim TTR_val_old As Byte
   
   TTR_val_old = TTR_control.Value '???
   TTR_val = CByte(Val(TextBox_TTR.Text))
   
   If TTR_val > TTR_val_max Or TTR_val < TTR_val_min Then[ATTACH=full]81803[/ATTACH]
       TextBox_TTR = TTR_val_old '???
       Exit Sub
   Else
      'Range("GC4").Value = TTR_val - for debugging only
       TextBox_TTR = TTR_val
       TTR_control.Value = TTR_val
   End If
End Sub

Private Sub TTR_control_SpinDown()
   TextBox_TTR = TTR_control.Value
End Sub

Private Sub TTR_control_SpinUp()
   TextBox_TTR = TTR_control.Value
End Sub
I found that the typed value is processed no matter if a hit Enter or not. More to add to my confusion.

Any suggestion on how to put this straight?

I am sure I have something mixed up (properties and...?)

Gracias.
 

Attachments

  • TTR control.jpg
    TTR control.jpg
    4.7 KB · Views: 75
You need to move where you write the old value. Also see comment in code.

Try,
Code:
Private Sub TTR_control_enter()
   Dim TTR_val As Byte
   Dim TTR_val_old As Byte
  
   TTR_val = CByte(Val(TextBox_TTR.Text))
  
   If TTR_val > TTR_val_max Or TTR_val < TTR_val_min Then[ATTACH=full]81803[/ATTACH]
       TextBox_TTR = TTR_val_old '???
       Exit Sub
   Else
      'Range("GC4").Value = TTR_val - for debugging only
       TextBox_TTR = TTR_val            <---------- Should this be TTR_control.Value
       'TTR_control.Value = TTR_val   <---------- I'd remove this as it seems circular
       TTR_val_old = TTR_control.Value '???   End If
End Sub

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top