Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date format but not current date HOW? 1

Status
Not open for further replies.

Svanholm

Programmer
Jul 11, 2007
31
0
0
DK
I am new to VB. Work with VB5 Enterprise.

I think this should be simple, but I cannot figure it out:

How do I create a text-box in which the user can write a random date that is not today's date. And still keep the date format?

Example: User inputs 12122009 and I want textbox to change into: 12-12-2009 or 12:12:09 or 12-12-09 or 12:12:2009.

The change should be instant or at the latest at "lost focus".

All the examples I have found work with the current date.

Any ideas?

All the best
Svanholm

 
How about
Code:
If IsDate(txtDate) Then
   txtDate = Format(txtDate, "mm-dd-yyyy")
End If

I would say put it in the LostFocus event. You can't really do it "instantly" because the user is still typing it.

 

Since Svanholm is new to VB, I would discurage using default property of controls and encurage spelling it out in the code:
Code:
If IsDate(txtDate[blue].Text[/blue]) Then
   txtDate[blue].Text[/blue] = Format(txtDate[blue].Text[/blue], "mm-dd-yyyy")
End If

Have fun.

---- Andy
 

Just to note that the OP calls for an input of

>Example: User inputs 12122009

and therefore you will often need to first parse the input without the date format, otherwise you may get an overflow error.

Also, you will need to use the txtDate_Validate event, as there is no guarentee that the LostFocus will always fire.
 
Code:
Private Sub txtDate_Change()

Dim lsTemp As String
Dim lsDay As String
Dim lsMonth As String
Dim lsYear As String

lsTemp = Replace(txtDate.Text, "-", vbNullString)
If Len(lsTemp) >= 6 Then
    'Assume 2 digit day, month
    lsMonth = Left$(lsTemp, 2)
    lsDay = Mid$(lsTemp, 3, 2)
    lsYear = Mid$(lsTemp, 5)
    With txtDate
        .Text = lsMonth & "-" & lsDay & "-" & lsYear
        .SelStart = Len(.Text)
    End With
End If
End Sub

There are quite a few assumptions in the code above but it should get you started.
 
Svanholm,

What is the requirement for entering the date into your text box? Do they have to put in two digits for the day, two digits for the month and four digits for the year? If there are no requirments, you may consider using three seperate text boxes; one for the day, one for the month and one for the year. Then in your code you can combine them using the separator of your choice and the format of your choice, i.e. dd-mm-yyyy, mm/dd/yy, etc.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
Hi all

Thanks for very clever suggestions.

Have tried the first two without luck. Am propably doing something wrong.

Do not quite understand SBerthold's suggestion. Am surely to unexperienced for that.

Am gonna try out Nimroduk's routine. Looks like it's down the right path.

And HyperEngineer's suggestion is very interesting to. I shall certainly consider that. And will definately try it. It has the advantage that it clearly shows the format logic to user.

Be back later.

Thanks guy's really appreciate.
 
If you're happy to split the entry across three textboxes then I would recommend going down that path too :)
 
I know this is not what OP was asking for, but....

When I need a Date from user, I do put the text box on the Form, but in _Click event I call MonthView control (Project - Components..., choose Microsoft Windows Common Controls-2 6.0)

And user can just pick the Date - no typing needed, no validation required.

Look into it.


Have fun.

---- Andy
 
Hi Nimroduk

Tried your suggestion both with "change" and "Lost Focus". Get compile Error message: Sub or Function not defined. Highlights "Replace".

Clue?


Dim lsTemp As String
Dim lsDay As String
Dim lsMonth As String
Dim lsYear As String

lsTemp = Replace(txtDate.Text, "-", vbNullString)
If Len(lsTemp) >= 6 Then
'Assume 2 digit day, month
lsMonth = Left$(lsTemp, 2)
lsDay = Mid$(lsTemp, 3, 2)
lsYear = Mid$(lsTemp, 5)
With txtDate
.Text = lsMonth & "-" & lsDay & "-" & lsYear
.SelStart = Len(.Text)
End With
End If
End Sub
 
Andrzejek

Salute!
That was a great suggestion.

That seems like the simple solution I search.

Do put the MonthView on a seperate Form or can you make it show by click event in the txtDate box? cause that I couldnt figue out so far.

Admit I am a newbie but I am trying reeeal hard (to be the shepard, Ringo!) :)

Best
Svanholm

 

You may not have Replace function available in VB 5 (it is in VB 6.0)

What do you see in Intelisense when you type:

replace(

?

Or just type Replace, highlight it and press F1 (help)
If nothing, you can not use Replace in VB 5 :-(



Have fun.

---- Andy
 

Try:
Code:
Private Sub txtDate_Click()

With MonthView1
    .Top = txtDate.Top + txtDate.Height
    .Left = txtDate.Left
    If Len(txtDate.Text) = 0 Then
        .Value = Now
    Else
        .Value = txtDate.Text
    End If
    .Visible = False
    .BorderStyle = cc2None
End With

End Sub

Private Sub MonthView1_DateClick(ByVal DateClicked As Date)

txtDate.Text = MonthView1.Value

End Sub

Code not tested (just from teh 'top of my head'), but it should be pretty close.

Have fun.

---- Andy
 
Oh, yes. Place the MonthView in te same Container as your txtDate (straight on the Form itself, I guess)

You can use a separate Form just for MonthView, but that's a lot more work

Have fun.

---- Andy
 
Andrzejek,

Looks cool. Will test it as soon as kid is tugged it. It's evening here (Denmark Europe).

So far thx a million. Guys like you (and forums) are just the perfect supplement to the VB-books I am chewing my way through.


 
Couldnt wait. had to try it.

It is absolutely perfect.

I use your code in the txtDate_click event (with .Visible True) to open the MonthView

And with .Visible False in MonthView1_DateClick event to close it.

Combined with txtDate=MonthView1 before your code in the same sub.

Just beautiful, userfriendly and quite logical coding once you know about the MonthView component.

I learn a lot, studying this.

Thanks pal
 

Glad to help :)

I fly by Copenhagen from time to time (from US to Warsaw), not very crazy about the airport thou.


Have fun.

---- Andy
 
Here's one for the very bright ones

I was working on my wife's pc with Adrzejeks great solution.

It worked just perfectly

Then I went to my own PC and guess what: the component: Microsoft common controls 2 6.0 is not available.

It's the exact same installation. I only have the same cd.

My wife's pc (Labtop) is quite new. Mine (desktop) is not. But both run Win XP SP2. My wife with Explorer 6. 0 Mine Explorer 7.0

Any ideas?

 
Found a way around it though not an explanation.

Copied the *ocx in question (Mscomct2.ocx) from my wife's pc to mine. Worked instantly.

They should deliver those components seperately. Didnt find it online.

BUT even better: Within Mscomct2.ocx, there's a component called DTpicker (Datepicker I guess) and It does exactly the same as Monthview+Andrjezek's coding.

Your work wasnt in vain Andrjezek. Cause I learnt quite a few other things from reading/writing and testing your coding. Besides I may even use your version, cause i'm not sure the properties of DTPicker allows me to define where it should appear.

Still, If someone has a clue why the components werent part of the installation on my pc - and yet on my wife's - I gladly hear from you. I mean: What else am I missing??

Best
Svanholm
 
Be warned that the DTPicker control has a reputation for being buggy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top