Click here to Skip to main content
15,913,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm creating a stock control system and i am at a stage where i need helping searching the database to return the data i want. For e.g i have a form called check stock now in this for i wish to enter the product id and hit search, and when i press search it should check against the ms access database if the data exists if not return an error message.

So far i have tried this but what happens is that i enter the correct product id it displays "data not found" but still displays the data, but incorrect product id says not found which is what it is meant to do.

Please can someone help me fix the error i get

Here is my code:


VB
Dim b As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source = " & fn
        If txtsearch.Text = "" Then
            MsgBox("Incorrect!", MsgBoxStyle.Critical)
        Else

            Dim a As New OleDb.OleDbConnection(b)
            a.Open()
            sql = ("SELECT[Product ID], [Product Name],[Qunatity in stock], [Quantity Ordered], [Date of order], [Supplier ID] FROM STOCK")


            Dim dscheckstock As New DataSet("Stock")
            Dim dacheckstock As New OleDb.OleDbDataAdapter

            dacheckstock = New OleDb.OleDbDataAdapter(sql, a)
            dacheckstock.Fill(dsstock, "stock")

            For index = 0 To dscheckstock.Tables("Stock").Rows.Count - 1
                Dim tblstock As DataTable
                tblstock = dscheckstock.Tables("Stock")


  If txtsearch.Text = dscheckstock.Tables("stock").Rows(index).Item("Product ID") Or _txtsearch.Text.Tolower = dscheckstock.Tables("stock").Rows(index).Item("Product Name") Then
                    txtsearch.Text = dscheckstock.Tables("Stock").Rows(index).Item("Product ID")
                    TextBox2.Text = dscheckstock.Tables("Stock").Rows(index).Item("Product Name")
                    TextBox3.Text = dscheckstock.Tables("Stock").Rows(index).Item("Qunatity in stock")
                    TextBox4.Text = dscheckstock.Tables("Stock").Rows(index).Item("Quantity Ordered")
                    TextBox5.Text = dsstock.Tables("Stock").Rows(index).Item("Date Of Order")
                    TextBox6.Text = (dscheckstock.Tables("Stock").Rows(index).Item("Supplier ID"))

                ElseIf index = dscheckstock.Tables("stock").Rows.Count - 1 Then


                    MsgBox("Not found!"  & "" & vbNewLine & Me.txtsearch.Text, MsgBoxStyle.Critical)

                End If

            Next





Thanks,
Posted
Updated 18-Feb-12 0:27am
v2
Comments
ledtech3 18-Feb-12 1:03am    
your code does not contain the words "data not found" so I would think it is returning from the database. Verify your Select Statement items are Spelled correctly and all of the names are actually in the data base.
And if you are wanting to just check if a Product ID exist then cut down what you are searching for.
("Qunatity in stock")is misspelled , that could possibly raise the "data not found" error.
alom_93 18-Feb-12 18:26pm    
thanks for your reply, the actual error i get is the line of code Not Found!
but the product ID that do exist i get the same error message which is not meant to happen.
The aim is i enter the product ID and the press search and all data is displayed on the product ID so cant really cut down, also i know quantity is misspelled but thats how it is on the actual database its self.

Any other suggestions why i get the error?
ledtech3 18-Feb-12 18:47pm    
Looking at your code again i see that you are checking for either the product ID or the product name , if they exist then return all of the info.
the line at the bottom
"ElseIf index = dscheckstock.Tables("stock").Rows.Count - 1 Then"
you might try
"ElseIf index = Nothing or -1 or < 0 Then" something like that.
alom_93 18-Feb-12 19:39pm    
i get an error if i try this
"ElseIf index = Nothing or -1 or < 0 Then"

it dont allow the <0
ledtech3 18-Feb-12 19:18pm    
I also just noticed that you are using your input text box as an output text box also. you may consider keeping them as seperate textboxes to avoid any problems there.So you would need to add anonther text box.

1 solution

This is what I have at the moment. It would not be the correct way to do this, but it does work. It will also throw a null error if a data item is returns is null. This error handeling type also depends on the textbox it is checking to be cleared each time you click the button.

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        tbDateOfOrder.Clear()
        tbProductID.Clear()
        tbProductName.Clear()
        tbQuantityOrdered.Clear()
        tbQunatityInStock.Clear()
        tbSupplierID.Clear()
        tbItemCount.Clear()
        Try
           
            Dim b As String = My.Settings.dsstockConnectionString 'this location can be edited in the settings or the app.config file
            Dim InputSearch As String = tbInput.Text
            If InputSearch = " " Or InputSearch = Nothing Then
                MsgBox("Nothing was input to search for", MsgBoxStyle.Critical, "No Input")
            Else

                Dim a As New OleDb.OleDbConnection(b)
                a.Open()

                Dim mySelect As String = ("SELECT[Product ID], [Product Name],[Qunatity in stock], [Quantity Ordered], [Date of order], [Supplier ID] FROM dsstock.STOCK")

               
                Dim adapter As OleDbDataAdapter = New OleDbDataAdapter(mySelect, b)
                Dim dscheckstock As DataSet = New DataSet
                adapter.Fill(dscheckstock, "Stock")


                For index = 0 To dscheckstock.Tables("Stock").Rows.Count - 1
                    Dim tblstock As DataTable
                    tblstock = dscheckstock.Tables("Stock")

                    ' below we set out input text .tolower and we also set the product id .tolower. and the same with the product name to handle case issues.
                    If dscheckstock.Tables("stock").Rows(index).Item("Product ID").ToString.ToLower.Contains(InputSearch.ToLower) Or _
                        dscheckstock.Tables("stock").Rows(index).Item("Product Name").ToString.ToLower.Contains(InputSearch.ToLower) Then

                        tbProductID.Text = dscheckstock.Tables("Stock").Rows(index).Item("Product ID")
                        tbProductName.Text = dscheckstock.Tables("Stock").Rows(index).Item("Product Name")
                        tbQunatityInStock.Text = dscheckstock.Tables("Stock").Rows(index).Item("Qunatity in stock")
                        tbQuantityOrdered.Text = dscheckstock.Tables("Stock").Rows(index).Item("Quantity Ordered")
                        tbDateOfOrder.Text = dscheckstock.Tables("Stock").Rows(index).Item("Date Of Order")
                        tbSupplierID.Text = (dscheckstock.Tables("Stock").Rows(index).Item("Supplier ID"))
                        'ElseIf tbProductID.Text = Nothing Then
                        '    MsgBox("Item Not found!" & "" & vbNewLine & InputSearch.ToString, MsgBoxStyle.Critical, "Item Not Found")
                    End If

                Next

                If tbProductID.Text = Nothing Then
                    MsgBox("Item Not found!" & "" & vbNewLine & InputSearch.ToString, MsgBoxStyle.Critical, "Item Not Found")
                End If

                a.Close() ' close the database
                tbItemCount.Text = dscheckstock.Tables("stock").Rows.Count 'count the number of rows
            End If

        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
 
Share this answer
 
Comments
ledtech3 19-Feb-12 23:40pm    
Found a problem with that code, and the system won't let me fix it here ,every time i hit Improve solution it goes to the original post instead of the the solution i posted. Any way replace this Line
If dscheckstock.Tables("stock").Rows(index).Item("Product ID").ToString.ToLower.Contains(InputSearch.ToLower) Or _
dscheckstock.Tables("stock").Rows(index).Item("Product Name").ToString.ToLower.Contains(InputSearch.ToLower) Then
with this: because if there are two item that both contain the string then it may return the wrong one.
If dscheckstock.Tables("stock").Rows(index).Item("Product ID").ToString.ToLower = (InputSearch.ToLower) Or _
dscheckstock.Tables("stock").Rows(index).Item("Product Name").ToString.ToLower = (InputSearch.ToLower) Then
alom_93 20-Feb-12 19:08pm    
THANK YOU SO MUCH IT FINALLY WORKS!! THANKS FOR ALL YOUR HARD WORK!!!!
THANK YOU
ledtech3 20-Feb-12 19:23pm    
Your welcome, any time.
Glad I could help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900