I have the following code for part of an application I'm building and I need some instruction on how to store this value and carry it to the next part of the program.

Private Sub dateEntryTextbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dateEntryTextbox.TextChanged
        Dim dateEntryTextBox As Date
        Try
            FormatDateTime(DateString, DateFormat.GeneralDate)
            MsgBox("Valid Date")
        Catch ex As Exception
            MsgBox("Invalid Date")
        End Try
    End Sub

the dateEntryTextbox will be a date entered that I want to store as a date and use it to fire a sql query in another button on my app. Any assistance would be appreciated.

Thanks

Doug

Dani AI

Generated

For a Windows Forms app (as confirmed), the simplest and safest pattern is to keep the chosen date in a form-level variable and pass that DateTime to SQL as a parameter. The DateTimePicker exposes a DateTime via its Value property, so avoid formatting the date into strings for SQL. See the control's Value documentation for details: DateTimePicker.Value.

A compact pattern in VB:

' form-level
Private selectedDate As DateTime?

' capture from the picker
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
    selectedDate = DateTimePicker1.Value.Date
End Sub

' use in the query button
Private Sub BtnRun_Click(sender As Object, e As EventArgs) Handles BtnRun.Click
    If Not selectedDate.HasValue Then
        MessageBox.Show("Select a date first.")
        Return
    End If

    Using conn As New SqlConnection(connString)
        Using cmd As New SqlCommand("SELECT ... WHERE MyDate = @d", conn)
            cmd.Parameters.Add("@d", SqlDbType.DateTime).Value = selectedDate.Value
            conn.Open()
            ' execute reader/adapter/etc.
        End Using
    End Using
End Sub

If the source is a textbox instead of a picker, validate with DateTime.TryParse or TryParseExact (for a fixed format) before storing:

Dim dt As DateTime
If DateTime.TryParse(textBox1.Text, dt) Then
    selectedDate = dt.Date
Else
    MessageBox.Show("Invalid date format.")
End If

Notes and cautions: always use parameterized SQL (avoid string concatenation) and pick the correct SqlDbType (Date vs DateTime/DateTime2) to match the column. For fixed-format input, prefer TryParseExact with an explicit culture. For more on parsing and parameters see DateTime.TryParse and SqlParameter.

u r working on web application or windows application

Actually I'm working on a windows application and have decided to go with a datetimepicker instead of a text box but I guess the same question still applies. Also because the datetimepicker variable has to be stored as a date, there will have to be some conversion as well (because I'm applying the date to a sql query that has already been written.) The datetime conversion (as it's currently written) is as follows:

Format$((([Timestamp]-(([LoggedIn]/1000)/60))/1440)+1,"yy/mm/dd")
HAVING (((Format$((([Timestamp]-(([LoggedIn]/1000)/60))/1440)+1,"yy/mm/dd"))
Between [Start (YY/MM/DD)] And [End (YY/MM/DD)]));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.