Im currently working now into a monitoring system


Adodc2.Recordset.AddNew
Adodc2.Recordset!employeeNumber = txtNumber.Text
Adodc2.Recordset.update


now i've noticed that after i input a value and save it. it goes to the bottom of the datagrid, how can i add value in the top of the datagrid? Thanks for the reply in advance

Dani AI

Generated

Short answer: a table has no guaranteed physical order, so new rows cannot be forced "on top" in the file. As noted, the right approach is to control the display order. The simplest, reliable fix is to add a sortable field (timestamp or autonumber) and present rows ordered by that field.

Add a Date/Time column (for example CreatedOn) or use the autonumber primary key. In Access you can set the Date/Time field's Default Value to Now() so each new row gets a timestamp automatically. Then return rows ordered newest-first. Example SQL (select explicit fields rather than *):

sql = "SELECT EmployeeNumber, EmployeeName, Position, CreatedOn FROM employee ORDER BY CreatedOn DESC"
Adodc1.RecordSource = sql
Adodc1.Refresh

If you prefer to sort the already-open recordset, use the recordset sort and then refresh the grid. After refreshing you can set the grid to show the top row so the new record is visible:

Adodc1.Recordset.Sort = "CreatedOn DESC"
Adodc1.Refresh
DataGrid1.Row = 0   'focus the first row

Troubleshooting notes: make sure every row has a valid timestamp (nulls break the sort); index the field if the table grows; call Refresh/Requery after inserting so the UI picks up the change. — since you decided on ORDER BY, use a timestamp or autonumber for stable, predictable ordering; that will solve the display problem without trying to depend on physical storage order.

Recommended Answers

All 6 Replies

How are you populating the data grid ?

i just keep informations like
employee number
employee name
and employee position using this code

im using adodc and datagrid

Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\database.mdb;Persist Security Info=False"
Adodc1.RecordSource = "Select * from employee"
Set DataGrid1.DataSource = Adodc1
Adodc1.Refresh

the above code is placed in form load

and the code below is place in a command button

Adodc2.Recordset.AddNew
Adodc2.Recordset!employeeNumber = txtNumber.Text
Adodc2.Recordset.update

but the latest record that i've input using this code goes to the bottom of the datagrid/ database.mdb, i need it ontop of the datagrid

Do you have any field in your table that stores transaction time ?

If yes, sort by descending order on that field.

I'd already tired that, it also works, but I prefer for the adding in top of the datagrid/ database if there is any code that can do that, thanks for you help Sir, really appreciate it :)

When data is stored in a database, physical position is not at all important and the user has hardly any control over the.
No code can do that for you.
You can only display that at the top.

okay sir, thanks :)
i've decided to use the order by method in SQL coding

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.