Access SELECT DISTINCT doesn't work - ms-access

Ok so I have a super simple database I am using to try to implement things for a more complicated database however I keep hitting walls on what seems like simple things
The database has one table contactsT with the fields ID first_name last_name I have one form with two combo boxes cboFirstName and cboLastName. I have some repeat first names and I only want the combo box to show unique names. I found this absolutely by the numbers tutorial,
https://www.techonthenet.com/access/comboboxes/unique_values2013.php
however it doesn't work which is baffling because it seems very simple. All that happens is I get nothing now showing in the combo box. Don't see an option to attach the database but here is the table
ID first_name last_name
1 Oliver North
2 Oliver Twist
3 Ren Saturn
4 John Smith
5 John Ringo
the Row source is
SELECT DISTINCT contactsT.first_name FROM contactsT

Your assessment is correct, DISTINCT does not work if you create a ComboBox and use the wizard to link it to a specific source.
The easiest way to bypass this is to simply create a ComboBox, hit cancel on the wizard and then type the query in manually into the property sheet.
This should solve your problem.
FYI: This is due to the fact that Access will try and base the ComboBox on the ID field by default, because of that, the fields you are seeing are technically unique, but only based on the the ID column.
Additionally, this can be circumvented in another method, perhaps you would prefer this (although I find it to be more difficult and slightly frustrating):
You can go into the Property Sheet of the ComboBox and change the Column Widths to include only one column, it most likely currently looks something like 0";1". So to fix it, remove the 0, like 1", this is where Access will "hide" the ID field on the ComboBox. If you then manually edit the query to be DISTINCT for the field you are looking for, that should also fix the problem.

Related

Displaying desired information in a combo box

I am trying to create what I think is a simple access Database. Problem is that I am new to using access but familiar with programming and very familiar with working in Excel. I can technically and have already done the task in excel but what I really like about Access is the Forms. I would essentially like a user to use the form as they would my Excel document but since they would have limited control, there would be less room to mess up the process.
Anyways what I am trying to do is populate a field in a table with a number. However this number signifies a severity (e.g. 1 for low 10 for high). Each severity has a "map" or "gauge" that explains what the number means in severity (e.g. 1 - will not happen, 2 - rarely happens, 3 - sometimes happens, 4 - happens, 5 - always happens). What I would like to do is to have the user see from a dropdown list the number and associated severity meaning. Then what I would like to stay once selected is only the number.
Example case:
User wants to say that something sometimes happens. User clicks on the drop down list and sees this menu:
1 - will not happen
2 - rarely happens
3 - sometimes happens
4 - happens
5 - always happens
They click on the "3 - sometimes happens". Then what they see in the field that they have just edited "3".
I think this should be possible but I am unsure on how I would even go about this. I have only started with tables, I have not made queries or forms yet. I would eventually like the user to do this all on a form so that way they have limited access. For now I am simply trying to populate a table the way I think it should be made.
The best way to do something like this is to use a value list and then display two columns in the drop down but show only one in the combo box.
Step 1
Go to the Data tab of the properties. Set the combo box Row Source Type to Value List and then follow the below example for what to put into the Row Source:
1;"Will not happen";2;"Rarely";3;"Sometimes"; etc
Make sure the Bound Column is set to 1
Step 2
Then go to the Format tab and set the Column Count to 2. Set the Column Widths below it to 1,3 or whatever size suits your system the best.
If you want to use data from a table instead of a value list it's a simple matter of writing a query to select the two columns containing the number and info like below:
SELECT [Severity], [SeverityMessage] FROM TBL_SEVERITY
To use this simply replace Severity and SeverityMessage with the relevant column names in your table and TBL_SEVERITY with the name of the table. Then place this line into the Row Source and change Row Source Type to Table/Query
That should now be you set up with what you're looking for.

Access combo box for DB mapping table 2-1 Table relation

I've run into a bit of a snag with a project I'm working on, and being new to Access I don't know if what I want to do is possible without VBA. I've looked around but all I can find are answers related to showing multiple columns, not controlling multiple fields in my DB.
To paint a picture I have a mapping system set up in my DB to help me distinguish the name and type of data is held in a table.
The setup is as follows:
-Data table is "LineItems" with an ID and the line data (think typical excel format)
-Mapping table is "LineItem_Mapper" with LineItem_ID, DataType, and Entity_ID
-A helper table "Data_Type" with ID and Name
-two "Entity" tables with differing properties both have ID and Name
The reason for the split is on data type is that the two types of data behave differently. One type has a parent child relation, and the other is a standalone row. I want to preserve this structure in my DB and feel I have done so with this mapping.
Now, on to the issue I'm running into. In my Access data entry form I want to use a combo box, as the options a user may chose for each line when entering are finite. However, this combo box is affecting the Mapping table above. I have been able to populate the box with my desired list with a custom query built from my 2 entity tables, but I don't know how to get Access to create or update the Mapping table using this box.
what I want to happen is when I chose something in the box, a line is created (or changed) in the mapping table with all 3 columns being populated. first the LineItem_ID for the line I am populating, and then the DataType and Entity ID to reflect the proper mapping.
Can Access do this on its own? Or do I need to do this with VBA?
As requested by the OP converting my commend as an answer (with a little bit more detail):
By far your best option is to use VBA. I doubt there is another way and even if there is it would be so convoluted it would be unworkable and unmanageable.
This should get you started:
In the combo box properties go tot the events tab and in After Update or On Change (look up the difference between the two events to see which behavior you prefer) click the down arrow and select [Event Procedure], then click on the … button. This will create a VBA module for you complete with the function that runs when the selected event is triggered.
You can use DoCmd.RunSQL "[Access SQL INSERT statement]" to add records to tables.
You can use Me.[MyComboBoxName] to get the current value of the combo box. Similarly the value of anything else in your active form.
You can use DLookup to get the value of any record in your tables.
Hopefully these will give you a relatively quick start.

Update single records in query with form based textbox

I am working with Access2013 and I have a query called PaidOrderQ with columns "SalesRep", "Customer" and "PaidAmount". I need to calculate the SalesRep's commission which isn't always the same percentage for each record. After reading through similar questions here I still haven't figured it out yet.
The way I thought doing it is to have a form called PaidOrderF with soucre is PaidOrderQ, a textbox called "CommRate" and a calculated field "Commission". The "Commission" gets calculated by "AmountPaid"*"CommRate".
I'm not sure how the percentage for commission is calculated, but there are a few ways this can be done depending on how the percentage is calculated. The first would be if you can calculate the percentage that will be used automatically. Then you can add everything automatically (even if the percentage changes based on the item). If this is possible you can open the query up in DESIGN view. Then click onto a blank field and you can make a calculated field for example:
Commission: [AmountPaid]*[CommRate]
Now you can add the calculation for CommRate for example:
CommRate: IIf([AmountPaid]>200,.15,.2)
Which would make any the comm rate 15% over $200 and 20% under.
If not you can make Text boxes in a form (which would be unbound and add an after update vba code to multiply them together and change the value of a third text box, label etc.)
You may also want to add a column to the original table to show commission rate so the information would be stored for future reference.
Let me know if anything is unclear or you would like to see an example of the vba code
EDIT::
Storing information to a table:
This one will be very basic and you probably know most of this, but just in case I will include it because it is the most straight forward.
Add a column to the table you are working from by opening up the table in Design view (you can also make a temporary table by using a make table query if you are using a liked table or do not want to modify the structure, but it is better to put it in the main table if possible). Here you could make a query that includes all of the fields plus the additional calculated field that shows amount paid * commission. Then when you make a form based on the query you will enter in the commission rate and the form will both save the information and automatically update the field to give commission.
Linking another table by Primary ID -
If neither of those options work you could also create another table that shares a primary ID. This option will be more complicated, but is possible. (I would recommend not making it a true primary ID for the second table so it is easier to modify (Or have a true primary ID and the ID you will use to link ie. 3 total columns)). If this is the option you need I can go into more depth, but the disadvantage of this method is if there is no CommRate listed for a given record it will automatically hide the information (it is possible to avoid this, but again will be more complicated) so it will be easy to make a mistake.
VBA coding
VBA will be the most complicated solution, and has the largest disadvantages. First - You will have to loop all of the records at one time (or at least for one employee) and when you close the form you will lose everything. If there is any way to avoid this I would suggest doing so, but you could theoretically store the information in an array.
The Standard Way -
As I understand it your table looks like this:
SalesRep Customer PaidAmount
John Eric 2040
Stacy Brian 1020
Stacy Eric 2004
etc.
You will want to open up your table in Design view and add a column "CommRate"
so it will look like:
SalesRep Customer PaidAmount CommRate
John Eric 2040
Stacy Brian 1020
Stacy Eric 2004
etc.
The data type for CommRate should be Number - type Double - Now I would use Comm rate as a percentage - ie 10.2 = 10.2%, but you could also do .102 = 10.2% either one is fine (more on that later) save and close the table.
Now with the table selected use the create query wizard add all of the fields to the query, give the query a name and click modify the query at the end (or save it then open it up in design view). Now you will see each field in a column with the table listed below. Copy and paste the entry below to your query in one of the bank "field" locations:
for percentage ie 10.2=10.2%
Commission: [CommRate]*[PaidAmount]/100
for .102 = 10.2%
Commission: [CommRate]*[PaidAmount]
Now save and close the query.
Lastly you will select the query then click on create form. This will make a form based on the query. With this form you can add the CommRate individually and it will be saved for the future. You will see that when you add the CommRate the Commission field will automatically update and the table will get updated with the new value.
This is the standard way of doing this. Does this work for what you are doing?
Alright well that is good news! The primary key will help a lot:
Ok here is how you can solve this with two tables.
First create a new table with three columns:
"ID" - Primary Key "RelatedID" - Number Long Int "CommRate" - Number Double
This table will basically just be used to "add on" the CommRate without changing the linked table.
Next you will make a query that includes "SalesRep", "Customer" and "PaidAmount" from the linked table AND the "RelatedID" and "CommRate" from the new table.
When you view the query in design view you will see both tables above the list of the different fields and you will click on the primary key from the linked table and drag it to the "ReatedID" in the new table. From here it will open up a dialog asking about the relationship join. This part is the most important - You want to make the relationship a LEFT JOIN - You do this by saying you want to show ALL records from the Linked table and only the record from the New table that match. Basically this makes it so it will always show the records from the new table even if there is no entry that matches in the new table.
After you have that created you will just make a calculated field as before by copying:
Commission: [CommRate]*[PaidAmount]/100
to a new field on the query design
From here everything is set up and you can create a form based on the query you just made - I would delete the field "RelatedID" from the FORM (or the Query the relationship is what links it so its not necessary it is just a good double check) so you don't change it.
How it works - Now when you cycle through the entries you can add the commission rate directly to the form. - This will make a new entry in your new table that shows the primary ID of the transaction and the commission rate for that sale. This will update the calculated field and show you the commission. Then when you view it it will save the values individually without changing your original table.
Keep me posted if you need more help on how to do this. It can be a little tricky the first time you do it.

MS Access 2010 Multi column Combobox Autocomplete

I have a simple combobox in Access which serves as a result set holder and contains two columns id and name.
I have developed a simple search feature,
i.e user types in a keyword in a textbox and the combobox returns the results after searching the substring,
Example:
TXT BOX Input: App
Combobox Results:
ID Name
1 Cinnamon Apple
2 Apple Candy Box
42 Carton of Apples
54 iphone App
6 App Store
Now when I go to search the combobox box within the results and I type "i", I expect to select "iphone App" from the drop down but nothing happens because it is searching the first column i.e ID
So how can I search for the names and autocomplete the combobox? or search by second column
Also, It is important for me to show the ID, If I hide the ID column I can search by the Name just fine, since it is the only column in the result set.
Hiding the ID is the simplest way of accomplishing what you want, but I recognize that you said you don't want to hide the ID field.
I think it is possible to do what you want. Basically, on every keypress you will need to reset the RowSource for the combobox to be filtered down to only items that match the users keypress. In my experience, this works best in dropdown menus that you are using for filtering, as opposed to dropdowns used for data entry, especially if you are using a continuous or datasheet form. There are a lot of gotchas and caveats you'll run into when doing this. I can't really recommend attempting this unless you are an experienced VBA programmer. My own implementation uses quite a number of events to make it work including Enter, KeyUp, KeyDown, KeyPress, and AfterUpdate. I also use a timer so that I can accumulate their keypresses and only change the rowsource after some 300ms of no keypresses. I also use ADO with a Disconnected Recordset so that I can filter the recordset without making calls to the database again (in the name of efficiency and "keeping the wire cool").
If you want some code, I did post a very basic version of this early in my development of the idea over at UtterAccess. I think it's probably going to be buggy and it might not work properly with you wanting to show the ID field. That is something that I had to make work properly on one of my projects since I originally posted my code.
http://www.utteraccess.com/forum/Autocomplete-Autosugges-t1986007.html
The Workaround is to show Name and then ID in the Combobox, So when I start typing in the Combobox , it Autocompletes Name by Default instead of ID.

Combo box for names isn't returning the correct value

I created a combo box for a persons last name and first name. The combo box works fine you can see a complete list of all the people, however a problem arises when I select someone who shares a surname with someone else.
For example:
Surname | First Name
Romas Issac
Romas Tony
If I select Romas Tony in the combo box I get the value for Romas Issac for the first name. This is a real problem as I'm trying to launch a query based on what is selected - how can I ensure that the correct first name is selected?
Thanks
Add the primary key from the table where that data originates to your combobox as a third, hidden column (to make the column hidden, you can just set the column width to 0; actually, I usually add this as the FIRST column, because then you can leave the entries in the 'Column Widths' property empty for all the other columns). Set its 'Bound Column' property to the number of the primary key column. It is generally good practice to do this with ALL comboboxes that get their data from tables.
If you haved stored the names in two seperate collumns, you have to make sure you are getting both .collumn(0) & .collumn(1), but wakjah's answer is always a good idea ;)