I have a listbox that displays the result of a query. However how the database has been designed all the information is in rows as there is a magnitude of possible controls to one product.
I.e
Productname | Control 1 | Control 2 | Control 3 | Control 4 | Control 5 ........ | Control 14
Product 1 | 010101010 | 101010101 | ..........
I would like the List box to show the column heads on the left rather than the top, or the combo box to List the part no's.
Is this a property issue or is it done through VBA?
So, for a combobox on one of my projects' main screens, I utilize a table as a Row Source. (Technically it's a query I suppose but it's only 2 fields from a table)
I have a table that looks like this:
+--------------+----------------+
| QuickEntryID | QuickEntryName |
+-------------------------------+
| 1 | Add Part |
| 2 | Add Control |
| 3 | Add Product |
+--------------+----------------+
(All of this can be done behind the scenes by making a few selections in the wizard.)
I use a SELECT statement to populate the combobox from this table. When right clicking on a combobox, in the Data tab, there is Row Source, this is where I put my query.
SELECT [QuickEntryLaunchTbl].[QuickEntryID], [QuickEntryLaunchTbl].[QuickEntryName]
FROM QuickEntryLaunchTbl
ORDER BY [QuickEntryName];
I format the column widths, under the Format tab: 0";1" (Or when you create the combobox, one of the steps is a checkbox to "Hide ID Field (recommended)" and I select that.
You should never have to try and arrange the Column name as the row source for a combobox, because that's not actual data. Now, if there was a table like this:
+--------------+----------------+
| PartID | PartNumber |
+-------------------------------+
| 1 | 010101010 |
| 2 | 110101010 |
| 3 | 210101010 |
+--------------+----------------+
Then your combobox would show the PartNumber in the drop down list, and it would actually be usable data.
I guess you could always create another table with column names as rows, but I'm not sure what use that would be.
Related
I have a MySQL table that looks like this (attached below).
I want to simultaneously select (in a single SQL Query) multiple columns (id, Last Name, and username) and import into them into different lists of strings the values if they meet a certain condition (in this case, where color="blue".
+----+------------+-----------+----------+----------------+
| id | First Name | Last Name | Username | Favorite Color |
+----+------------+-----------+----------+----------------+
| 1 | John | Smith | jsmith | Blue |
| 2 | Avery | Nelson | aNelson | Red |
| 3 | Jack | Brooklyn | jBrook | Blue |
| 4 | Arnold | Nam | aNam | Blue |
| 5 | Charlie | Smith | cSmith | Orange |
+----+------------+-----------+----------+----------------+
... Continued
I am trying to select all the required data that meet the condition where color=Blue with the MySQL query of SELECT id, Last Name, username FROM `myTable` WHERE color="Blue". Once this data is selected, I want to import each selected column that meets the color requirement into separate lists.
For example, list FirstName should be list of strings "John, Jack, Arnold" (in that order) and Username list should contain "jsmith, jBrook, aNam" etc. In the end, I want to be able to produce three lists that contain these values which meet the Favorite Color condition in the MySQL database of Blue. How can I do this?
I know that I can make three separate reader queries but I want to do them all in one to save time.
You have not posted VB code so I'm not sure how you intend to use the results but you could load the whole table to a Datatable (unless it is really big), then use Dataview.RowFilter to filter your data with different criteria in-memory, without making any further request to the database backend.
You can see an example in VB.net here
Then you could use LINQ for example to generate a list of string from a datatable or view, see here for an example.
This problem would apply equally if we used listboxes, some combination of list and comboboxes, or even another control type for our first control (textbox, checkbox, etc.) upon which our second control (a combbox or listbox) bases its possible values from which a user could choose.
There are two parts to this problem.
Firstly, having the second combobox's possible values (from which a user can select) be based on a value previously chosen through the first combobox.
After that problem is solved there arises a second. That across the multiple records in the displayed datasheet (or continuous form) the second combobox will blank out values that that were excluded by the last choice of the first combobox.
I answer my own question, solving both parts of the problem ...
Edit 01: Added first and second parts of the problem.
Edit 02: Note to Mods. It is not possible for the question to be more focused. It expresses a single focused problem. If anything the question could be changed to be more general, swapping out "combobox" for "control". However that's undesirable as: a combobox is the more common scenario; and the general applicability is explained in the body of the answer..
Edit 03: Note to mods ...
Just to emphasize why the question is as focused as it reasonably ought be. There exists online the nearby less focused problem E.g. at O'Reilly > Limit the Items in One Combo Box Based on the Selected Item in Another.
But that only addresses the case where you have a combo on a main form and subform (a solution that could also apply to two combos on a form in the standard "form view"). Indeed the O'Reilly page warns that in the more focused circumstance "Using related combo boxes in the detail section of a continuous form" that this "can cause problems" with the implication these are unsolvable problem. (This is the same problem if the form is datashet view).
That focused problem, the specific problem my question references, is also raised repeatedly on stack overflow. E.g.
Using cascading combo boxes in a datasheet subform
Change control source only selected combo box in continous form
Dependent Combobox in a Datasheet in Access?
But each of those posts are less ideally formed in manner which mine has sought to overcome. The first and second aren't in the form of a question (and arguably neither is the third even with the presence of a question mark). And all three of these don't capture (in the text of the "question") that the problem applies just as well to a form in datasheet view as with continuous view (they pick out one view over the other). In that way they are wrongly too focused.
In addition each of these posts have chosen answers that wrongly assert there is no solution to this problem, sometimes with vague allusions to workarounds. My answer provides a detailed solution to something this is generally dismissed on stackoverflow as unsolveable.
Overview
Broadly this is achieved by having the second combobox be based on a parameters query that references the first combobox.
But after you've done that the relatively harder part of the problem emerges. Across the multiple records in the displayed datasheet (or continuous form) the second combobox will blank out values that that were excluded by the last choice of the first combobox.
That problem is solved by temporarily altering the Row Source of the second combobox during the Enter event of that combobox to a "selecting relevant" dataset (that limits possible values in the second combobox based on the value selected in the first). Then on the Exit event of the second combobox the RowSource restores a "display relevant" dataset (that doesn't limit possible values in the second combobox based on the value selected in the first).
Details
Our Example
For all objects, tables apart, I'll use Reddick's RVBA Naming Conventions. Specifically from Host Application and Component Extensions Tags > Access Objects. For tables I won't have any "Hungarian" prefix (this is just a personal quirk of mine).
In our example we'll have a frmCashInflow (Form), displayed as a datasheet, that (obviously enough) tracks cash inflow to a business.
Our first combobox will be cboCashInflowCategory.
Our second combobox, which is to restrict its possible values based on the value chosen in the first combobox, will be cboCashInflowSubcategory.
The relevant lookup data is as follows:
CashInflowCategory (Table)
| CashInflowCategoryID | CashInflowCategoryName |
| -------------------- | ----------------------------------------------------- |
| 1 | Sales |
| 2 | Bank Interest Received |
| 3 | Refunds from Suppliers |
| 4 | Property, Plant, and Equipment - Asset Disposal Sales |
| 5 | Intangible Assets - Asset Disposal Sales |
| 6 | ATO Refunds Received |
| 7 | Contributions from Owner |
CashInflowSubCategory (Table)
| CashInflowSubcategoryID | CashInflowSubCategoryName |
| ----------------------- | -------------------------------------- |
| 1 | Retained-copyright-software-copy-sales |
| 2 | Services |
| 3 | Notice of Assessment, Business Portion |
| 4 | Business Activity Statement |
Next we have a table the expresses the subcategories that belong to particular categories. For the sake of a full example we'll allow that some categories don't have any subcategories.
CashInflowCategorySubcategory (Table)
| CashInflowCategorySubcategoryID | CashInflowCategoryID | CashInflowSubcategoryID |
| ------------------------------- | ----------------------------------------------------- | -------------------------------------- |
| 1 | Sales | Retained-copyright-software-copy-sales |
| 2 | Sales | Services |
| 3 | Bank Interest Received | |
| 4 | Refunds from Suppliers | |
| 5 | Property, Plant, and Equipment - Asset Disposal Sales | |
| 6 | Intangible Assets - Asset Disposal Sales | |
| 7 | ATO Refunds Received | Notice of Assessment, Business Portion |
| 8 | ATO Refunds Received | Business Activity Statement |
| 9 | Contributions from Owner | |
Some aspects of this table:
The fields CashInflowCategoryID and CashInflowSubcategoryID actually store numbers. In design view of the table, on the [Lookup] tab a Display Control of "Combo Box" is set, with Row Sources of CashInflowCategory and CashInflowSubCategory respectively. Other values on the [Lookup] tab are set appropriately/as desired. The net effect is that in the table above we see the user friendly ID names (as above) rather than the raw numeric IDs.
Also in table design view we create an Index called idxCashInflowCategorySubcategory comprising the two fields CashInflowCategorySubcategoryID and CashInflowSubcategoryID . For this index "Ignore Nulls" is set to No. Unique is set to "Yes". This index is created (and a single field primary key is added) rather than setting the primary key to comprise both CashInflowCategorySubcategoryID and CashInflowSubcategoryID to allow that some Categories have no Subcategories. This is a common, although not a necessary, requirement - depending on the business rules.
In the relationship diagram referential integrity is set between CashInflow (table) and:
CashInflowCategorySubcategory (table), via both CashInflowCategorySubcategoryID and CashInflowSubcategoryID. That is, rather than
CashInflowCategory (table) and CashInflowSubCategory (table) separately.
This is to enforce that in CashInflow (table) a Subcategory can't be chosen that isn't associated with a Category in the way expressed by CashInflowCategorySubcategory (Table).
In the relationship diagram referential integrity is set between:
CashInflowCategorySubcategory (table) and CashInflowCategory (table); and between
CashInflowCategorySubcategory (table) and CashInflowSubcategory (table)
That is, the CashInflowCategorySubcategory table expresses a many-to-many relationship between CashInflowCategory (table) and CashInflowSubcategory (table).
The Procedure
We have the tables as above. We have frmCashflow bound to CashInflow (table), the table that adds our transactions.
On frmCashflow add cboCashInflowCategory.
Control Source: CashInflowCategoryID
Row Source: CashInflowCategory
Row Source Type: Table/Query
Bound Column: 1
Limit to List: Yes
Column Count: 2
Column Widths: 0cm;1cm [We hide the ID, as this is numeric and not user friendly]
List Width: 11cm [Some generous value to show our user friendly Category name]
Create a parameter query qprmCashInflowSubcategory in the Query By Example (QBE) grid, to produce the SQL as follows.
SELECT CashInflowSubcategory.*
FROM CashInflowSubcategory INNER JOIN CashInflowCategorySubcategory ON CashInflowSubcategory.CashInflowSubcategoryID = CashInflowCategorySubcategory.CashInflowSubcategoryID
WHERE CashInflowCategorySubcategory.CashInflowCategoryID=[Forms]![frmCashbook]![sbctCashInflow].[Form]![cboCashInflowCategory];
Note that the combobox cboCashInflowCategory on the form is referenced. This is what powers our second combobox cboCashInflowSubcategoryto confine it's values to that selected by the first combobox cboCashInflowCategory.
On frmCashflow add cboCashInflowSubcategory
Control Source: CashInflowSubcategoryID
Row Source: qprmCashInflowSubcategory [Temporarily set, to demonstrate a problem]
Row Source Type: Table/Query
Bound Column: 1
Limit to List: Yes [Attempting to set to "No" causes all sorts of strife]
Column Count: 2
Column Widths: 0cm;1cm [We hide the ID, as this is numeric and not user friendly]
List Width: 8cm [Some generous value to show our user friendly Category name]
Add the following to cboCashInflowSubcategory's On Enter event (among several possible events where you might be tempted execute the Requery) .
Private Sub cboCashInflowCategory_AfterUpdate()
Me.cboCashInflowSubcategory.Requery
End Sub
Open frmCashflow (in datasheet or continuous form view), select a "filtering" value from cboCashInflowCategory. Namely "Sales" or "ATO Refunds Received". Move to cboCashInflowSubcategory and observe that the values are successfully filtered. E.g. If in cboCashInflowCategory you selected "ATO Refunds Received" then in cboCashInflowSubcategory the available choices are only "Notice of Assessment, Business Portion" and "Business Activity Statement" (and we can't see "Retained-copyright-software-copy-sales" nor "Services" ).
However, you'll also see a new problem. The only values displayed in cboCashInflowSubcategory, across the whole datasheet, will be confined to the lastly chosen value in cboCashInflowCategory. E.g. You'll get either ...
| Date | Category | Subcategory | Amount (Inc. any GST) |
| :--------- | ----------------------------------------------------- | -------------------------------------- | --------------------- |
| 2020-07-28 | ATO Refunds Received | Notice of Assessment, Business Portion | $70.00 |
| 2020-07-29 | ATO Refunds Received | Business Activity Statement | $2,000.00 |
| 2020-09-19 | Intangible Assets - Asset Disposal Sales | | $500.00 |
| 2021-01-26 | Property, Plant, and Equipment - Asset Disposal Sales | | $100.00 |
| 2021-03-11 | Sales | | $3,000.00 |
| 2021-04-17 | Sales | | $1,000.00 |
.... or, if "Sales" was lastly chosen in cboCashInflowCategory ...
| Date | Category | Subcategory | Amount (Inc. any GST) |
| ---------- | ----------------------------------------------------- | -------------------------------------- | --------------------- |
| 2020-07-28 | ATO Refunds Received | | $70.00 |
| 2020-07-29 | ATO Refunds Received | | $2,000.00 |
| 2020-09-19 | Intangible Assets - Asset Disposal Sales | | $500.00 |
| 2021-01-26 | Property, Plant, and Equipment - Asset Disposal Sales | | $100.00 |
| 2021-03-11 | Sales | Retained-copyright-software-copy-sales | $3,000.00 |
| 2021-04-17 | Sales | Services | $1,000.00 |
... while what we want is to display all Subcategories ...
| Date | Category | Subcategory | Amount (Inc. any GST) |
| ---------- | ----------------------------------------------------- | -------------------------------------- | --------------------- |
| 2020-07-28 | ATO Refunds Received | Notice of Assessment, Business Portion | $70.00 |
| 2020-07-29 | ATO Refunds Received | Business Activity Statement | $2,000.00 |
| 2020-09-19 | Intangible Assets - Asset Disposal Sales | | $500.00 |
| 2021-01-26 | Property, Plant, and Equipment - Asset Disposal Sales | | $100.00 |
| 2021-03-11 | Sales | Retained-copyright-software-copy-sales | $3,000.00 |
| 2021-04-17 | Sales | Services | $1,000.00 |
To give us what we want, to both restrict cboCashInflowSubcategory's possible values based on the value chosen in cboCashInflowCategory but also display all chosen cboCashInflowSubcategory values as above, we do the following ...
Undo the previous step. That is delete the cboCashInflowSubcategory's On Enter code and event.
Set cboCashInflowSubcategory's Row Source to CashInflowSubcategory (table). This will be our "display relevant" dataset.
Temporarily set cboCashInflowSubcategory's Row Source to a "selection relevant" dataset, then restore the "display relevant" dataset. Add the following event code (verifying this is hooked up correctly in the comboboxes' property sheet)
Private Sub cboCashInflowSubcategory_Enter()
' Temporarily set a "selection relevant" dataset
Me.cboCashInflowSubcategory.RowSource = "qprmCashInflowSubcategory"
Me.cboCashInflowSubcategory.Requery
Me.cboCashInflowSubcategory.Dropdown ' Optional
End Sub
Private Sub cboCashInflowSubcategory_Exit(Cancel As Integer)
' Restore the "display relevant" relevant dataset
Me.cboCashInflowSubcategory.RowSource = "CashInflowSubcategory"
Me.cboCashInflowSubcategory.Requery
End Sub
Everything should now work seamlessly in the UI as desired.
Edit: Added 'Unique is set to "Yes".' for idxCashInflowCategorySubcategory
I have a database for a list of items stored in our office. Some of the items are physically located inside a box, bag or another container, so to describe this situation I have one record for the "Box" item, and a separate record for any and all items inside the box. Here's an example:
+--------+--------------+-------------+----------+
| ItemID | Item | ContainerID | Quantity |
+--------+--------------+-------------+----------+
| 1 | Box | | 1 |
| 2 | Notebook | 1 | 3 |
| 3 | Pencil case | 1 | 2 |
| 4 | Shopping bag | | 1 |
| 5 | Paper plate | 4 | 10 |
+--------+--------------+-------------+----------+
So, the box contains 3 notebooks and 2 pencil cases, the bag contains 10 paper plates, etc.
I have a lot of items to input, and I want to create a form that will make the process easier. Currently I have to go into datasheet view and look up the number for the ItemID I want to reference. If I use a form to enter data, I'll have to either know the ID for the particular container item, or look it up every time.
What would be ideal is if I could click on a drop-down menu and select from a list of all the items already inputted, and when I select the item, it fills in the ID of the Item for the ContainerID field. It would be even idealler if the drop-down menu was ordered with the most recently input item at the top.
Is this possible to implement without VBA?
I have a database which contains a lot of data and although I was not involved in setting it up it is what I have to work with.
Within this database is somewhat of a lookup table. However, this table has no link to any other tables. It essentially takes the following form
ID | input | table_name |
-------------------------------------
1 | Movie | movie_tbl |
2 | Cartoon | cartoon_tbl |
3 | Animation | cartoon_tbl |
4 | Audio | audio_tbl |
5 | Picture | picture_tbl |
The table is a lot larger than the above, but the structure is as above. So what happens is someone visits my site. Here, they have an input field. Say they enter Movie then the above table is called to find the input with Movie. It then gets what table it needs to look in. I would imagine that the query would be something like
SELECT table_name FROM lookup_table WHERE input LIKE Movie;
Now that should return movie_tbl. I now know that I need to search for Movie within movie_tbl and return all the data for its row. So movie_tbl might be like this (data would be some type of data and the column names different)
ID | input | col_1 | col_2 | col_3 |
----------------------------------------------------
1 | Movie | data | data | data |
2 | Cartoon | data | data | data |
3 | Animation | data | data | data |
4 | Audio | data | data | data |
5 | Picture | data | data | data |
So now my query will be something like this
SELECT * FROM movie_tbl WHERE input LIKE Movie;
Now the tables have tens of thousands of lines of data. My real question is whether the above will be effecient or not? With the database I was given however, I do not see any other way I could do this (I cant touch the database). Is there anything I can do to make this more effecient?
Any advice appreciated
Thanks
Why are you checking for input in the 2nd table? You have already filtered the input from the first table:
SELECT table_name FROM lookup_table WHERE input LIKE Movie;
In this case you dont have to make 2 queries. Just the 2nd one should suffice. Or just having Movie data in the 2nd table and separate tables for Cartoon, Animation etc. Because then you wont be accessing the 'WHERE' clause, just:
SELECT * FROM movie_tbl;
2nd Suggestion: Use = instead of LIKE. No need for pattern matching if you know the exact input string.
Ciao, this is my scenario.
I'm building a Report with SQL Server Reporting Services.
I have two DataSet:
Continent
Countries
And I build a report like that:
+----+-----------+
| ID | Continent |
+----+-----------+
| 01 | Europa |
+----+-----------+
+----+-------------+
| ID | Countries |
+----+-------------+
| 01 | Italia |
+----+-------------+
| 02 | Switzerland |
+----+-------------+
| 03 | Germany |
+----+-------------+
| 04 | Etc. |
+----+-------------+
My report work good for one page. Now I would generate multiple pages like that:
Page 1
+----+-----------+
| ID | Continent |
+----+-----------+
| 01 | Europa |
+----+-----------+
+----+-------------+
| ID | Countries |
+----+-------------+
| 01 | Italia |
+----+-------------+
| 02 | Switzerland |
+----+-------------+
| 03 | Germany |
+----+-------------+
Page 2
+----+-----------+
| ID | Continent |
+----+-----------+
| 01 | America |
+----+-----------+
+----+-------------+
| ID | Countries |
+----+-------------+
| 01 | USA |
+----+-------------+
| 02 | Equador |
+----+-------------+
| 03 | PerĂ¹ |
+----+-------------+
| 04 | Etc. |
+----+-------------+
How can I generate multiple pages like example above?
Thanks
It looks like combining the two datasets into one and then grouping on continent would work. You can then set a page break after each continent to produce the output you're looking for.
In the group properties select the option to break "Between each instance of a group."
Join the datasets together in TSQL
Select ContinentId = c.ContinentId ,
Continent = c.Name,
CountryId = country.CountryId,
CountryName = Country.Name
From Contintent c
Inner Join Country on Country.ContinentId = c.ContinentId
Add a table (or tablix) to your design surface. Assign the above data set to your table.
Right click on the details group, Add Group | Parent Group
Group by ContinentId
Click OK
Now go to group properties (of parent group) and Set the page break
Will produce the following output.
I had the need for a report that was 4 pages long per each row in Dataset (read: 66 rows, 66 4-page reports). No rows needed to be displayed, just lots of textboxes with values all over the place on all 4 pages from the dataset row.
I added a Tablix control, and deleted the header row and all the columns in the details row. Then I added a Rectangle to that row. Then I right clicked on the left side of the Details row and added a Parent Group Row that grouped on my row's unique ID (in this case, Client ID). Then I went into Group Properties and checked Page Breaks "Between Each Instance of a Group". This was thanks to info in #Trubs answer above.
I made the first rectangle the size of a page more or less (10in, .5in margins) and clicked on the left border and picked "Insert Row, Inside Group Below". Boom. That was my next page. However, just touching that row causes a TextBox to pop into and take the row's shape so you can write something. All fine and dandy, but I had a bunch of textboxes I needed to set up. So, very carefully, I made sure the TextBox was highlighted and right clicked > Deleted it. And then very carefully I went over to tool box and added a Rectangle. Set the Rectangle to page size more or less, and added my various textboxes.
I repeated this for 2 more pages. Always double checking by previewing to make sure they printed out 2 sided (which is a printer setting.)
I looked at each Rectangle property that I used to outline each page and the option "Keep contents together on same page if possible" was checked, which is probably why everything didn't go "sprong" and add extra blank pages. Not sure, but I wasn't too precise about any page breaks anywhere else but what I have outlined above.
I hope this helps someone. Sometimes you don't need a detail section with a bunch of rows of data. This was my solution to generate a 4-page report with each row's data all over the 4-pages. The Tablix was bound to the Dataset.