SpinButton & Date Format

P

pkenchatt

I would like to use a SpinButton to change the date value held in
TextBox1. Using the follow code in the SpinUp event procedures works
------------------------------
Private Sub SpinButton1_SpinUp()
TextBox1.Text = Format(CDate(TextBox1.Text) + 1, "dd mm yyyy")
End Sub
------------------------------

But I need to return the date in the form "Monday 21 Dec" with no
year

------------------------------
Private Sub UserForm_Initialize()
TextBox1.Text = Format(Date + 1, "dddd d mmm")
End Sub
------------------------------

I would welcome any advice

Thanks
Phil
 
B

Bob Phillips

Phil,

The problem lies with the string your store in Textbox1. Although it looks
like a date, it is just a string.

I have used a date variable to hold the date, increment that, and display
it. like so

Dim myDate As Date

Private Sub SpinButton1_SpinUp()
myDate = myDate + 1
TextBox1.Text = Format(myDate, "dddd d mmm")
End Sub

Private Sub UserForm_Initialize()
myDate = Date + 1
TextBox1.Text = Format(myDate, "dddd d mmm")
End Sub
 
P

pkenchatt

Bob

Thanks, That did indead work. I was getting a little frustrated with
this problem because I knew I had solved it before, but couldn't
remember how. After 4 hours and at 6am UK time I posted my message.
Having read your advice I saw what the problem was

Private Sub SpinButton1_SpinUp()
TextBox1.Text = Format(SpinButton1.Value, "dddd d mmm")
End Sub

Private Sub UserForm_Initialize()
TextBox1.Text = Format(Date + 1, "dddd d mmm")
End Sub

Thanks again
Phil
 
B

Bob Phillips

I see. Should you not also initialise the spinbutton value to today's date +
1 (it is only a number after all.
 
P

pkenchatt

Hi Bob,

I am a little surprised but it would seem that setting the
SpinButton's Max and Min properties has initialised the control for
me. If I REM out these two values clicking SpinDown sets the TextBox
to Saturday 30 December (from which point if will not move further
down) SpinUp is free to move until the TextBox displays Saturday 9
April

Private Sub SpinButton1_SpinDown()
TextBox1.Text = Format(SpinButton1.Value, "dddd d mmm")
End Sub

Private Sub SpinButton1_SpinUp()
TextBox 1.Text = Format(SpinButton1.Value, "dddd d mmm")
End Sub

Private Sub UserForm_Initialize()
With SpinButton1
..Enabled = True
..Max = Date + 7
..Min = Date
End With
End Sub
 
T

Tom Ogilvy

Private Sub UserForm_Initialize()
With SpinButton1
..Enabled = True
..Max = Date + 7
..Min = Date
..Value = Date + 1
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top