10 Replies - 1676 Views - Last Post: 29 December 2013 - 07:05 AM Rate Topic: -----

#1 dimdim80  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 69
  • Joined: 28-December 08

visual basic search button

Posted 12 December 2013 - 03:18 PM

My code is the following:
i have an access database which i upload.i am able to navigate and display the recordes from the database everything is fine(for saving time i have not included the code for the four navigation buttons).
My question is the following: i want to create another textbox where when i enter a name for example and by pressing a search button it would automatically fill the corrspoding textbox,in taht case it would be 'txtFirstName',this is the name of my textbox.

Dim inc As Integer
    Dim con As New OleDb.OleDbConnection
    Dim dbprovider As String
    Dim dbsource As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim MaxRows As Integer

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
        Me.TblContactsTableAdapter.Fill(Me.AddressBookDataSet.tblContacts)


        dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        dbSource = "Data Source = C:/AddressBook.mdb"

        con.ConnectionString = dbprovider & dbsource
        con.Open()
        sql = "Select * from tblContacts"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "AdrressBook")
        con.Close()
        MaxRows = ds.Tables("AddressBook").Rows.Count
        inc = -1


    End Sub
    Private Sub NavigateRecords()

        txtFirstName.Text = ds.Tables("AddressBook").Rows(inc).Item(1)


    End Sub














So far i have not found a solution or the solutiuons i have found are not understanble.
Thank you in advance.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: visual basic search button

#2 thecoat  Icon User is offline

  • D.I.C Regular

Reputation: 75
  • Posts: 267
  • Joined: 07-December 13

Re: visual basic search button

Posted 12 December 2013 - 10:45 PM

Similar to doing:

sql = "Select * from tblContacts"


You can select specific records using a WHERE clause.

"Select * from tblContacts Where SurName = 'Smith'"

Was This Post Helpful? 0
  • +
  • -

#3 dimdim80  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 69
  • Joined: 28-December 08

Re: visual basic search button

Posted 13 December 2013 - 03:34 AM

Yes i know this,but what i want is how to type for example in the textbox for name ,a specific name and by pressing the search button if it exists to automatically fill the correspoding textboxes.

This post has been edited by dimdim80: 13 December 2013 - 03:41 AM

Was This Post Helpful? 0
  • +
  • -

#4 x-logan  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 83
  • Joined: 18-April 13

Re: visual basic search button

Posted 13 December 2013 - 04:17 AM

for this purpose you can use DataReader
Was This Post Helpful? 1
  • +
  • -

#5 dimdim80  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 69
  • Joined: 28-December 08

Re: visual basic search button

Posted 15 December 2013 - 06:32 PM

problem solved thanks!!


One more question if you do not mind...whenever i add a new record i have to restart my programm in order for the record to be shown?any ideas how to do it withount restarting?updating a record is not a aproblem though.Thanks
Was This Post Helpful? 0
  • +
  • -

#6 x-logan  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 83
  • Joined: 18-April 13

Re: visual basic search button

Posted 16 December 2013 - 02:26 AM

i can't really tell you without seeing your code...
because i don't know whether you are using datagrid or something else..
In order to reply i want to have a look in your code, if you don't mind !
Was This Post Helpful? 0
  • +
  • -

#7 dimdim80  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 69
  • Joined: 28-December 08

Re: visual basic search button

Posted 16 December 2013 - 03:22 AM

    Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbsource As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim inc As Integer
    Dim MaxRows As Integer
    

    

    


  
    
    Private Sub NavigateRecords()

        txtFirstName.Text = ds.Tables("AddressBook").Rows(inc).Item(1)
        txtSurname.Text = ds.Tables("AddressBook").Rows(inc).Item(2)

    End Sub

    

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        dbsource = "Data Source = C:/AddressBook.mdb"

        con.ConnectionString = dbProvider & dbsource
        con.Open()
        sql = "select * from tblcontacts "
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "AddressBook")





        MaxRows = ds.Tables("AddressBook").Rows.Count
        inc = -1
        btnUpdate.Enabled = False


        con.Close()
    End Sub

    

    
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        btnUpdate.Enabled = True
        If inc <> MaxRows - 1 Then

            inc = inc + 1

            NavigateRecords()

        Else

            MsgBox("No More Rows")

        End If
    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click

        If inc > 0 Then

            inc = inc - 1

            NavigateRecords()
            btnUpdate.Enabled = True
        ElseIf inc = -1 Then

            MsgBox("No Records Yet")
            btnUpdate.Enabled = False
        ElseIf inc = 0 Then

            MsgBox("First Record")
            btnUpdate.Enabled = True
        End If


    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        btnUpdate.Enabled = True
        If inc <> MaxRows - 1 Then

            inc = MaxRows - 1

            NavigateRecords()

        End If
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        btnUpdate.Enabled = True
        If inc <> 0 Then
            inc = 0
            NavigateRecords()
        End If
    End Sub
    

    

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub

    Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click

    End Sub

    Private Sub Label14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label14.Click

    End Sub

    Private Sub ΠΑΤΡΩΝΥΜΟ_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ΠΑΤΡΩΝΥΜΟ.TextChanged

    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

        Dim cb As New OleDb.OleDbCommandBuilder(da)
        ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text
        ds.Tables("AddressBook").Rows(inc).Item(2) = txtSurname.Text

        da.Update(ds, "AddressBook")

        MsgBox("Data updated")



    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
        btnCommit.Enabled = True
        btnAddNew.Enabled = False
        btnUpdate.Enabled = False
        btnDelete.Enabled = False

        txtFirstName.Clear()
        txtSurname.Clear()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        btnCommit.Enabled = False
        btnAddNew.Enabled = True
        btnUpdate.Enabled = True
        btnDelete.Enabled = True

        inc = 0
        NavigateRecords()
    End Sub

    Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
        If inc <> -1 Then

            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Dim dsNewRow As DataRow

            dsNewRow = ds.Tables("AddressBook").NewRow()

            dsNewRow.Item("FirstName") = txtFirstName.Text
            dsNewRow.Item("Surname") = txtSurname.Text

            ds.Tables("AddressBook").Rows.Add(dsNewRow)

            da.Update(ds, "AddressBook")

            MsgBox("New Record added to the Database")

            btnCommit.Enabled = False
            btnAddNew.Enabled = True
            btnUpdate.Enabled = True
            btnDelete.Enabled = True

        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim cb As New OleDb.OleDbCommandBuilder(da)

        ds.Tables("AddressBook").Rows(inc).Delete()
        MaxRows = MaxRows - 1

        inc = 0
        da.Update(ds, "AddressBook")
        NavigateRecords()
    End Sub
End Class




This is my code needed for you to check and have a look.the problem is when i add a record 1:it requires to enter both name and secondname ot it displays an error. 2:if i add a record, the new record will show only after exiting the programme and restarting it.if i update it it will show at once ,after i press the next button ,withunt exiting.
Thanks

This post has been edited by dimdim80: 16 December 2013 - 03:29 AM

Was This Post Helpful? 0
  • +
  • -

#8 x-logan  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 83
  • Joined: 18-April 13

Re: visual basic search button

Posted 16 December 2013 - 03:40 AM

you code is pretty confusing..right now i am busy i will reply you after sometime...
Was This Post Helpful? 0
  • +
  • -

#9 x-logan  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 83
  • Joined: 18-April 13

Re: visual basic search button

Posted 16 December 2013 - 03:49 AM

   NavigateRecords()


add this line inside your insert command like you did inside next button, have a try after that let me know what are you up to
Was This Post Helpful? 0
  • +
  • -

#10 dimdim80  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 69
  • Joined: 28-December 08

Re: visual basic search button

Posted 16 December 2013 - 07:45 AM

View Postx-logan, on 16 December 2013 - 03:49 AM, said:

   NavigateRecords()


add this line inside your insert command like you did inside next button, have a try after that let me know what are you up to

Unfortunately no result..i need to exit and start again in order to be shown..Strange because the record is added in the access database.
Was This Post Helpful? 0
  • +
  • -

#11 dimdim80  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 69
  • Joined: 28-December 08

Re: visual basic search button

Posted 29 December 2013 - 07:05 AM

Solved ...thanks for your help anyway:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1