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?

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.