0
votes

I am having issues with setting a Validation rule in Access.

I have a database with the tables Clients, TypeClient, Sales, SalesList, Items.

I have a form for Sales, with a sub-form SalesList inside which has a relationship with Items in order for me to put several stock items in Sales instead of only one item.

Inside Sales table is ID, Date Sold, ID Client.

Inside SalesList is ID_List(referenced to ID in Sales), ID_Item(referenced to ID in Items), Quantity.

Inside Items is ID, Name, Stock(how much we have in stock), Price

The issue is that I am trying to validate the data I enter in the sub-form Quantity field to be higher than 0, but no higher than the available stock.

The issue is that I have tried using the expression builder to get the value from a calculated query that has 2 fields - the ID and the value for that id, with criteria that is for the ID to get it from the main form, the sub-form, the combo box input and the query works.

But when trying to get it like this:

It shows an error "The expression [Query bla] you entered in the form control's ValidationRule property contains the error The object doesn't contain the Automation object 'Query bla'".

I tried using directly the table Value(but it won't work anyway as it doesn't know for which field to get the stock value from), still the same error. I guess it can't reference to anything else? Normal validation rules work, but I want to validate it not to exceed the value of the available stock for the item I am selling now.

Nowhere on the internet there is a tutorial how to do it with expression builder. How do people validate their fields then?

It doesn't stop me in this case to sell 200 items when I currently have stock of only 2 of them, for example.

Note: I have tried DlookUp in expression builder, straight up tells me No.

Sample wrong validation rule expression builder code:

<=[Query bla]![Stock]

<=[Items]![Stock]

I am currently using VBA and fetch the record I need(the current stock) with one of the following and my unbound text is changing on every subform to the same, it shouldn't happen like that. What can I use to populate a field for each record uniquely:

Private Sub ID_Product_IZBOR_Click()
    'Me.Stock_ValueField = DLookup("[Nalichnost]", "Stoka", "[ID]=" & Me.ID_Product_IZBOR)

    Me.Stock_ValueField = Me.ID_Product_IZBOR.Column(2)
End Sub

Partial solution: I created a new Dim as Integer and fetched the records based on the ID provided by the field in the form and used BeforeUpdate to validate the current stock. Unfortunately the only way to see the current stock level is to click on the combo box where you choose your product and check the column for that one, it doesn't show anywhere else :(

1
Consider using the Quantity control's Before Update event to do your validation.HansUp
How would that happen with a VBA example? I am a bit rusty on that topic.Mishoka
Ideally, stock balance would not be saved into table - it would be calculated from raw data when needed. Review allenbrowne.com/AppInventory.htmlJune7

1 Answers

0
votes

Don't use the expression builder (I NEVER do) - just type the needed expression in property.

>0 AND <=DLookup("Stock", "Items", "ID=" & [ID_Item])

Another approach is to return Stock in textbox ControlSource then ValidationRule references that textbox. Also, user can then see the quantity limit. Methods of pulling the Stock value from Items table:

  1. include Stock field in Items combobox RowSource - textbox then references column by its index (index begins with 0): =[cbxItems].Column(2); VBA may be needed to Requery combobox after record entry/edit is committed to table.

  2. include Items table in form RecordSource - bind textbox to Stock field (Locked yes and TabStop no)

  3. DLookup() expression in textbox ControlSource

Use ValidationText property to give users a custom message.

However, ValidationRule will not prevent user not entering any value if they skip the control. If you want to make sure a value is entered, either set field as required in table or use form BeforeUpdate event to validate record data.