Calculate the sum of a field in a while loop but after an iteration the variables sum is reset (1 Viewer)

VasiuF

Member
Local time
Today, 19:57
Joined
Apr 1, 2020
Messages
42
Hello everyone

I need some advice

I have to calculate the sum of a field in a while loop but after an iteration the variables credit1,credit2,sum1 and sum2 are reset (take the value 0).

I don't know where I'm wrong.

Can you help me ?






ok, leave it at that, it was a simple question ...
 
Last edited:

June7

AWF VIP
Local time
Today, 08:57
Joined
Mar 9, 2014
Messages
5,490
Why must you use a looping structure as opposed to an aggregate query?

Why is there no procedure declaration line nor End Sub or End Function line in posted code?

Have you step debugged?

Those variables don't need to be declared in header, just within the procedure.

Only Variant can hold Null so use of Nz() on explicitly declared variables is meaningless. Integer variables default to 0 when initialized.

You have declared credit2 yet code uses credit22: credit22 = Nz(cr2, 0)

Should have Option Explicit in module header to catch spelling errors like that. All variables should be declared. You have some that are not.
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 17:57
Joined
Jul 9, 2003
Messages
16,305
I was going to say that I don't see a wend to finish your do while statement.

but then I thought, hold on a minute, I don't recall ever seeing a recordset loop with do while in it.

Now I'm not saying it can't be done with a do while statement I'm saying I've never seen it done with do while statement so I may be getting an education here!

It's normally done in this fashion:-

Do Until rs.EOF = True

.
.
.

Loop

See DAO example here:-

 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 17:57
Joined
Jul 9, 2003
Messages
16,305
Well I just looked, there are numerous examples using "While" and I can see at the end of your statement you have:-

rscalcmed.MoveNext

Loop

Which I don't recall seeing on my mobile?

So I think "wrong tree" and "barking up" sum up my comment!
 

HalloweenWeed

Member
Local time
Today, 12:57
Joined
Apr 8, 2020
Messages
213
Have you declared "Option Explicit" at the beginning of your module?
I see you have declared several variables as 'Public.' I suppose you are aware of the dangers of using 'Public' variables?
Now to that subject, I see no declaration for the variables credit1 and credit2. Are they Textbox Controls (try putting them in brackets: [credit1])? Where do they reside? And is this place tied somehow to your module?
 
Last edited:

plog

Banishment Pending
Local time
Today, 11:57
Joined
May 11, 2011
Messages
11,662
Why? Why all this? What's the big picture of what you are trying to accomplish? Looking at it, it all just seems convoluted and incorrect.

1. You are deleting a table (calcmed) then building it again. What does that table do for you that just building a query or just looping through data can't help you with?

2. I don't think your "static" tables are properly structured:

Code:
...
      & " FROM materii INNER JOIN [note] ON materii.codm = note.matern  " _
      & " WHERE  Mid(Str(matrn), 5, 2)  =  Mid((Str(anadmm)), 3, 2) " _
      & " AND (note.ns1e <> 0 OR note.ns1v <>0 OR note.ns2e <> 0 OR note.ns2v <>0 OR note.np1 <>0 OR note.np2 <>0) " _
...

2a. You shouldn't extract data from data in fields. If matrn and anadmm contain a 2 character code inside them, then that 2 digit code should be stored seperately from the other data in matrn and anadmmm.

2b. You shouldn't numerate, prefix nor suffix field names. All the data in those note fields (e.g. note.ns1e, notens1v, note.ns2e, etc.) should go into another table with fields to store the number, prefix and suffix data now encoded in the field names.

3. Comment your code. A lot of the answers to my questions might be helped with commenting your code. When you have that much code you really need to document the general thinking and then the main components so that people who come later and try and work with this (including yourself) know what you are thinking.

Again, what's all this for? It seems you are coding around prior errors and building something that could be implemented in a simpler manner.
 

VasiuF

Member
Local time
Today, 19:57
Joined
Apr 1, 2020
Messages
42
Have you declared "Option Explicit" at the beginning of your module?
I see you have declared several variables as 'Public.' I suppose you are aware of the dangers of using 'Public' variables?
Now to that subject, I see no declaration for the variables credit1 and credit2. Are they Textbox Controls? Where do they reside? And is this place tied somehow to your module?
 
Last edited:

HalloweenWeed

Member
Local time
Today, 12:57
Joined
Apr 8, 2020
Messages
213
I see two recordsets: rs_calcmed and rscalcmed.
I don't see where rs_calcmed comes from (no recordset open, no declaration of Access object type - table or otherwise). Could this be a typo?
 

VasiuF

Member
Local time
Today, 19:57
Joined
Apr 1, 2020
Messages
42
Why? Why all this? What's the big picture of what you are trying to accomplish? Looking at it, it all just seems convoluted and incorrect.

1. You are deleting a table (calcmed) then building it again. What does that table do for you that just building a query or just looping through data can't help you with?

2. I don't think your "static" tables are properly structured:

Code:
...
      & " FROM materii INNER JOIN [note] ON materii.codm = note.matern  " _
      & " WHERE  Mid(Str(matrn), 5, 2)  =  Mid((Str(anadmm)), 3, 2) " _
      & " AND (note.ns1e <> 0 OR note.ns1v <>0 OR note.ns2e <> 0 OR note.ns2v <>0 OR note.np1 <>0 OR note.np2 <>0) " _
...

2a. You shouldn't extract data from data in fields. If matrn and anadmm contain a 2 character code inside them, then that 2 digit code should be stored seperately from the other data in matrn and anadmmm.

2b. You shouldn't numerate, prefix nor suffix field names. All the data in those note fields (e.g. note.ns1e, notens1v, note.ns2e, etc.) should go into another table with fields to store the number, prefix and suffix data now encoded in the field names.

3. Comment your code. A lot of the answers to my questions might be helped with commenting your code. When you have that much code you really need to document the general thinking and then the main components so that people who come later and try and work with this (including yourself) know what you are thinking.

Again, what's all this for? It seems you are coding around prior errors and building something that could be implemented in a simpler manner.



330/5000
 
Last edited:

Isaac

Lifelong Learner
Local time
Today, 09:57
Joined
Mar 14, 2017
Messages
8,841
What @June7 said. Debug the code and step through it line by line -- you will come to your solution doing that a lot faster than we will trying to digest all of that code in the post.
 

plog

Banishment Pending
Local time
Today, 11:57
Joined
May 11, 2011
Messages
11,662
It's a master's class of passive aggressiveness. And I thought I was good.
 

Users who are viewing this thread

Top Bottom