Solved VBA Record Editing issues (1 Viewer)

NearImpossible

Registered User.
Local time
Today, 08:20
Joined
Jul 12, 2019
Messages
225
There are 8 records in the table and then an unbound field I am using for the Servings value, but for some reason, its only using the quantity value of the last record entered for the quantity value of all the other records, or if I select a row in the table and then click the update button, then it will use that value for all the records vs using each records value.

db attached

Code:
Set rs = CurrentDb.OpenRecordset("Select Quantity from [RecipesBuild]", dbOpenDynaset, dbSeeChanges)

    While Not rs.EOF
        With rs
            .Edit
            .Fields("Quantity") = Quantity / Servings
            .Update
        End With
        rs.MoveNext
    Wend

Any help is appreciated

thank you
Kevin
 

Attachments

  • Recipies.accdb
    640 KB · Views: 78

Gasman

Enthusiastic Amateur
Local time
Today, 14:20
Joined
Sep 21, 2011
Messages
14,306
So qualify the quantity with the Fields declaration again?
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:20
Joined
Sep 21, 2011
Messages
14,306
not sure what you mean
Code:
.Fields("Quantity") = .Fields("Quantity")  / Me.Servings

I always add the Me to ensure I use the form control.

Not sure your code is actually advisable anyway?
Wouldn't a new field be set to Quantity/Servings?

So if I have 60 sausages and there is 2 to a serving. The 'heads' would be 60 /2 ?
 

GPGeorge

Grover Park George
Local time
Today, 06:20
Joined
Nov 25, 2004
Messages
1,869
The code does NOT select records from the subforms recordsource to provide the different quantities. It's stays on the first record in the subform's recordsource. You need to add a second step to get the quantities from the different records. I'm puzzled by this whole thing anyway.

It looks like you are trying to take a quantity from the RecipeBuild table and multiple it by the servings, and then update that same quantity. All that does is change each RecipeBuild, based on what you used the previous time you used it. In other words, if you start with 8 tbs of Olive Oil and multipl that by 5 servings, the quantity is now 40. But if you run it again, that 40 will turn into 40 x 5, or 200 tbs and so on. I doubt that's the goal is it?
 

GPGeorge

Grover Park George
Local time
Today, 06:20
Joined
Nov 25, 2004
Messages
1,869
The code does NOT select records from the subforms recordsource to provide the different quantities. It's stays on the first record in the subform's recordsource. You need to add a second step to get the quantities from the different records. I'm puzzled by this whole thing anyway.

It looks like you are trying to take a quantity from the RecipeBuild table and multiple it by the servings, and then update that same quantity. All that does is change each RecipeBuild, based on what you used the previous time you used it. In other words, if you start with 8 tbs of Olive Oil and multipl that by 5 servings, the quantity is now 40. But if you run it again, that 40 will turn into 40 x 5, or 200 tbs and so on. I doubt that's the goal is it?
Actually, I got that backwards, you're dividing, but the result is the same thing. Each time you execute the update, you make the quantity smaller and smaller and smaller.
 

GPGeorge

Grover Park George
Local time
Today, 06:20
Joined
Nov 25, 2004
Messages
1,869
Ah, the light might be flickering a bit. You need a "Standard" recipe table with base quantities for each ingredient. Than each time you build a new recipe from that you would use the base quantity, not the unique recipebuild quantity, would you not?
 

NearImpossible

Registered User.
Local time
Today, 08:20
Joined
Jul 12, 2019
Messages
225
Code:
.Fields("Quantity") = .Fields("Quantity")  / Me.Servings

I always add the Me to ensure I use the form control.

Not sure your code is actually advisable anyway?
Wouldn't a new field be set to Quantity/Servings?

So if I have 60 sausages and there is 2 to a serving. The 'heads' would be 60 /2 ?
Code worked perfect, thanks !!

in seeing the code, I see why I would have to list it that way now.

Just a FYI, the quantity listed is the total per servings listed, i'm updating the quantity to be reflective of 1 serving, i guess I could create another field to hold that value, but after the conversion, i don't need the original values as everything else will be calculated by the single serving amounts.

thank you again !!
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:20
Joined
Sep 21, 2011
Messages
14,306
Well after sleeping on it I realised that if it was a calculation, then it should not be stored anyway. :)
 

Users who are viewing this thread

Top Bottom