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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RecordsetDTC AddRecord not working 2

Status
Not open for further replies.

rcarr

Programmer
Oct 19, 2000
12
0
0
CA
The following code is overwriting an existing record:

Sub btnNew_onclick()
rstExtensions.addRecord
End Sub

If I have selected a previous record in the recordset, then click the btnNew button, the AbsolutePosition value still points to the former existing record. When finished filling in DTC textboxes, I click on a button to call rstExtensions.update, which overwrites the previous record, instead of writing to a new record. The AbsolutePosition still points to the former record. rstExtensions is a static, client-side cursor with optimistic locking. Any ideas?


Randy Carr
randy.carr@carteretcraven.ncemcs.com
 
Do you have a grid on the same page? The Display routine for a grid will set the current row (grid._markPos), which occurs after the onclick event.

You may need to create a separate one-record-only display page, and navigate to this page via a page method (create a Page DTC object and register a navigation method, who's job is to perform the AddRecord).

Merlin
 
I´m experiencing the same problem when I try to add a new record through rds.addnewrecord() methode.

It sometimes runs and sometimes not. I don´t use any grids.

I´m confused.

Iogha
 
Merlin said:

>Do you have a grid on the same page? The Display routine >for a grid will set the current row (grid._markPos), which >occurs after the onclick event.

>You may need to create a separate one-record-only display >page, and navigate to this page via a page method (create >a Page DTC object and register a navigation method, who's >job is to perform the AddRecord).

I´ll be very pleased if you could tell me more details about it.

Where do I write the method UpdateRecord... etc.. the code-structure and calls to methods...

Thanks a lot!
 
A recordset object has a 'bug' in the Javascript library code (_ScriptLibrary/RECORDSET.ASP) in that it 'forgets' the state of the 'newRecord' flag between server trips.
Adjust the restoreState method as follows...

function _RS__restoreState()
{
var r = false;
this._fireEvent(this._funcInit);
if (this.maintainState)
{ // attempt to restore previous state
var state = thisPage.unpersistState(this.name);
if (state != null)
{ // restore previous state
if (state._addNew != null) {
this._bAddNewImmediate = true; //???original code??
this._bAddNew = true; // <<<ADD THIS LINE
}
 
I´ve wrote it where you´ve told me.

But after doing it, each time when I execute .updateRecord, it creates me a new entry in the Datenbank instead of updating the existing one.

I´m getting crazy... Do you know another way of patching it?

Thanks again MerlinB!
 
Yes, there is definately a mix between the bAddNew flag and the bAddNewImmediate flag, which seems to throw things a bit. I am not convinced that the current system of flags works correctly - and my 'FIX' does seem to upset this delicate balance. A new 'FIX' is shown below, but first..

------- About the 'bAddNew...' flags-------
Basically, the AddNew status should be reset whenever an updateRecord, cancelUpdate or anything that refreshes the recordset occurs. I note that the current code will set the bAddNewImmediate flag to false in most of these strategic places - and I am tempted to believe that the logic of these flags is as follows:

If you perform an AddRecord, it clears the 'field' collection and sets the bAddNew flag. When you try to 'get' a field value, it will now return nothing (due to the this.fields._newRecord = true;). If you perform a server round-trip, then most of these flags are NOT reset.
At this point the recordset is actually still positioned on an existing row - it is only the bAddNew flag that is set.

If (as is usual) the user fills in the fields and clicks a button - causing a server round trip - then the following happens during page initialisation on the server:
establish the 'bAddNewImmediate' status given the bAddNew status.
Re-construct the recordset (but still no 'new record' - its still just a flag)

(If ANOTHER server round trip occurs, then the 'bAddNewImmediate' flag becomes false - and your NewRecord status is now lost forever! So if you have a click event on a drop-down list (for example) that causes a server round trip to gather new info - then the Add status be lost)

When an updateRecord is executed now, it detects the 'bAddNewImmediate' flag (NOT the bAddNew) and creates the 'new row' (this._rsADO.AddNew();) before feeding in the values of the text boxes etc..

Therefore, the updateRecord MUST occur AFTER a SINGLE server round trip for the thing to work!!!

Without the 'Fix' the bAddNewImmediate flag seems to get reset easily - but WITH the Fix there isn't anything to turn OFF the New state (as you noticed... - though you could manually set the _bAddNew flag to false after an updateRecord if you wish).

-------------- NEW IMPROVED FIX!!---------------
So UNDO the earlier 'fix' and try this instead...

function _RS__preserveState()
{
if (this.maintainState)
{ // preserve state in hidden field
var bState = false;
var state = new Object;
if (this._bAddNew || this._bAddNewImmediate)
{
bState = true;
state._addNew = true;
} ...etc...

This will preserve the 'New' state on round-trips UNTIL an update or cancel resets it.

ALSO

function _RS_addRecord()
{
//bFir...
if (this._allowUpdate && this.isOpen() && !this._bFiringOnBeforeUpdate)
{
this._bAddNewImmediate = false;
@if (@trace_events)
thisPage._traceEvent(this.name,RS_ONROWEXIT);
@end
this._objEventManager.fireEvent(RS_ONROWEXIT);
// set flag to indicate new record for subsequent update
this._bAddNew = true;
this._bAddNewImmediate = true; //ADD THIS LINE

I am also sure that it would be a good idea to set the fields._newRecord flag following the OPEN in the _restoreState method, or within the _open method. And also, the AddNew flags really should be turned off if you requery or change the recordset, which it currently does not.. Still, see what happens first!

If this works then post a note! (I don't have time to test this at the moment.)

If it does, then then bAddNew flag could be removed all together as it now serves no useful purpose.

Good Luck!
 
I have tried your new improved fix but the record is still overwritten. I see this is an old post but any ideas on how to get this to work? I tried the setBookMark and getBookMark methods but I get an &quot;undefined&quot; error message. I assume it is because while adding a new record there isn't really anything to bookmark? Any help is greatly appreciated.
 
You could try the addImmediate command instead:
rsRS.addImmediate array(&quot;col1&quot;, &quot;col2&quot;), _
array(txtVal1.value, txtVal2.value)

But sometimes you cannot add items via a recordset - particularly if the recordset contains multi-table joins. In this case you may want to add a named INSERT command to a DataEnvironment connection (found under the Global.asa file). Assume that you add a command called
cmdInsertDingbat which has the SQL text:
INSERT INTO DINGBAT (col1, col2) VALUES (?, ?)

To use this in your page do:
thisPage.createDE
iResCode = DE.cmdInsertDingbat (txtVal1.value, _
txtVal2.value)

its as simple as that.

Of course you could create a connection and a command object and string together the INSERT clause etc. But the above technique is simple (though a bit mysterious, I agree).

In both cases, you will need to REQUERY the original recordset in order to see the new row - and you may want to read through the recordset in order to position to the new row. Or you can add a 'find' method to the Recordset.ASP code. (Content Management)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top