i have sucessfully coded my form so that when a user enters data into the texxt box and searches any matching data is displayed on another form my problem is when no data matches the search criteria the form that would display the result still opens but is blank how do i create a message that tells the user there is no matching crieria and stop the other form from opening?

Dani AI

Generated

A few concise, practical points to close this out based on the thread from , and .

Always check the query results immediately after the query and before you open or close forms. For a typed DataSet check the target DataTable row count (for example the typed table's Rows.Count); DataSet.Tables.Count is the number of tables in the DataSet, not the number of result rows. Make sure any Me.Close() or navigation only happens inside the branch that confirms rows were returned so the search form is not closed when the result set is empty.

Two common gotchas seen above: repeated calls to a TableAdapter.Fill can append rows unless you clear the DataTable first, and placing Me.Close() after the If/End If (outside the conditional) will close the search form even on a "no results" path. You can also use the integer return from a Fill call (it returns the number of rows added) as a quick check. See the adapter Fill behavior in the docs: DbDataAdapter.Fill. If you use a DataReader, HasRows is the property to check: DbDataReader.HasRows.

Extra tips for a robust UX: validate and trim search input before querying; show a clear message like "No records found for '<term>'" and set focus back to the search field; wrap DB calls in Try/Catch to surface errors; clear the destination table before filling; and prefer passing just the result DataTable (not an entire DataSet) to the display form. A simple flow to follow: clear table → run Fill (or read) → if rows > 0 then open result form and close/hide search form; else show message and keep the search form visible.

Recommended Answers

All 6 Replies

If you use a DataReader then the answer is quite easy.
The reader has a property called HasRows that indicates whether or not the database query was successful, or not.
Put the code for opening the form inside an If DataReader1.HasRows = True Then statement, and the code for displaying a MessageBox inside an Else statement.

If you're using the DataAdapter method and use that to fill a DataSet then you can check either DataSet1.Tables.Count or DataSet1.Tables(0).Rows.Count.

i have sucessfully coded my form so that when a user enters data into the texxt box and searches any matching data is displayed on another form my problem is when no data matches the search criteria the form that would display the result still opens but is blank how do i create a message that tells the user there is no matching crieria and stop the other form from opening?

Hi thank you my select query was done using the query qizard within the data set
SELECT FROM WHERE
i then put my code within the search button which was

Me.NamesTableAdapter.FillByName(Me.DataSet.Names, Me.ForenameTextBox.Text, Me.ForenameTextBox.Text, Me.SurnameTextBox.Text, Me.SurnameTextBox.Text, Me.RoleTextBox.Text, Me.RoleTextBox.Text)

        Dim frm As New Staff
        frm.SetEmployeeData(Me.DataSet.Names)
        frm.Show()
        Me.Close()

so would i change it to

If DataReader1.HasRows = True Then
Me.NamesTableAdapter.FillByName(Me.DataSet.Names, Me.ForenameTextBox.Text, Me.ForenameTextBox.Text, Me.SurnameTextBox.Text, Me.SurnameTextBox.Text, Me.RoleTextBox.Text, Me.RoleTextBox.Text)

        Dim frm As New Staff
        frm.SetEmployeeData(Me.DataSet.Names)
        frm.Show()
        Me.Close()
ELSE
Msgbox("")

Check whether record count is 0 or more than. If its >0 then show ur searh data into second form, else redirect into first form (Search form) with message box

thank you im quite new to this would it be..

If DataSet.Tables.Count = 0 Then


            Me.namesTableAdapter.FillByName(Me.DataSet.names, Me.ForenameTextBox.Text, Me.ForenameTextBox.Text, Me.SurnameTextBox.Text, Me.SurnameTextBox.Text, Me.RoleTextBox.Text, Me.RoleTextBox.Text)

            Dim frm As New Staff
            frm.SetEmployeeData(Me. DataSet.names)
            frm.Show()
            Me.Close()
        Else 
message box....

Perhaps something like this.
The check should be performed after the query.

Me.NamesTableAdapter.FillByName(Me.DataSet.Names, Me.ForenameTextBox.Text, Me.ForenameTextBox.Text, Me.SurnameTextBox.Text, Me.SurnameTextBox.Text, Me.RoleTextBox.Text, Me.RoleTextBox.Text)

If Me.DataSet.Names.Rows.Count > 0 Then
        Dim frm As New Staff
        frm.SetEmployeeData(Me.DataSet.Names)
        frm.Show()
Else
        MessageBox.Show("No records found")
End If
        Me.Close()

thank you for your time and help

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.