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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validdating Start Date

Status
Not open for further replies.

JackBurton07

IS-IT--Management
Oct 17, 2007
75
GB
Hi

I have a product start date which I am trying to validate. The user needs to input a date ( any date) in the correct format and then they will move down to the product selection combo box. I dont know how I can get it to validdate the date is in the correct format and then focus on the combo box - theres something wrong with my code

please help! thanks jb


Private Sub txtstartdate_Validating2(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtstartdate.Validating

Dim validdate As Boolean
validdate = False


If txtstartdate.Text.Length = 0 Then
MsgBox("Please the start date in DD/MM/YY format", MsgBoxStyle.Exclamation, "Empty field")


Else
Try
Dim cli_start As Date
cli_start = CDate(txtstartdate.Text)
Catch err As Exception
REM display a message box
MsgBox("Please enter the date correct format dd/mm/yy", MsgBoxStyle.Exclamation, "Incorrect date format")
txtstartdate.Text = "dd/mm/yy"
txtstartdate.Focus()

' problem is around here somewhere'

validdate = True
cboProduct1.Enabled = True
cboProduct1.Focus()
End Try





End If

End Sub
 
IsDate will tell you if it is in a valid date format, but if your looking for a specific date it will not work. A valid date can end up 2/12/2006, March 5, 2006, etc. You could check if it is valid and then try to format it if you just need it to be a valid date in a select format (as it looks like you want from the above code). If you need it to match a date then I wouldn't allow them to enter it, but use a combo box list of dates.

-I hate Microsoft!
-Forever and always forward.
 
Hi Thanks for this

I am going to change it to a datetimepicker to prevent user inputting via typing

I need the startdate value to pass to another text box to calculate the difference in the number of days

the problem is that as it's a datetimepicker I get a "conversion from string to date error is not valid". how can I get around this?

thanks

jb


Private Sub cboclient_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboclient.SelectedValueChanged
'select from the database the client with the name we selected'
Dim ans1, ans2, ans3, ans4 As Double



txtdaysrem.Text = getnoofdays(txtstartdate.Text, txtterm1.Text)

ans1 = txtrate.Text + 1
ans3 = txtdaysrem.Text / 365
ans4 = ans1 ^ ans3 'ans1 to power of ans3

ans2 = ans4 * txtInvestment1.Text
txtmat1.Text = ans2

End Sub

Function getnoofdays(ByVal DateFrom As Date, ByVal DateTo As Date) As Integer
Dim ans As Integer

ans = DateDiff(DateInterval.Day, DateFrom, DateTo)

Return ans

End Function









 
I've not used a datetimepicker so I don't know how it passes back it's date info. I would put a break on the line and see if it is passing back a valid date format (which I think it would). If so then one of two things should work.

Either:
Code:
  txtdaysrem.Text = getnoofdays("#" & txtstartdate.Text & "#", txtterm1.Text)

or
Code:
  txtdaysrem.Text = getnoofdays(Ctype(txtstartdate.Text, Date), txtterm1.Text)

-I hate Microsoft!
-Forever and always forward.
 
oops. Didn't notice both where text. You need to do the same for txtterm1.text

-I hate Microsoft!
-Forever and always forward.
 
I went back to just using a textbox as datetimepicker wouldnt work.

hmmmmm
 
Here is how you get value from "DateAndTimePicker"
Code:
    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        'Date & Time
        MessageBox.Show(Me.DateTimePicker1.Value)
        'Short Date Format
        MessageBox.Show(Me.DateTimePicker1.Value.ToShortDateString)
        'Long Date Format
        MessageBox.Show(Me.DateTimePicker1.Value.ToLongDateString)
    End Sub

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
do i need to convert it to a date to pass to the function ? or is it ok as string? sorry im a bit confused

is this right?

Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

'Short Date Format
cdate(Me.DateTimePicker1.Value.ToShortDateString)
 
Not necessarily requires to convert
try both
Code:
        If IsDate(Me.DateTimePicker1.Value) Then
            MessageBox.Show("It is a valid date")
        Else
            MessageBox.Show("It is not a valid date")
        End If
Code:
        If IsDate(Convert.ToDateTime(Me.DateTimePicker1.Value.ToShortDateString)) Then
            MessageBox.Show("It is a valid date")
        Else
            MessageBox.Show("It is not a valid date")
        End If
will return the same message

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
some date methods/functions require it be a date, but datediff doesn't seem to. It will take any object that contains a valid date.

-I hate Microsoft!
-Forever and always forward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top