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!

Datagrid - Toolbar - Adding new records 1

Status
Not open for further replies.

Knutjo

Programmer
Jan 15, 2002
31
NO
I have posted this question here before, but I haven't got the right answer yet. I'm sure someone must have done this before....

I have a form with a DataGrid connected to a Access 2000 database with an ADO-object. In the same form I have a toolbar with som buttons. One button is ment for "adding a new record" into the datagrid.

When I push the button in the toolbar I want to set focus on the last line in the datagrid where you normally register new reords.

I have tried to set the col,row properties, .Addnew etc, but with no luck.

Can anyone tell me how that VB code will look like ?


Regards
Knut
 
Set the grid's property "AllowAddNew" to True

Then,

DataGrid1.SetFocus

And either:
rsADO.AddNew

or

Adodc.Recordset.AddNew

depending if you are using a recordset object variable or a data control [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Thanks CCLINT !

....but this gives me the same result as before.
One line is inserted before the "*"-line where new records are supposed to entered.
When doing it this way you don't get events like "OnAddNew".


Regards
Knutjo
 

rsADO.AddNew
DataGrid1.AllowAddNew = dbgNoAddNew

Now the user has to enter something.

If the user decides to cancel adding a new row, then the Form's KeyDown event you will need to capture the ESC key (plenty of examples in this forum) and add the following:

If rsADO.EditMode = adEditAdd
rsADO.CancelBatch
DataGrid1.AllowAddNew=True
End If [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Thank you CCLINT, but it still doesn't work.

I have just tried to implement what you wrote.
The last line in the "DataGrid", which is ment for inserting new records, disappears with your example code.
The only way to get the DataGrid to trigger OnAddNew event is to insert records on this line. My "OnAddNew"-code is not triggered when I'm using code like you described.



 
Think about it.

The OnAddNew is triggered when the User clicks on the AddNew(*) line.

So, based on what you asked, I gave you working code.

You actually do not need this event, because your code is already acting as an event. You could put the code you have in the OnAddNew either in the same proceedure where you called the recordset AddNew method, or in a seperate proceedure which is called after that method as well as called from the OnAddNew event, or just call the event yourself after calling the rs AddNew method:

DataGrid1_OnAddNew
rsADO.AddNew
DataGrid1.AllowAddNew = dbgNoAddNew

>The last line in the "DataGrid", which is ment for inserting new records, disappears with your example code...

That's right! Because it is supposed to!
The AddNew(*) line is then hidden to act as it would in ACCESS, but shows up again after the user cancels the AddNew mode by pressing ESC, or actually adds the new record, In which case you you need to add:

DataGrid1.AllowAddNew=True
In the grid's AfterInsert event.

Come on! You are going to have to spend some time trying to understand this all, and play around and look at what is happening, as well as the tools (properties, methods and events) available to you. It is not that difficult at all.
[/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Another, simpler, way would be to do the following:

DataGrid1.Row = rsADO.RecordCount
DataGrid1.Row = DataGrid1.Row + 1

However, this is not the same way as done in ACCESS for certain reasons. [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 

You may need the last line also

DataGrid1.Row = rsADO.RecordCount
DataGrid1.Row = DataGrid1.Row + 1
DataGrid1.SetFocus [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Thank you so much for your help CCLINT.

Have been hooked up in some other things so I haven't been able test all your tips until now.

You gave me the idea of turning off AllowAddNew in the datagrid and programmatically control everything. That seems to be the soulution for me. I have to do a lot of coding, but it will work in the end.

.... continuing with other problems....

Thanks again !

 

You're welcome! I'm glad it worked for you! [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top