Userform

Please can you help with the attached

On the userform I want to amend the code so that the spinbutton1 alters the date by +1 (Add/Subtract) in textbox1

 Book1.xls
Kiwi-123Asked:
Who is Participating?
 
[ fanpages ]IT Services ConsultantCommented:
For example, see attached...
Book1-Updated.xls
0
 
[ fanpages ]IT Services ConsultantCommented:
Add two event subroutines like this...

Private Sub SpinButton1_SpinUp()

  If IsDate(TextBox1.Text) Then
     TextBox1.Text = Format$(DateAdd("d", 1, CDate(TextBox1.Text)))
  End If
     
End Sub
Private Sub SpinButton1_SpinDown()

  If IsDate(TextBox1.Text) Then
     TextBox1.Text = Format$(DateAdd("d", -1, CDate(TextBox1.Text)))
  End If

End Sub

' --- rest of your existing code ---

BFN,

fp.
Private Sub SpinButton1_SpinUp()

  If IsDate(TextBox1.Text) Then
     TextBox1.Text = Format$(DateAdd("d", 1, CDate(TextBox1.Text)))
  End If
     
End Sub
Private Sub SpinButton1_SpinDown()

  If IsDate(TextBox1.Text) Then
     TextBox1.Text = Format$(DateAdd("d", -1, CDate(TextBox1.Text)))
  End If

End Sub

Open in new window

0
 
Kiwi-123Author Commented:
Thanks Fanpages, I just need to tweak at the code so that:-

TextBox2.SetFocus

Is performed after the Spinbutton click. My code works if I press the spin button twice, but not if just once?

Private Sub SpinButton1_SpinUp()

  If IsDate(TextBox1.Text) Then
     TextBox1.Text = Format$(DateAdd("d", 1, CDate(TextBox1.Text)))
     TextBox2.SetFocus
  End If
     
End Sub
0
 
[ fanpages ]IT Services ConsultantCommented:
Hi,

Try placing a 'DoEvents' call in between the two lines, thus:

Private Sub SpinButton1_SpinUp()

  If IsDate(TextBox1.Text) Then
     TextBox1.Text = Format$(DateAdd("d", 1, CDate(TextBox1.Text)))
     DoEvents
     TextBox2.SetFocus
  End If
     
End Sub

BFN,

fp.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.