Help with code

I have a table like this

7357.tablelikethis.JPG

then created a form and added the button code:


pSum:=0;
WITH rs1 DO BEGIN
SETRANGE(guncat_Name);

IF FIND('-') THEN BEGIN
pGunCatName:=guncat_Name;
//pSum:=gun_Price;
REPEAT

IF pGunCatName = guncat_Name THEN
pSum:=pSum+gun_Price;

UNTIL NEXT=0;
END;

rsRes.INIT;
rsRes.ID:=getID;
rsRes.Group:=pGunCatName;
rsRes.Sum:=pSum;
rsRes.INSERT;

END; //with rs1 end

After you click should be:

ID Group Sum

1 Pistol Sum1

2 Auto Sum2

3 Heavy Sum3

but i got this

2364.table2defthis.JPG

how to show the other categories? = (

You want this in form only?

why dont you try in report?

He sums up the values ​​from the first table and displays them in another (img 2)

I tried different ways, but does not work :redeyes:

Create a FlowField in your second table to sum the entries from the first table with a matching type. If you don’t know how to create a FlowField there are numerous examples on the forums, Microsoft documentation, and online help.

I think it would be easier :slight_smile: But my “teacher” needs to do it in code, but I don t know how to do unfortunately. He gave me the algorithm, I don t know how to implement it

it looks like this:

If the IF pGunCatName = guncat_Name THEN we write the sum
If NO then
1) submit the result to another table (3rd table)
2) Resets the pSum
3) assign pGunCatName:=new value guncat_Name

If I were you I would go back to this teacher and have them explain exactly what they want you to do. This makes absolutely no sense at all to me, and so more clarification is in order. Also, you need to be a better student by asking questions when things are not clear to you.

hi,

First of all the whole idea looks stupid.

K anyway lets give a try.

setcurrentkey(gun_type,gun_name);

if findset then

repeat

if Type1<>Rec1_gun_type then

Rec2.setrange(Rec2.gun_type,Rec1.gun_type);

if Rec2.findset then

repeat

Sum+= Rec2.gun_price;

until Rec2.next=0;

table2.setrange(group,Rec2.gun_name);

if not Table2. find set then

Table2.Group:= Rec2.gun_name;

Table2.Sum:=Sum;

until next=0;

Type1:=Rec1_gun_type;

Consider the logic only this will help you to sort the recording according to the gun type then it will not go to the next rec if the gun type is same.

you need to be careful whr you need to insert the Type1 variable, which will help you to avoid same type.

Use clear variable function also.

I hope the rest you can manage

hmm … there may be another way?
If you did, how would you have done this task (in code)

Hi,

I think another way is there. In the first table define the cost as sumindex field with appropriate keys here i think gun_type will be fine.

then inset the record in the second table and call the sum-index field with proper filter this will minimize the code you have to write.

OK, I made ​​a little differently

f1:=0;
WITH rs1 DO BEGIN
SETRANGE(guntype_ID,'W-1');
IF FIND('-') THEN REPEAT
f1:= f1 + gun_Price;
UNTIL NEXT=0;

rsRes.INIT;
rsRes.ID:=getID; //function
rsRes.Group:=guncat_Name;
rsRes.Sum:=gunSum;
rsRes.INSERT;
END;

I made ​​these as much as I have a group…but it works! :smiley:
but that if such groups would be 100?

may be possible to somehow make this code more universal? :slight_smile:

If you’re not doing the FlowField, which you should, think about it logically. What do you want to do?

Loop through table 1 and sum the price for every id. That means you need to sort your record set by the ID using the correct key. When you come to a new ID you want to insert a new record.

In your first example you are only inserting a record after you’ve processed every item from table 1.

I would highly recommend going through the NAV development training material.

Duke before going any further, you need to take a step back and realize that you are working in Navision, not C## or VB or Access. If you want to learn Navision and grow with it, you need to do things the Navision way. This should have been the first thing your teacher taught you, and clearly they haven’t done that. The way you are writing code, is like taking the German language, using a dictionary to replace all the German words with English words and then expecting someone to be able to read the sentence.

Get your hands on the Navision style guide (your teacher should have a copy, but sadly maybe not). Create all the field names and variable names according to the Navision rules. Learn about flow fields, by playing with standard Navision, take a look at tables 27 and 32 and 5802 and 37 and see how they all link together. Take a look at code (CodeUnits 211, 212 and 213 are a good place to start and take a look at tables 156 and 203)

Change your table name to Gun, and the fields to:

Code

Type

Category Name

Name

Price

That will make you code more readable.

If you don’t do it the Navision way, you will struggle and it will be very difficult for you to learn from standard Navision.