Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO UPDATE/REFRESH DATAGRID IMMEDIATELY AFTER ADDNEW RECORD 2

Status
Not open for further replies.

Hiccup

Programmer
Jan 15, 2003
266
US
can someone tell me how to accomplish this DataGrid/RecordSet update immediately after adding a new record.

My DataGrid doesn't show the newly added record unless I unload the Form containing the DataGrid and then reload it, then it shows the added record.

My setup is VB6 connected to an Access2K mdb Table via an Adodc. I have a DataGrid with its Datasource. I have an "Add New Record" data-entry Form for adding a new record, but when returning to the main DataGrid Form, the record doesn't show unless I unload the main DataGrid Form and reload it.

I've tried Update, Refresh and moving to a different record (I read somewhere that that action would add the new record to the DataGrid, but it didn't work.)

Here's my code:


Private Sub Command11_Click(Index As Integer)
Unload Accident_Reports_Database
Load Accident_Reports_Database
Accident_Reports_Database.Show
Unload Accident_Report_Add
Adodc1.Recordset.Update
Adodc1.Recordset.MoveFirst
If Adodc1.Recordset.EOF = True Then
Adodc1.Recordset.MoveLast
End If
End Sub

Thanks for any suggestions in advance!

 
Hiccup ,
If I read your click event correctly you are updating the database but not the form the grid is on...I found this in the MSDN, you may want to add code to you click event


Changing Displayed Data at Run Time
Once you have created a grid using the design-time features, you may also wish to dynamically change the data source of the grid at run time. The general methods for accomplishing this are discussed below.

Changing the RecordSource of the DataSource
The most common method of changing displayed data is to alter the query of the DataSource. For example, if the DataGrid control uses an ADO Data control as its DataSource, rewriting the RecordSource and refreshing the ADO Data control will change the data displayed.

' The ADO Data control is connected to the Northwind database's
' Products table. The new query asks for all records which have
' the SupplierID = 12.
Dim strQuery As String
strQuery = "SELECT * FROM Suppliers WHERE SupplierID = 12"
Adodc1.RecordSource = strQuery
Adodc1.Refresh

Changing the DataSource
At run-time you can reset the DataSource property to a different data source. For example, you may have several ADO Data controls, each connected to different databases, or set to different RecordSource properties. Simply reset the DataSource from one ADO Data control to another:

' Reset the DataSource to an ADO Data control that is connected to
' the Pubs database, using the Authors table.
Set DataGrid1.DataSource = adoPubsAuthors
************************************************


To effectively improve in the efficiency of your performance, one must be proficient in the implementation of a positive mental attitude.
 
Thanks for your suggestions zgtrman. I tried adding this to the code, but it's not refreshing the DataGrid:

Set DataGrid1.DataSource = adodc1

Here's my complete code I have for the Command Button's Click Event:

Private Sub Command11_Click(Index As Integer)
Load Contacts_Database
Contacts_Database.Show
Unload Contact_Add
Adodc1.Recordset.Update
Adodc1.Recordset.MoveFirst
Set DataGrid1.DataSource = Adodc1
Adodc1.Recordset.Requery
End Sub

When the Command Button is clicked, it "Loads" and "Shows" the main Form containing the DataGrid and "Unloads" the "Add New Record" Form (i.e., "Contact_Add" Form in the above code.)

The DataGrid is still not showing the newly added record unless I put this at the end of the code:

UnLoad Contacts_Database
Load Contacts_Database

Then this shows the new record in the DataGrid. The problem with these two lines of code is that it causes the screen to flicker due to the unloading/loading actions.

There must be a simple way to get the DataGrid to be updated immediately.

Do you see where I might be making my coding midtake?

Thanks again in advance zgtrman!!
 
Hiccup

Look at the following code snipett:

Dim strQuery As String
strQuery = "SELECT * FROM Suppliers"
Adodc1.RecordSource = strQuery
Adodc1.Refresh

modify this to match the fields of your datagrid or the table from your database and include it in your click event...then it "should" work..

zgtram

To effectively improve in the efficiency of your performance, one must be proficient in the implementation of a positive mental attitude.
 
Thanks zgtrman. I wasn't sure where to place the last code you suggested, but I added it to the Command Button's Click Event. The Command Button Unloads the "Add New Record" Form (last indented line in the code below.) The fifth and sixth indented lines first Load the main Form containing the DataGrid and then Show it.

Unfortunately, where I put your code isn't updating the DAtaGrid without loading and reloading the main Form a second time, then the DataGrid shows the newly added Record.

Here's the code I'm using that isn't working:

Private Sub Command10_Click(Index As Integer)
Dim strQuery As String
strQuery = "SELECT * FROM Contacts"
Adodc1.RecordSource = strQuery
Adodc1.Refresh
Load Search_Contacts_Conflicts_Menu
Search_Contacts_Conflicts_Menu.Show
Unload Contact_Add
End Sub

Also, I get a compile error and the "Adodc1.Refresh" line is highlighted.

Any thoughts as to what I'm doing wrong.

Appreciate your help!
 
Hiccup hi,
adodc1.refresh must work, may be if you put:
call adodc1.refresh
I always use like that, the problem is this: you must call two times to make a good refresh like this:
call adodc1.refresh
call adodc1.refresh
This work good for me.
did some body knows why only if i call 2 times the refresh works?
 
Hiccup,

Is the command button click event on a seperate form?

I did something very similar in my college days however I did not use the ADO control, I did it all through code but the end result is the same...if the click event is on another form that you need to reference the form that the ADO control is on for the refresh to happen.
If you would like to see my program then let me know I will e mail it to you..but like I said...no ADO controls..all code

zgtrman@hotmail.com

To effectively improve in the efficiency of your performance, one must be proficient in the implementation of a positive mental attitude.
 
Thanks zgrtman & xpanic!

From your suggestions, this is the code I ended up with and it updates the DataGrid. Everything is working GREST now!!

Private Sub Command11_Click(Index As Integer)
Dim strQuery As String
strQuery = "SELECT * FROM SortContacts"
Adodc1.RecordSource = strQuery
Adodc1.Recordset.Requery
Load Contacts_Database
Contacts_Database.Show
Unload Contact_Add
End Sub

A star for each of you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top