Solved got Error in last Row of sub_form (1 Viewer)

FahadTiger

Member
Local time
Today, 17:49
Joined
Jun 20, 2021
Messages
115
Hi Expert
when Im trying to renumber of sub_form for every new invoice ..i used this for control source of unbound textbox
=IIf(IsNull([ItemCode]);Null;Nz(DCount("ItemCode";"SalesOrderDetailsT";"SaleID=" & [SaleID] & "AND SaleOrder <" & [SaleOrder]);0)+1)
its work good..but I Get Error at the last row
1693162872706.png
 

Attachments

  • invoice_v03.accdb
    892 KB · Views: 52
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 15:49
Joined
Sep 21, 2011
Messages
14,306
Check to see if you are on a new record?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:49
Joined
Oct 29, 2018
Messages
21,473
This previous discussion might also apply.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:49
Joined
Feb 28, 2001
Messages
27,187
You are clearly on a new record in the last row of your display in post #1 of this thread. If so, your DCount criteria include doing anything mathematical, either summing or comparing) against a NULL (guessing either [SalesOrderDetails].[SalesID] or [SalesOrderDetails].[SaleOrder]). Doing math against a NULL is bad. You get errors.

So the question is why that last record exists at all, and why those fields (which I'm guessing would be linking fields) aren't autoloaded. But you actually gave us a hint: "i used this for control source of unbound textbox" - and if it is unbound, it has nothing automatically linking it to anything. Your design essentially automatically creates an orphaned record which leads to the nulls being where you don't want them.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:49
Joined
Feb 19, 2002
Messages
43,275
The code is incorrect - it uses DCount() instead of DMax() and it is in the wrong place. Put the following code into the form's BeforeInsert event. This event runs as soon as the user starts typing into the subform.

Me.ItemCode = Nz(DMax("ItemCode";"SalesOrderDetailsT";"SaleID=" & Me.SaleID ;0)+1)

I don't know why SalesOrder is in the criteria at all. The criteria should be ONLY the ID of the parent record.

Using DMax() rather than DCount() allows for potentially deleting rows. So, if you add, 1,2,3 and then delete 2, Your code would attempt to add 3 again rather than 4.

Other thoughts. What is the purpose of the item numbers? Do you want to renumber them to eliminate gaps if you delete one? Using my suggested technique, if you cancel an add, you don't burn an item number as you do with autonumbers. So, if you are adding #4 but do not complete it and end up cancelling it but come back later, then the next item number will still be 4.
 

FahadTiger

Member
Local time
Today, 17:49
Joined
Jun 20, 2021
Messages
115
The code is incorrect - it uses DCount() instead of DMax() and it is in the wrong place. Put the following code into the form's BeforeInsert event. This event runs as soon as the user starts typing into the subform.

Me.ItemCode = Nz(DMax("ItemCode";"SalesOrderDetailsT";"SaleID=" & Me.SaleID ;0)+1)

I don't know why SalesOrder is in the criteria at all. The criteria should be ONLY the ID of the parent record.

Using DMax() rather than DCount() allows for potentially deleting rows. So, if you add, 1,2,3 and then delete 2, Your code would attempt to add 3 again rather than 4.

Other thoughts. What is the purpose of the item numbers? Do you want to renumber them to eliminate gaps if you delete one? Using my suggested technique, if you cancel an add, you don't burn an item number as you do with autonumbers. So, if you are adding #4 but do not complete it and end up cancelling it but come back later, then the next item number will still be 4.
Sir part hartman ... All I want is a new numbering for each sub-invoice starting from the one
 

Eugene-LS

Registered User.
Local time
Today, 17:49
Joined
Dec 7, 2018
Messages
481
I Get Error at the last row
Try this expression:
Code:
=IIf(IsNull([SaleOrder]);Null;Nz(DCount("*";"SalesOrderDetailsT";"SaleID=" & [SaleID] & "AND SaleOrder <" & [SaleOrder]);0)+1)
 

Attachments

  • invoice_v04.zip
    39.8 KB · Views: 54

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:49
Joined
Feb 19, 2002
Messages
43,275
i did...but its not working...thank you
It was my mistake because I used a field equal to zero and not Null
You did NOT try my suggestion since it does not need to include the IsNull() function since the code ONLY runs when it is supposed to. It only runs when you are creating a new record. That is the power of understanding what form events are supposed to be used for.

Since you did not use my suggestion, you probably also stuck with the dangerous DCount() rather than the safe DMax() as well.

For those of you who don't understand the danger of DCount(), I sure hope, you have covered ALL your bases and never, ever, accidentally delete a row. Even if you think you are not allowing a delete, why would you want to find out that there is an error by having an insert fail because a duplicate sequence was generated (unless of course you forgot to also set a multi-field unique index on SaleID + ItemNum by using the indexes dialog:(). Sure makes you look stupid to the user. Think of using DMax() for this purpose rather than DCount() as defensive driving or a safety play if you play bridge. You have a 100% option or an option that might fail. Why would you ever choose the option that might fail?
 
Last edited:

FahadTiger

Member
Local time
Today, 17:49
Joined
Jun 20, 2021
Messages
115
You did NOT try my suggestion since it does not need to include the IsNull() function since the code ONLY runs when it is supposed to. It only runs when you are creating a new record. That is the power of understanding what form events are supposed to be used for.

Since you did not use my suggestion, you probably also stuck with the dangerous DCount() rather than the safe DMax() as well.

For those of you who don't understand the danger of DCount(), I sure hope, you have covered ALL your bases and never, ever, accidentally delete a row. Even if you think you are not allowing a delete, why would you want to find out that there is an error by having an insert fail because a duplicate sequence was generated (unless of course you forgot to also set a multi-field unique index on SaleID + ItemNum by using the indexes dialog:(). Sure makes you look stupid to the user. Think of using DMax() for this purpose rather than DCount() as defensive driving or a safety play if you play bridge. You have a 100% option or an option that might fail. Why would you ever choose the option that might fail?
I think I was stupid in explaining this...
I wanted to count from the one in each invoice .. I was wrong in relying on the itemcod because it bears the number zero and is not null
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:49
Joined
Feb 19, 2002
Messages
43,275
I wanted to count from the one in each invoice
You keep thinking you want to count but that is not at all what you want to do. How hard will it be to just try what I suggested --- exactly ?????????????

It is only ONE line of code in ONE form level event.
 

Users who are viewing this thread

Top Bottom