Helllo
I have a database acess (db) with column ID, Age, Sex, Name
And a windows form1 (vb.net) with textbox as ID and a button "search". A second form2 having textboxes as ID, Age,Sex. Name
When i enter ID no. with search buuton it should retrieve data from db to various textboxes in form2. i dont't know the code.
plese help

Imports System.Data.OleDb

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Change Tablename,Access Full path,Password
        Dim CN As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source='" & "Access Full Path" & "';" & "Persist Security Info=False;" & "Jet OLEDB:Database Password=" & "your pass" & ";")
        Dim cmd As OleDbCommand
        Dim dr As OleDbDataReader
        Try
            CN.Open()
            cmd = New OleDbCommand("SELECT *  from TABLENAME WHERE ID=" & ID.Text & "", CN)
            dr = cmd.ExecuteReader
            If dr.Read Then
                form2.ID.Text = dr("ID")
                form2.Age.Text = dr("Age")
                form2.Sex.Text = dr("Sex")
                form2.Name.Text = dr("Name")
                form2.show()
            Else
                MsgBox("No Such Account")
            End If
        Catch
        End Try
        dr.Close()
        CN.Close()
    End Sub
End Class

Hi
i tried to do as same but a warning msg came that Variable dr is used before it has been assigned a value. A null reference exception could result at runtime.
So please tell me what to do?

Put dr.close in here:

            If dr.Read Then
                form2.ID.Text = dr("ID")
                form2.Age.Text = dr("Age")
                form2.Sex.Text = dr("Sex")
                form2.Name.Text = dr("Name")
                form2.show()
                dr.Close()
            Else
                MsgBox("No Such Account")
            End If
        Catch
        End Try

        CN.Close()

can i retrieve data from two tables of databade access with sigle ID search and display it in one form?

Yes.

How can i add to above code. my another table is reportregister and form this i have to retrieve "Hb"

if your question is solved then mark it as such. for the other question start a new thread.
Click Here

How can i do that. my another table is reportregister and i want fetch Hb data from that. i want to use single button to fetch data name,age,sex from table-accessonbook and Hb from table-reportregister of sigle Id

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.