I want to compare a date in a database to today's date and it they are equal. Display the contents of the related date in the database in datagridview on a new form.  


This is my code



Private Sub TimerAppoint_Tick(sender As Object, e As EventArgs) Handles TimerAppoint.Tick
        Dim TheDate As String = Now.ToString(" MMMM dd yyyy")
        Dim con As New OleDbConnection
        Dim cmd As New OleDb.OleDbCommand
        cmd = New OleDbCommand(Command, con)

        con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("Patients")

        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter("SELECT AppDate AS Expr7 FROM Appointments", con)
        da.Fill(dt)

        For Each DataRow In dt.Rows

            If (TheDate - 1) = (AppDateDateTimePicker.Text = DataRow(0)) Then

                appoint.Show()
                appoint.AppointmentsDataGridView.Show()

                con.Close()
                Return

            End If
        Next
        con.Close()
        Return
    End Sub

Dani AI

Generated

Short answer: treat appointment values as DateTime, not strings, and let the database do the filtering when possible. Pulling the whole table and comparing text is brittle (time parts, locale and formatting issues) and will make your Timer open the form repeatedly. Use a parameterized date-range query against Access (or, if you must check rows in memory, convert each field to DateTime and compare the .Date property).

Example (recommended: parameterized range, binds DataTable to the grid):

Using con As New OleDbConnection(ConfigurationManager.AppSettings("Patients"))
    Using cmd As New OleDbCommand("SELECT * FROM Appointments WHERE AppDate >= ? AND AppDate < ?", con)
        cmd.Parameters.Add(New OleDbParameter("StartDate", OleDbType.Date)).Value = DateTime.Today
        cmd.Parameters.Add(New OleDbParameter("EndDate", OleDbType.Date)).Value = DateTime.Today.AddDays(1)
        Dim dt As New DataTable()
        Using da As New OleDbDataAdapter(cmd)
            da.Fill(dt)
        End Using

        If dt.Rows.Count > 0 Then
            appoint.AppointmentsDataGridView.DataSource = dt
            appoint.Show()
        End If
    End Using
End Using

Notes and quick troubleshooting tips:

  • OleDb uses positional parameters — add them in the same order as the ? placeholders. Use OleDbType.Date so the provider sends a true date value, avoiding locale problems.
  • If AppDate contains a time component, equality to Today fails; the date-range above (>= today and < tomorrow) is reliable.
  • If you must loop rows, parse safely (e.g., Convert.ToDateTime(row("AppDate")).Date = DateTime.Today) and wrap DB work in Using/Try..Catch to avoid leaks.
  • Prevent repeated pop-ups from the Timer by disabling it once a notification is shown, setting a short cooldown, or marking a record as "Notified" in the DB.

Context from the thread: ’s point about matching formats is valid in principle, but avoid string comparison; ’s suggestion to filter in SQL is the right direction for performance; ’s loop idea works if the comparisons use typed DateTime values. ’s current If expression mixes strings and logic — switching to the approach above will fix that and keep the UI from opening repeatedly.

Recommended Answers

All 5 Replies

i do this with mysql :

Using conn As New MySqlConnection("myConnectionString")
   conn.Open()
   Dim command As New MySqlCommand("SELECT DATE_FORMAT(myDateColumn, '%d-%m-%Y') FROM myTable", conn)
      If command.ExecuteScalar = Format(Date.Now, "dd-MM-yyyy") Then
         MsgBox("this date is equal with now date")
      End If
   command.Dispose()
   conn.Close()
End Using

PS : i format date from mysql side to "14-07-2013", and so with vb side to "14-07-2013"

if you are using mssql then use this query

select field1 , field2 from table1 where table1.DateField = getdate()

Regards.

Im using access database hence i cant use those codes

I hope this small piece of codes can help you in some way:-

01 Protected Sub Calendar1_Selectionchanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.Selectionchanged
02 Label7.Text = Calendar1.SelectedDate().ToShortDateString
03 Calendar1.Visible = True
04 Dim GETDATE As Date
05 If Label7.Text > GETDATE Then
06 Label3.Text = "Please insert a valid date"
07
08
09 End If
10 End Sub

Ref: AllCodingtips.com

try this one

For Each datarow In dt.Rows
    if datenow = datarow.item(1) then   " datarow.item(1) = date in your database 
        Return True                     " indicate function you want
    End If
Next
    Return False
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.