Navigate Database

tqmd1

Well-known member
Joined
Dec 5, 2009
Messages
60
Programming Experience
Beginner
Dear Developers

I have following codes on lostfocus of textbox1.text to search values.
It works fine but how to navigate database. I mean what codes should I use for Next Record and Previous Record Buttons.
My SQL database name is accounts and table name is employees

Please help

str = "select * from employees where sno =" & Val(Me.TextBox1.Text)
dt = GetTable(str)

If (dt.Rows.Count >= 1) Then
Me.TextBox2.Text = dt.Rows(0)("name")
Me.TextBox3.Text = dt.Rows(0)("city")
Me.TextBox4.Text = dt.Rows(0)("phone")
Else
Me.TextBox2.Text = ""
Me.TextBox3.Text = ""
Me.TextBox4.Text = ""
End If

'this function is in main module
Public Function GetTable(ByVal mstr As String)
cmd = New SqlClient.SqlCommand(mstr, con)
da = New SqlClient.SqlDataAdapter(cmd)
dt = New DataTable
da.Fill(dt)
Return (dt)
End Function
 
{Be hAppy}

dim n as integer=0

While Next button clicked
n+=1
if n=dt.Rows.Count then
n-=1
end if
ShowRecord(n)

While Previous button clicked
n-=1
if n=-1
n+=1
end if
ShowRecord(n)

Private sub ShowRecord(byval n)

Me.TextBox2.Text = dt.Rows(n)("name")
Me.TextBox3.Text = dt.Rows(n)("city")
Me.TextBox4.Text = dt.Rows(n)("phone")


end sub
 
Dear Sir,

Thanks for helping but your codes do not work
Suppose I have only THREE records in Table, I enter 2 in textbox1 then I press Back Button then does not show first record.

Where I am making wrong?

Please help again
 
Are you looking for navigation?

Are you sure you looking for "navigation"?

Navigation (in this case) means to scroll through the list ... backwards and forwards.

If, however, you'd like to enter an number and then have it pull up that information and display it, this is not navigation. This would be handled by a query.

For example... lets say the employee has a unique ID #.

then, you'd have a textbox where the user would enter the ID#. When he presses the button (or hits enter), a query would be sent to the database...

like.... SELECT lastname, firstname, jobposition, title FROM employees WHERE (employeeID = ?)

Then.... when you call this in code, it would be something like

Me.DataTableAdapter.FillBy(DataSet, employeeID:= textbox.Text)
 
Back
Top