Access multi selection list box as a criteria to INSERT, UPDATE OR DELETE records from a table - sql

For a database with this schema (a product may belong to one or more categories):
Item_category(product, category_name)
Category(category_name)
Will it be possible for me to build a multi-selection list box (List box data source from the Category relation) using an Access form and then highlighting the categories that the product belongs to (by querying the Item_category table), and at the same time letting users select new categories or deselect highlighted categories so that when an "Update" button is pressed, VBA code will automatically determine either INSERT, UPDATE or DELETE queries are needed to update the Item_category table?
How can I do that? Thanks!

I've generally done this type of interface using a form which looks very much like the "Which fields do you want on your form?" in the Form Wizard. Where you have the Table/Query list of Available Fields in the table in the listbox on the left hand side and the Selected Fields in the listbox on the right hand side. Along with the move left and right command buttons in the middle. The users quite like this interface.
Added: Note that the left hand listbox contains all the category records which aren't present in the products category table. So a category would be present in one or the other listbox but never both.
You very likely will want to use the Multi Select property of the listbox. If you hit the help on that field in the property sheet it should lead you to a page which mentions the ItemsSelected collection. Visit that page for sample code on how to read all the items selected by the user. Or visit http://msdn.microsoft.com/en-us/library/aa196172%28v=office.11%29.aspx.

Related

How to display out of list items in a combobox?

I have a tbl_ProjectList that includes the project's Name and End Date.
A qry_cboProject queries this table to only display projects in a combobox on Subform A where the End Date is after the selected date on Subform B, both on Mainform C.
On Subform A, a macro copies (INSERT INTO SQL) projects from Subform B's previous months into the new months. However, if an out-of-date project gets copied over to a new month, the combobox field would be empty for that record, even though the Key exists on the back-end.
I've tried playing with the combobox's properties on Subform A by changing the Bound Column, Column Count, Column Widths, and Limit To List, but am only able to get the out-of-date project to display by its Key, rather than its Name.
The front-end reasoning for this macro is that employees do not have to repetitively select the same projects for each month, and employees already working on out-of-date projects may still need to put in some hours to close out the project.
Does anyone have any suggestions? Thank you in advance!
The order in which fields show up in you combo box depends on how the control source is querying the information i.e. to get name and not the key to show up in a combobox using the a control source query like the following:
SELECT Key, Name FROM tbl_ProjectList
You would need to set the following attributes:
Column Count: 2
Column Width: 0"; 2"
Bound Column: 1
It sounds like you may need to requery the control source as well. This should cause all the information to update.
#Parfait - apologies for not describing my problem in more detail. There are multiple subforms on one main form to allow the user to select a date in one subform, which populates projects on a second subform and responsibilities on a third subform.
Jeffrey's suggestion made me realize that the underlying query to the combobox should be adjusted for projects that are carried over into new months, where a foreign key exists in the underlying tbl_ProjectUserEntry
Therefore, I added a WHERE criteria to the query, which uses a DLookUp function to see if the foreign key exists:
DLookUp("[DateID]","tbl_ProjectUserEntry","[DateID] =" & Forms.frm_UserEntry.tbDateID) IS NOT NULL
frm_UserEntry is the main form..
Again, apologies for my brief description to a complex problem.

Ideas for diplaying query results next to buttons

I have a temporary table that is created from a series of query with a VBA code. The name and number of fields in the table changes with the results of the queries.
Right now I simply open the table with DoCmd.OpenTable.
What I would like to do is display the table in a form so I can include some buttons for example to allow the user to export the table.
I tried a listbox but the formatting was not good and I can't add horizontal scroll bar to see all fields.
I tried a subform populated from the table, but it would not adapt to the changes of the table (fields and numbers).
Any Idea of what can I do next ?
the subform seems to be a good idea. When your table is ready to be shown, you can assign it to a subform object via Me.subFormName.SourceObject = "Table.tableName"
(of cource "subFormName" and "tableName" have to be replaced by the actual names of the subform and the table)

Creating a search query in Microsoft Access to query unbound fields and subform

I cannot seem to figure this out. I have made it very far with this database but now i am at a loss. I am going to post screenshots and details below and I hope it will be enough info.
This is my main form (dashboard). It shows the status of calls my employee has received. I need to implement a search. I would like it to search by customer or part.
My Goal is to have someone enter a string in the search field and retrieve matches to display on my details from (that contains a subfrom)
This is the detail screen. It shows the customer and the part(s) on the subform. All the customer info is unbound due to the way it is handled. I have a customer master table my employee can search from and add to the call. If the customer is not in my master table he can enter one manually. The unbound boxes are tied by a CustomerID field.
These are my Relationships for the database
This is the function that matches the CustomerID to fill the unbound boxes
This is my table for the detail form
And finally my table for the subform

Populate combo box with distinct values from one table based on values existing in another?

I am trying to populate a combo box with distinct values pulled from a clients table, but only if those clients exist in the mfgOrders table. I have set the Row Source to the following query:
SELECT DISTINCT Client.ClientName FROM Client
INNER JOIN mfgOrders ON Client.id=mfgOrders.client;
When I switch to Datasheet view, I can see all the distinct clients that are also in the mfgOrders table. The problem is that this does not populate in the combo box.
I originally thought that this may be due to the data size, but oddly enough when I select ClientName from Client, it will populate the box successfully but with ALL clients.
Unfortunately, I don't want all clients in the combo box. Just the clients which also appear in the mfgOrders table.
I'm stumped on this one. Is my logic incorrect?
It appears that when linking a drop-down list to a query, the list will be populated based on how many fields are queried.
In this case, despite the first field being used simply to create the union, Access created a combo box with 2 columns, the first of which had a width of 0".
This appears to have been done because the "show" box for the first field in the query was unchecked. The result was a blank list.
To fix the error I simply re-arranged the query.

How can I bind a multi-row update or insert statement to a form?

I have a table Prices:
ID -- primary key, autonumber long integer
PriceDate -- Date
Price - Currency
Quantity - Number, DECIMAL subtype
UnitPrice - Number, DECIMAL subtype (an update statement is run to keep this in synch with price and quantity, but it's just a convenience for indexing... probably it'll be replaced with an expression in my queries)
ItemNote - Text
NewStores_ID - long integer key, lookup to another table of stores
NewItems_ID - long integer key, lookup to another table of items
To enter prices for a given store on a given day, I would like to be able to select the store and date ONCE on a form, then enter the items individually in a datasheet. For reasons unexplained, this proves difficult.
I can create a subform binding everything but the store and price to a temp table TempPrices with the same structure as the original. Then I run the SQL statement
INSERT INTO Prices
(PriceDate,Price,Quantity,UnitPrice,Brand,ItemNote,NewStores_ID,NewItems_ID)
SELECT
PriceDate,Price,Quantity,Price/Quantity AS
UnitPrice,Brand,ItemNote,NewStores_ID,NewItems_ID)
FROM Temp_Prices;
This will feed all the new rows into the main table. But, when I want to set the store and date only once, I run into problems. I've tried using named parameters for date an store in the insert statement... which can cause a pop-up prompt, but I cannot bind it to a form control. I've tried binding an update statement for those fields in the temp table to a form... but it doesn't even show an option to bind a multi-row update.
How can I get this to work with a minimum of clumsy hackery? It seems like there ought to be a simple solution, and if I were using something like PHP or JDBC I'd just run an extra query.
Edit: changed storage type for Quantity and UnitPrice to Number, Decimal subtype in place of double float. Just so people won't cry about using a float in any proximity to currency. It doesn't pose a problem in my use, but there are enough people who have a knee-jerk reaction to that.
Edit 2: Form/Subform
I'm trying to structure this as a master form with a either fields for entering store name and date, or a subform for the same, then a subform mapping to the temporary table for entering pricing data. There is an action button to run the insert/update queries to dump the temp table into my main prices table and clear out the temp table. However, the problem is that I can't figure out how to get the date/store fields in the master (or subform) to bind to an insert/update value applied to all the new rows at once.
Edit 3: SQL Statements (for clarity)
INSERT INTO
PRICES(NewStores_ID,PriceDate,NewItems_ID,Brand,Price,Quantity,
UnitPrice,ItemNote)
SELECT
#MyStore_ID,#MyPriceDate,NewItems_ID,Brand,Price,Quantity,
Price/Quantity,ItemNote
FROM TempPrices;
UPDATE TempPrices SET PriceDate=#MyPriceDate,NewStores_ID=#MyStoreID;
For these queries, I cannot bind parameters for #MyStore_ID or #MyPriceDate to fields in any form. The queries don't show up as options when trying to link them to a form. I can run them and get popup boxes to input parameters, but that's not what I want.
This is the target I'm aiming at:
"I would like to be able to select the store and date ONCE on a form, then enter the items individually in a datasheet."
However, if you have a reason why you need to do it with a temp table and DML statements, then this suggestion will not be useful.
I created a Prices table with only 4 fields, then a query on that table which I used as the Record Source for a form, "fsubPrices":
SELECT p.ID, p.NewStores_ID, p.PriceDate, p.Price
FROM Prices AS p
ORDER BY p.NewStores_ID, p.PriceDate;
The form has text boxes (txtNewStores_ID, txtPriceDate, and txtPrice) bound to the similarly-named query fields. I set Enabled=Yes for txtPrice, and Enabled=No for the other two.
Then I created an unbound form "frmPrices", and in the form header added a combo box "cboStores" and a text box "txtPriceDate". The combo has Bound Column = 1 with this query for its Row Source:
SELECT l.Store_ID, l.Store_name FROM tblkupStores AS l ORDER BY l.Store_name;
Then I added fsubPrices as a subform control to the detail section of frmPrices. The tricky part is setting the Link Master/Child Fields. There is a "wizardy dialog thing", but it will only allow you to select from the available fields, and I needed to link controls on the main form with fields on the subform. To do that, I had to type what I wanted directly into the subform control's property sheet:
Link Child Fields......NewStores_ID;PriceDate
Link Master Fields.....cboStores;txtPriceDate
The result is ... choose a store and date combination in the main form ... any matching records are displayed in the subform. You can navigate to the "new record" in the subform to add records for that store/date combination. But the txtNewStores_ID and txtPriceDate controls don't show the updated values until the new record is saved.
I hope this is close to what you want. It's actually fairly quick and easy to create; not so easy to describe.
You can't really do that. You are always going to have one row being worked with at a time.
What you can do is simulate it by changing the form's Default View from "Single Form" to "Continuous Form" or perhaps "Data sheet" and making is a child(sub) form of a master form.
Then you can put the store and date on the Master form, and linking to the child form using the NewStores_ID and PriceDate fields.