VB.NET ListView Problems - vb.net

I have two list views, LVPRODUCTS & LVSUPPLIERS.
When I click a Productdata in LVPRODUCTS, LVSUPPLIERS will show the suppliers of the product and the Productdata will be inserted in a textbox which can be use to add another supplier for it.
QUESTION.
When I add a supplier to my product and save it . I want LVPRODUCTS to keep my chosen Productdata Clicked and LVSUPPLIERS will just refresh its data and show the new set of suppliers including the newley added supplier.

I would record the variable using listbox.selecteditem and then after refreshing setting the selected item back to the recorded one.
try something like this: (untested code)
dim item as string = LVPRODUCTS.selecteditem
'do your refresh
LVPRODUCTS.selecteditem = item
Below is a link to the property page.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditem.aspx#Y0

Related

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

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.

Prevent to click "Add to Cart" button if specific product not selected - Shopify

Hi experts have a nice day,
I have a project on Shopify where I'm getting struggled on how to prevent and make alert when a specific product selected for some conditions.
I made a product template where I populate list of products from collection A as a first set of selection and collection B as a second set of selection.
This is a product page where I can select 1 product from collection A and also 1 product from collection B (Each product has Add to Cart button)
The condition is collection A is required to have a least 1 product added from collection B while collection B does not required to have product from collection A.
Here is I wan't to achieve:
when either products from collection A button clicked it will alert like
"Need to select at least 1 product from collection B"
I'm using cart.js for the multiple products in a product template.
Is this possible? or is there's a simplest way to implement this?
Thanks
I can't speak to the inner workings of cart.js, but what you're asking for should be reasonably straightforward with regular Javascript.
What it sounds like you want to do is:
On submit of Form A;
Select Form B;
Check if Form B has a variant selected;
Prevent the add-to-cart action if no variant in Form B
If cart.js has an easy way to insert a validation function, that's where I would insert the code. Otherwise, I would add the validation function to the form submit or click of the submit button, whichever is more convenient with cart.js being in play.
A sample validation function would look something like:
function validateFormA(evt){
var formB = document.forms('product-form-b'); // document.forms can access any form on the page using the appropriate ID attribute
if(!form){
// Couldn't find form, should this pass or fail?
return;
}
var variantIdField = form.id; // To access the form control with the name 'id'
var value = variantIdField.value;
if(!value){
evt.preventDefault();
return false;
}
return true;
}

How to prevent duplicate item in listview

I want to add an item to a ListView when a button is clicked and I want to add the quantity. I keep getting an error saying
Cannot add or insert the item 'Burger' in more than one place. You must first remove it from its current location or clone it.
Here's my code so far:
For Each listItem As ListViewItem In lvOrder.Items
If Not lvOrder.Items.ContainsKey("Burger") Then
listItem.Text = "Burger"
listItem.SubItems.Add(1) 'Quantity
listItem.SubItems.Add(50.0) 'Price
lvOrder.Items.Add(listItem)
Else
MessageBox.Show("Item already exist")
End If
Next
You can't add more than one item with the same Name (key) to a ListView. But, I think what you are actually trying to do is edit an existing item.
You can simplify your code a lot. Add items like:
Dim newListItem As New ListViewItem
newListItem.Text = "Burger"
newListItem.Name = "Burger" ' this is a unique key
lvOrder.Items.Add(newListItem)
Notice that Text and Name are different properties. Text is what is displayed to the user, and Name is a key that helps the ListView keep track of what items have been added. It can be the same as Text, or something totally different.
If you need to modify an item you've already added, it's as easy as looking it up with the key!
lvOrder.Items.Item("Burger").SubItems.Add(1) ' Quantity
lvOrder.Items.Item("Burger").SubItems.Add(50.0) ' Price
No need to loop through every item to find the one you're looking for!

Add unbound combo box to form that can trigger a BeforeUpdate() event?

I have a datasheet form ItemsForm based on table Items. Items is one-to-many related to table StatusHistory, between Items.ID and StatusHistory.ItemID. There is also a Status table, with the relationship between Status.ID and StatusHistory.StatusID.
I want to add a StatusBox combo box to ItemsForm so that when the user selects a Status value from the box and then moves out of the record, which should trigger the Form_BeforeUpdate() event, a new entry is added to StatusHistory with the Items.ID of the currently selected entry.
I've successfully added the StatusBox field to the form and populated its list by setting its RowSource with a query of Status. But there are two big problems:
I can scroll through the values in the box's list, but after I
select one, it doesn't show up in the field; the field stays blank.
When I select a value in StatusBox, and then click onto another
record, the Form_BeforeUpdate() isn't triggered. It seems that
Form_BeforeUpdate() is only triggered if I modify data in the
fields from Items that the form is based on. Is there a different event that I should be using here?
Solved it as follows:
The ID field for Status is actually named StatusID instead of
ID. Fixing this allowed me to enter values into the field.
I put the code into the BeforeUpdate() event for StatusBox, viz:
Private Sub StatusBox_BeforeUpdate(Cancel As Integer)
End Sub
This means it gets triggered whenever I select an item in the list, rather than when I move to a new record, but for now that's all I need.

VB.NET web application - Update a data bound Listbox when underlying table / query changes

I have a page in my web application that contains two listboxes with buttons to move items back & forth between them. Each listbox is bound to a SQL query, the results of which change as the selected items are added or removed from the corresponding lists. This all works fine, except I cannot get the list boxes to update their contents on the fly, however if I refresh the web page, the contents update correctly.
Basically the user selects items in LeftListbox and clicks the Add button which calls code to loop through the LeftListbox and for each selected item adds a new record to a table (Score). The RightListbox should then update to show that the items have been added to the table.
Here is a snippet of code from the Click event of the Add button:
Dim i As Integer
For i = 0 To (LeftListbox.Items.Count() - 1)
If LeftListbox.Items(i).Selected Then
Try
DbUtils.StartTransaction()
Dim rec As ScoreRecord = New ScoreRecord
rec.Player_ID = CInt(Me.LeftListbox.Items(i).Value)
rec.Save()
DbUtils.CommitTransaction()
Catch ex As Exception
DbUtils.RollBackTransaction()
Me.Page.ErrorOnPage = True
Finally
DbUtils.EndTransaction()
End Try
End If
Next i
'** Here is where I want to refresh the list **
I've searched quite a bit for a solution, but I can't find anything that works so any help would be much appreciated.
Andrew
Use the same method (or code) used to populate the "right listbox" in the first place. The right ListBox's DataSource will be the same as it was prior to this code snipped being ran, so it must be updated since the underlying data has changed.