Link to home
Start Free TrialLog in
Avatar of PC-Alex
PC-Alex

asked on

VB 4.0, Access 7, DAO: Recordset.Edit / Update problem

I have a Sheridan DBGrid in VB 4.0, bound to a Recordset resulting
from a Query to an Access 7.0 Database.

The problem: The Database cannot be updated. I use Edit and Update, but before
Update, the Recordset has the new Value, after Update it has the old one again:

    Do
        ' Data1.Recordset.Updatable yields True here!
        Data1.Recordset.Edit
        ' Data1.Recordset.Updatable yields True here!
       
        Data1.Recordset!SortValue = Data1.Recordset!SortValue + 1
        'This works, SortValue is incremented from 1 to 2 here.
        Data1.Recordset.Update
        ' ... but here, it is 1, the old value, again! (if Update isn't executed
        ' here, it is performed by "MoveNext")

        Data1.Recordset.MoveNext
    Loop While (Not Data1.Recordset.EOF)
   
The Loop shall increment a special sorting field of each Record, starting
with the current one. The Recordset was the result of a Query
(Data1.Recordset = Query.OpenRecordset()), and is Dynaset-Type. It is requeried
after this action, but has the old values.

The Query, saved in the Database, looks like:
PARAMETERS [Key] Long;
SELECT Bauteilschicht.*
FROM Bauteiltyp INNER JOIN Bauteilschicht ON Bauteiltyp.BT_ID = Bauteilschicht.BT_ID
WHERE (((Bauteiltyp.BT_ID)=[Key]))
ORDER BY Bauteilschicht.SortValue;

I also tried wrapping a transaction around the complete loop, with no effect.

Can anyone tell me what might be wrong ?

If I wanted to implement the source within the Access-Database: would it be
faster ? How is it done, as a macro or as a module ?

One field of the data table is connected to the key
(auto value) of the "parent" table, 1 : n, with referential
integrity, and all updating provided.
Avatar of dirkmartin
dirkmartin
Flag of United States of America image

I don't read this much code for 50 points.  I didn't wade through all your code, but rather just to let you know, there is also an UpdateRecord method also.
Avatar of aktis
aktis

This code will work if you create a relationship between the two tables with Update Cascade.
Avatar of PC-Alex

ASKER

Edited text of question
Avatar of PC-Alex

ASKER

Edited text of question
Avatar of PC-Alex

ASKER

aktis:

One field of the data table is connected to the key
(auto value) of the "parent" table, 1 : n, with referential
integrity, and all updating provided.

Sorry, it doesn't work instead.
Avatar of PC-Alex

ASKER

Adjusted points to 100
This is the way I do one:

Dim Qd As QueryDef
Dim Rs As Recordset
Set Qd = dAccounts.QueryDefs("qFindAccountDetails")
Qd.Parameters("MailListID") = mUserMailID&
Set Rs = Qd.OpenRecordset()
Set Data1.Recordset = Rs

The DBGrid has to have the properties set to allow edit and updates (AllowAddNew, AllowAupdate), otherwise I can add new records right on the grid and edit records on the grid or by program control with:

Data1.Recordset.Edit
Data1.Recordset("Credit/Debit") = "C"
Data1.Recordset.Update

I hope this helps
Avatar of PC-Alex

ASKER

mjsmith,

I have the two Allows on True, and use nearly the same code;
it doesn't work instead.

Thank you & sorry.
Avatar of PC-Alex

ASKER

Adjusted points to 150
Avatar of PC-Alex

ASKER

Adjusted points to 200
Are you using the32bit or 16bit VB?
Avatar of PC-Alex

ASKER

32 Bit.
Avatar of PC-Alex

ASKER

Adjusted points to 275
Where do you put the above code?
Avatar of PC-Alex

ASKER

I have a special "insert" event, that is triggered after clicking a button.
I want to clarify some more before I can figure out the cause of the problem.  Can you show me the special "insert" event (function?) first?  Also, is the Sheridan Data Grid has to do with your update process?  Tell me your flow of program, too.
Avatar of PC-Alex

ASKER

ksleung,

I'm on holiday in the moment, I try to tell you as good as I know from here.
The source of this one function is mainly listed above. The problem is, I want to give users the ability to enter a Row "in between" a SSDBGrid, and the order shall be maintained. So I have a field (SortValue) to contain this sorting information. After a user inserts a Row, this SortValue of the Rows that follow must be incremented by 1. That leads to the problem mentioned.
I guess there are some "AddNew", "Update" and "Edit" commands fired automatically from the SSDBGrid ... but even if so, theoretically the code above should work, but it doesn't.
So much for today, I'm on duty on April 29th next time.

I must admit, that I really dont understand what the data-sturcture behind is. But here is my proposal to avoid the problem (its the best method for working when using access :)) ):
Give your rows a primary key, e.g. a counter. Save the sorting-position in a seperate table, where you keep  the primary key and the sorting position. Perhaps the update will work properly on this table. For sorting you could use something like the dlookup() function.
Avatar of PC-Alex

ASKER

Hey BBC,
you did not mean that honestly, did ya? The sorting field is an attribute of the record ... where would you come if you'd always create a new table with all that cross-referencing when you encounter such a problem ? Think of the "theoretical", object - oriented approach ... there must be a "proper" way, I still believe in that.
Avatar of PC-Alex

ASKER

worked on the problem today. Got a new trace: I'm sure now that the cause has to do with the Query, from that the recordset results. As one can see in the definition of the query, the SortValue is used as a sorting criteria, and exactly this field isn't updatable later on. There must be some properties of the recordset or data control or database to be set afterwards, to make the changes in this SortField last even behind the update.
I experimented with the database definition of the SortValue, tried "indexed, double values allowed" and "not indexed", also tried "value must be supplied" and "doesn't have to be supplied", all with no effect.
Just to verify this: Are there double values allowed ? It is not a primary key or something like that ?
Avatar of PC-Alex

ASKER

BBC,
the data type of this OrderValue is Long Integer, and it isn't a primary key.
Are you sure there is no other procedure for lets say "beforeUpdateRecord" that cancels your update somewhere else ?
Avatar of PC-Alex

ASKER

BBC,
there it is:

Private Sub BaustoffGrid_BeforeUpdate(Cancel As Integer)
    Data1.Recordset!BT_ID = DataBauteilbez.Recordset!BT_ID
    Data1.Recordset!OrderValue = BaustoffGrid.Row
End Sub

ASKER CERTIFIED SOLUTION
Avatar of BBC
BBC

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PC-Alex

ASKER

Hey BBC,
perhaps you're right, I solved the problem two weeks ago in another, a little more complicated way, and have no time to roll up the whole thing once again now.
But as you were the one to spend the most time trying to help me I give the points to you with an Average grading, I think thats fair.