Solved Sales Order Form Coding

accessonly11

Member
Local time
Today, 19:38
Joined
Aug 20, 2022
Messages
91
dear access members,
please check where is trouble in the coding. need assistent to make
1662096930038.png

here is view of my sales order form,
there is some issues with coding behind it.
i am trying to make it like northwind form, (there is some difference)

i need, when ProductID combobox get focus, dorpdown list show,
and then user type product in productid combobox, afterupdate event, it check
if the product have quantity in stock, otherwise show msgbox asking for to add this product in purchase order
and make it is sure it is not a double (product) entry
then go to next field of quantity.

in Quantity text after update event, it check
quantity is not 0, and not greather then available in stock.

my coding is as under, but is not working as requirment
Private Sub ProductID_BeforeUpdate(Cancel As Integer) lngProduct = Nz(Me!ProductID, 0) If lngProduct = 0 Then Cancel = True MsgBox "Please select a product from the list" Me.ProductID.SetFocus Exit Sub End If With Me.RecordsetClone .FindFirst "ProductID = " & lngProduct If Not .NoMatch Then Cancel = True MsgBox "This product is already entered!" Me.Undo Me.TimerInterval = 100 End If End With End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer) If DCount("*", "OrderDetails", "ProductID = " & Me.ProductID & " AND OrderID = " & Me.OrderID) > 0 Then MsgBox "This Product already entered" Cancel = True Me.ProductID.SetFocus Exit Sub End If 'Me.ProductID.DefaultValue = 1 End Sub

Private Sub ProductID_AfterUpdate() With Me.ProductID If Not IsNull(.Value) Then Me.RetailPrice = .Column(3) Me.SalePrice = .[Column](4) 'Me.Qty = .[Column](2) End If End With ProductID.Requery With Me.RecordsetClone If ProductID.Column(2) <= 0 Then Cancel = True MsgBox " This Product is Not Available, Do you want to add in Purchase Order", vbInformation, "Not In Stock" Me.Undo End If End With End Sub

Private Sub Qty_AfterUpdate() If Qty.Value > ProductID.Column(2) Then MsgBox "There is only " & ProductID.Column(2) & " available", vbInformation, "Stock" Qty.Value = ProductID.Column(2) End If End Sub Private Sub Qty_LostFocus() If Qty.Value = 0 Then MsgBox "There is only " & ProductID.Column(2) & " available", vbInformation, "Stock" End If Qty.SetFocus End Sub
 
currently facing these problems,
firstly, in product combo, i enter product baydal which have 100 in quantity, but after entering more then quantity, it is not showing msgbox "MsgBox "There is only " & ProductID.Column(2) & " available", vbInformation, "Stock".
despite this, the msgbox which is related to check double product entry showing, while i have not double entry.
thirdly, default value of productid is disabled, but still it is showing value " panadol"


1662098178671.png
 
default value of productid is disabled, but still it is showing value " panadol"
Can you post a copy of your application with just the form (with subforms) and required tables/queries.
Just a few (fictitious) records to understand.
 
Walk through your code with F8. It dos not help when messages are exactly the same unless you do that.
Dim your variables. lngProduct is not even dimmed? :(

You must have Option Explicit at the top of every module. VBA window/Toops/Options and Reuire variable declarattion'
That will only now work for new modules. You have to fix existing ones yourself.
 
If you are Saving the balance to a field on a table (inventory table), then you can check that field.
otherwise, you can compute the balance of an item in a Query and summing all Receipt and deducting
the sum of all your Invoice.
 
Checking the value in the AfterUpdate or the lostFocus event event is like closing the barn door after the horses have escaped.

You need to do your validation in the BeforeUpdate event (preferably, the form's BeforeUpdate event) so you can cancel the event and prevent Access from saving a record that shouldn't be saved.
 
Checking the value in the AfterUpdate or the lostFocus event event is like closing the barn door after the horses have escaped.

You need to do your validation in the BeforeUpdate event (preferably, the form's BeforeUpdate event) so you can cancel the event and prevent Access from saving a record that shouldn't be saved.
understand
 
i think you already have that "feature"?
sales.png
 
after insertion of available product, if user put value 0 or just leave it unchanged unconciously, there is no check on this step,
like as under,
gramex
septran
1662203953515.png
 
add code to the Form's BeforeUpdate event.
set Cancel = True if qty <= 0
 
i did not have, do i put code under command button click event
I guess you didn't understand my "after the horses have left the barn" analogy. If you don't want to save bad data, you need to do the validation in the form's BeforeUpdate event (or rarely, in the control's BeforeUpdate event)

Here's a video you might want to watch. You don't have to join. You can just play it.

 
add code to the Form's BeforeUpdate event.
set Cancel = True if qty <= 0
1662465856426.png

its working now, me.qty.value was not responding, but when i change it to me.qty.text=0 now working
1662465911022.png

in form beforeupdate event
 
I guess you didn't understand my "after the horses have left the barn" analogy. If you don't want to save bad data, you need to do the validation in the form's BeforeUpdate event (or rarely, in the control's BeforeUpdate event)

Here's a video you might want to watch. You don't have to join. You can just play it.

thanks for your guidance, i understand that validation of data is not good in afterupdate events of form or control.
 
Use Me.Fieldname to reference controls since .Value is the default property. There is no need to include the .Value peoperty and the .Text property has a special use. It is used when the control has the focus. Each control has THREE buffers ---

Me.FieldName.OldValue = The value loaded into the control in the current event. It is either Null for a new record or it is the value that is currently stored in the table which might be null if null is valid for that field.

Me.FieldName.Text = the buffer into which data is typed. In earlier versions of Access, the .Text property was only available when the control had the focus so I would be very careful about referencing the .Text buffer. This is the buffer that is generally referenced when you are using the change event. As each character is typed into the .Text buffer, the change event runs and you can ensure that the data length is not exceeded or that certain characters are not present. For example, if you want only uppercase letters, you can check each one as it is typed.

Me.FieldName.Value = the buffer where the completed entry is stored and from which it will be saved. When the control looses focus, the .Text value is moved to .Value and after that, they will be the same. Between the BeforeUpdate and AfterUpdate events, the .OldValue becomes the contents of the .Value buffer. So, at that point, all three buffers have the same contents, assuming you can still reference the .Text buffer

To verify what I am saying, put code into the Change event to display the value of each of the buffers as you type each character.
 
Last edited:
Use Me.Fieldname to reference controls since .Value is the default property. There is no need to include the .Value peoperty and the .Text property has a special use. It is used when the control has the focus. Each control has THREE buffers ---

Me.FieldName.OldValue = The value loaded into the control in the current event. It is either Null for a new record or it is the value that is currently stored in the table which might be null if null is valid for that field.

Me.FieldName.Text = the buffer into which data is typed. In earlier versions of Access, the .Text property was only available when the control had the focus so I would be very careful about referencing the .Text buffer. This is the buffer that is generally referenced when you are using the change event. As each character is typed into the .Text buffer, the change event runs and you can ensure that the data length is not exceeded or that certain characters are not present. For example, if you want only uppercase letters, you can check each one as it is typed.

Me.FieldName.Value = the buffer where the completed entry is stored and from which it will be saved. When the control looses focus, the .Text value is moved to .Value and after that, they will be the same. Between the BeforeUpdate and AfterUpdate events, the .OldValue becomes the contents of the .Value buffer. So, at that point, all three buffers have the same contents, assuming you can still reference the .Text buffer

To verify what I am saying, put code into the Change event to display the value of each of the buffers as you type each character.
very important informations. thanks
 

Users who are viewing this thread

Back
Top Bottom