How do I validate a Form input field with a Table row value in microsoft access? - sql

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 :(

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:
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.
include Items table in form RecordSource - bind textbox to Stock field (Locked yes and TabStop no)
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.

Related

Getting a type mismatch but combo box still searches and displays data. ID column is being used to pull through Name but doesn't work

Error popup after click ok on type mismatch
I am using a combo box to search for records and it is telling me I have a type mismatch. I have been unable to find where this mismatch would be. The combo box still performs the desired function of selecting all records based on their "region"
I am using an ID field which is selected using the combo box but I want to pull through a name as well so I have input an unbound textbox and used VBA on change in the ID to update the name to the corresponding ID in the unbound textbox
Form
For privacy reasons I snipped around some of the other fields showing data.
The VBA in the on_change event in ID goes
Private Sub txtSupplierID_Change()
Me.txtSupplerName.Value = Table("tblSuppliers").SupplierName
Where Table("tblSuppliers").ID = txtSupplierID
End Sub
I'm aware this code could be very wrong but I could not find anywhere else that showed me how to do what I was trying to do. Any suggestions on how to actually do it would be appreciated. If I have not been as detailed as needed or yo have any questions please ask.
Edit for Clarification:
The form is not being used to save data. It is only being used to display data and issue an output to a report.
Code is not just wrong, it is nonsense. Cannot reference a table object like that. And that Where usage is not valid. One way to pull value from a table is with DLookup domain aggregate function. Advise use AfterUpdate instead of Change event.
Private Sub txtSupplierID_AfterUpdate()
Me.txtSupplerName.Value = DLookup("SupplierName", "tblSuppliers", "ID = " & txtSupplierID)
End Sub
Might want to correct textbox name from txtSupplerName to txtSupplierName.
However, should not save both supplier ID and supplier name into this table. There really should be no need for this VBA. Just put DLookup() expression in a textbox ControlSource. For other methods of pulling the supplier name to form, review answer in MS Access Identical Comboboxes for Autofill

(Access) search text field for strings matching those in another table's field and display these matching records in a subform

Here's my situation,
tbl_products / frm_products
ID__products
products__name (short text)
products__description (short text)
tbl_articles / frm_articles
ID__articles
articles__name (short text)
articles__body (long text)
I have a form bound to tbl_articles containing a subform bound to tbl_products.
What I would like to happen is,
once a user enters text into the articles__body field, (using the After Update module)
it searches this long text field for any words which match a product name
and then (in the subform) displays the matching products.
For example if the articles__body record that the user is currently viewing contains 'product 1', it will display product 1's record in the subform.
Perhaps After Update is not appropriate as it needs to appear to remember these matches. For example, if tbl_articles' record 1 matches/displays product 1 on the subform whereas record 2 matches/displays products 2,3 and 4; I need the user to be able to revisit record 1 and see the product 1 match without having to edit the text (and trigger the After Update).
I have no idea where to start with this. It's not a simple if string contains, Originally I entertained the idea of something like - Like '* [in tbl_products, record 1's products__name] *' repeated for each record (obviously not the correct syntax but simply identifying the process to myself), however this is impractical because the number of strings to match against will grow over time as more products are added.
Any help would be great,
Kind regards
Set Filter and FilterOn properties. Suggest naming subform container control different from the form it holds, like ctrProducts.
Me.ctrProducts.Form.Filter = "InStr('" & Me.articles_body & "', [products_name])>0"
Me.ctrProducts.Form.FilterOn = True

PowerApps formula for matching records from a drop down

I'm trying to build a login form that performs a simple check against the user's PIN. The pin in stored in the database as a field in the Person entity (table).
I have a Team entity that allows the Person to select the Team they are on from a dropdown list of all Teams. The Person entity dropdown then populates based on the Person's TeamID. The User selects their Name and enters a PIN number in the txtPinNumber textbox.
The Login button is hidden by default and should only have visible = true when the pin entered into txtPinNumber is the same as the PIN field in the database.
I have the visible property of the btnLogin button set to
If(txtPinNumber.Text=Filter(Personnel,ddResponder.Selected.PIN),true,false)
However, I'm getting an error of unspecified type. Is it because I'm trying to check the entered PIN against a filtered datatype?
Any ideas on how to fix this error?
It looks like you're having difficulty because of your usage of the Filter() function. Filter() returns a table with all of the rows that meet a given criteria. However, it seems that ddResponder.Selected.Pin is the PIN you are seeking to match. If this is the case, you can simply have the visible property set to:
txtPinNumber.Text = ddResponder.Selected.Pin
If, however you need to do a search in your table to find the relevant PIN, you will instead want to use the Lookup() function.
You can see the documentation for these functions here:
https://powerapps.microsoft.com/en-us/tutorials/function-filter-lookup/
The Lookup() function will return the first matching row based on the criteria you provide, otherwise it will return a blank. You can use this with the IsBlank() function to determine whether or not to display the button.

Problems with Report Variables

I'm new to SSRS 2012 and have a question about variables. I have a report that retrieves order and order line information from our database (order #, order line #, item, quantity, etc). I need to add a textbox to the footer of the report that can only be displayed for certain items (i.e. if item # equals "123" then show textbox, otherwise hide it.
I added a report variable called 'ItemExists', and unchecked the read-only flag, and set the default value to 0.
I then added an expression to the textbox on the report footer to show it if variable=1, and to hide it if variable=0.
I'm trying to add a textbox with an expression to the body of the report (same group as where the item # is displayed), but I'm unable to get the report variable to update properly. Here's what I tried:
=IIF((Variables!ItemExists.Value = 0) and (Fields!item.Value = "123"), (Variables!ItemExists.SetValue(1)), Nothing)
This works if the value of the item is 123, but if the item is not equal to that value, it still sets the ItemExists variable to 1.
The reason I'm checking to see if the variable is equal to 0 is that there may be multiple lines in the report, and if any one of the items is a match, I want the variable to be set to 1.
I'm not sure if I'm way off track here, but if you could provide any assistance on the best way to achieve this, that would be great.
Thanks in Advance.

Subform "assigned" control not updating

I'm creating a MS Access 2010 database to allow users enter series of data which would be used in a query.
I have a Main form named "Entry" and a bounded subform named "SubEntry" (used to view the entered records).
The main form (Entry) consists of text/combo box controls that allows users to key in values, such as:
**Controls ControlSource**
Date "Entered by the user"
CompanyID "Entered by the user"
TicketID "Entered by the user"
TicketPrice "Automatic retrieved by a query based on the entered 'Date' and 'TicketID' "
Pieces "Entered by the user"
TotalPrice "TicketPrice * Pieces"
CorrectedPrice "Entered by the User"
Revenue "=IIf([CorrectPrice]=0,[TotalPrice],[CorrectPrice])"
This works fine with entering and viewing record-sets. But the problem is, at the initial stage when the user adds recordset to the subform, the exact "Revenue" value is added to the subform's "Revenue" field. But after then, when the user scrolls in the subfrom and chooses a recordset (which appears in the main form's respective controls) and changes the values. For instance chooses another ticketID or Date, the "Revenue" value in the main form changes but not updated in Subform's Revenue field. Meanwhile anyother changes made main form automatically updates in the subform but not that of the "Revenue" value.
I'm not that conversant with MS Access nor VBA, just wondering if there is something obvious that is missing? How to deal with the SubEntry's "Revenue" field to automatically updates like any other fields in the subform does.
Thanks for your time and assistance.
You need to associate the textbox with the relevant field in the subform, so:
txtEinnahme
ControlSource: KorrEinahme
Then you need code to add the calculation, for example:
Private Sub txtpiece_AfterUpdate()
Me.txtEinnahme = IIf(Me.[DKorrEin] = 0, Me.[txtTotal], Me.[DKorrEin])
End Sub
Put similar code in any control should change the value of txtEinnahme.