Ms access: Autocomplete field with values from another table - ms-access

please forgive me for my poor english and my big ignorance on programming.
I'm using Ms Access 2003.
Let's suppose i have two tables:
Table1: ID (autonumber), [...], Keywords (memo)
Table2: ID (autonumber), Keyword (text)
I want:
1) As the user types letters in Table1.Keywords that my database searches in Table2.keyword for the nearest value and proposes it by autocompleting (just like google proposes a search word as you type)
2) When user presses ", " that he can add one more keyword in the same field (and the autocomplete still runs for this next value)
3) If he types a keyword not included in Table2 and press ", " that he is asked if he wants this value to be added in Table2
Well, i'm not sure if all these are clear... maybe they are a lot of things...
But i'd appreciate if you could help me...
Thanks in advance
J.

It would be complicated to do it with a single control, but with two controls, a dropdown list for choosing the value to add, and a textbox displaying the memo field, you could have the combo box's AfterUpdate event append a comma and the chosen value to the existing data. Something like this:
Private Sub cmbChooseKeyword_AfterUpdate()
If Not IsNull(me!cmbChooseKeyword) Then
Me!txtKeywordMemo = (Me!txtKeywordMemo + ", ") & Me!cmbChooseKeyword
End If
End Sub
You'd also want the rowsource of your combo box to not list items that are already entered, so this is one way that would work for a relatively short list of keywords:
SELECT tblKeywords.*
FROM tblKeywords
WHERE InStr(Forms!MyForm!txtKeywordMemo, tblKeywords.Keyword) = 0;
Then you'd add:
Me.Dirty = False
Me!cmbChooseKeyword.Requery
...at the end of the AfterUpdate code above (inside the End If):
Private Sub cmbChooseKeyword_AfterUpdate()
If Not IsNull(me!cmbChooseKeyword) Then
Me!txtKeywordMemo = (Me!txtKeywordMemo + ", ") & Me!cmbChooseKeyword
Me.Dirty = False
Me!cmbChooseKeyword.Requery
End If
End Sub
...and you'd want to add the requery to the OnCurrent event of your form, as well (so that when you arrive on a record, the combo box already omits any keywords that are already in the list).
Now, all that said, I'd completely recommend against doing this. This is a denormalized way to store the data, and this leads to problems:
what if you want to delete one keyword?
what if you want the keywords to be sorted in alphabeticsal order?
what if you have 100s of thousands of records and you want to search this field with LIKE "*Keyword*" -- will it bog down to be terribly slow (no indexes, and not used well even if there were)?
You really should use a proper many-to-many structure, with an additional table between the one where you're currently storing the keyword memo and your keyword list table. This "joins" the two, and would then give you a list.
You could then use a subform with a dropdown list to populate each row of the join table.
If you like presenting the keywords on reports as a comma-separated list (as you're currently storing them), you can write a simple function to do the concatenation for you at the presentation layer of your reports (concatenation functions for that purpose are a frequent Access question here on Stackoverflow).

Why not use a "Combo Box" and set its Row Source Type to Table/Query, and then make the Row Source a query on the second table. Just make sure you don't turn on Limit to List.

CodeSlave mentions a way that will work. But it will only work for one value. There is no way to do the multi-words-separated-by-commas thing. Only one word at a time.
As for the Adding new values. The combobox control support an OnNotInList event which can do what you say.
Seth

Related

Populate two rows of a combobox with a query Access 2007

I'm working on an Access 2007 database and I'm having a problem with a query.
I have a table named Vehicles, which contains data such as ID, license plate and fuel type for each vehicle in it.
I'm trying to make a query which will populate a combobox in a Form with each vehicle's fuel type, based on the license plate chosen by the user beforehand.
The thing is, we have some some cars that work with two types of fuel and I cant' find a way to display them separately in the combobox.
So far it kinda works, code follows:
CheckDiesel: IIf([Diesel]="Yes";"Diesel";IIf([Gasoline] AND [Ethanol]="Yes";"Gasoline"+ "Ethanol";IIf([Ethanol]="Yes";"Ethanol";IIf([Gasoline]="Yes";"Gasoline";""))))
If you look at the second IIf, I have a condition for a bi-fuel car. I want to display Gasoline and Ethanol separately, each one in a row.
I've tried using "&Chr(10) Chr(13)&" and "\r\n" but I had no success so far.
Can anyone help me?
Storing multiple pieces of data in a single field rarely ends well. A few options I see
A series of binary fields for gasoline type. So you'd have a True/false for gasoline, ethanol, and diesel. This is easy to show with checkboxes on the form.
If you know there will only be certain combinations, like if there will not be a 'diesel and ethanol' without gasoline, you can build it as a single value combobox.
You will probably need some VBA to do what you need to do. Try something like this:
Dim R as String
R=""
if (Me.Diesel) then R = R & "Diesel;"
if (Me.Gasoline) then R = R & "Gasoline;"
… {add any other types of fuel}
Me.MyComboBox.Rowsource = R
Me.MyComboBox.Requery
This can be added tot he form's OnCurrent event so it will update the combo box every time a new record is displayed.
If your form does not include the various fuel types as fields, either add them as hidden fields or use DLookup to read them off the table.

Access: Fill a combobox with selections from another field

I'm just learning how to use Access and while I've managed to muddle my way through most of what I'm trying to do, there's something I haven't been able to figure out how to do yet.
I have two forms and corresponding tables. In frmProducts is ColorOptions, a multi-select combobox containing a list of possible color options for a product, and Design, a text control for the name. In frmCustomers is OrderDesign, a combobox with a list of items from tblProducts, and OrderColours, a combobox.
Now, this is the problem: I want OrderColours to display list of the color options in tblProducts, but I can't figure out how. I can get it to display the value, but it's not a list of items, just one entry with the 'list' (e.g. a single entry reading "Brown,Red,Green"). I want the user to be able to select a single item from that subset.
Ideally I'd like to do this without messing with VBA or any advanced SQL, but if that's not possible then that's fine as well. I think the issue may be that the Colours field which contains the colours for that product is stored as text, but I'm not sure how else to store it as there's no 'array' or 'list' option for datatypes.
Sorry if I haven't been clear enough, or if this is posted in the wrong sub. I'm a beginner in Access, so I may have not been clear enough or used the wrong terminology. Any help would be much appreciated.
I'm not quite sure I understand exactly how you want this set up, so I'm assuming the following. Please correct me if this is not right:
tblProducts contains (at least) the two fields productDesign and productColour
It is possible for there to be multiple records in tblProducts with the same productDesign but different productColour (different colours of the same design)
There is another table, tblCustomers, in which each record contains a productDesign and one of the corresponding productColours.
So you need the combobox OrderColours to display a list of the possible productColours for the selected value of productDesign in OrderProducts.
Now, set up combobox OrderDesign as follows:
Row Source Type: Table/Query
Row Source: SELECT DISTINCT tblProducts.productDesign FROM tblProducts;
and OrderColour:
Row Source Type: Table/Query
Row Source: SELECT tblProducts.productColour, tblProducts.productDesign FROM tblProducts WHERE (((tblProducts.productDesign)=[Forms]![frmCustomers]![OrderDesign]));
Column Count: 1
and give OrderDesign the following event AfterUpdate:
Private Sub OrderDesign_AfterUpdate()
Me.OrderColour = Null
Me.OrderColour.Requery
Me.OrderColour = Me.OrderColour.ItemData(0)
End Sub
You may well also need to consider what happens when moving between records, if your comboboxes are bound:
Private Sub Form_Current()
Me.OrderColour.Requery
End Sub
in the Form_Current event should do the trick.
Read this for details.

Filtering A Lookup Field Based On Another Field

I have a lookup field in my table based on another table. I'm having trouble filtering those values based on another field that is entered prior to the field.
Is it possible to filter a lookup field based on another field?
EDIT
Let me try and clarify my original question, sorry about that. Ok, so I have a table1 that has the following fields: ID, Name, Logo.
If a user enters a specific name in the Name field, when they click on the Logo field, it'll only display those values associated that are similar to the name entered. Does that make any sense? If it does make sense, would there be an easier suggesion on accomplishing this task?
If you're talking about inside a table, the answer is "No". You can create cascading combo boxes on a form, but you can't base a lookup value in a field of a table off of a different field in that table (or the field in any other table).
Here is an example of how to handle filtering a combo box based on the value selected in another combo box:
I have the following form:
The combo boxes are named cboIntPN and cboManPN.
The Row Source for cboIntPN is set to: SELECT uniq_key, part_no, revision FROM inventor. The Row Source for cboManPN isn't set to anything.
When the user selects a value for Internal PN the following AfterUpdate Event is triggered:
Private Sub cboInternalPN_AfterUpdate()
[cboManPN].RowSourceType = "Table/Query"
[cboManPN].RowSource = "SELECT uniqmfgrhd, mfgr_pt_no FROM invtmfhd " & _
"WHERE uniq_key = '" & cboIntPN.value & "'"
End Sub
It sounds like he is having the same issue as me. I also wanted to filter a field in a table for data entry on another field's input and my conclusion is "it is time I stopped entering data manually in tables and begin to create Data entry forms. I was putting this task off until later, but if I don't do it now, I might make worse trouble for myself later.
Btw, what an old thread.

How do I change the values of a radio button from 1, 2, 3, etc to text values?

I have created a small database in Access 2007 that consists of one table and two forms, one for entering data, and one for retrieving data.
My problem is this: On my input form I have a group box with three radio buttons in it. The question being asked is Is the element a sensor?
The buttons represent Yes, No, and Don’t Know.
In the database I have a column named Sensor to hold the value the user chose, but since the radio buttons return a value of 1 for yes, 2 for no, or 3 for don't know, it makes generating a report or query that makes sense to the user very difficult.
At this point I’m writing huge SQL statements with nested iif’s to return the data the way I want to see it.
Is there a way to populate the table with data the way I want to see it (yes, no, don’t know) instead of populating it with 1’s 2’s or 3’s? This is a bound form by the way, I wish I would have done it unbound, but I can’t go back now.
I would suggest not using the radio buttons, and instead opt for a combo box. You'll be able to use string values for the results directly in the combo box.
Now if you are dead set on using radio buttons, try this:
Add a new field to your table that holds text. Bind this to a hidden text box on your form.
Then, add a BeforeUpdate event (or AfterUpdate depending on what you are doing) to the radio group. Add code similar to the following:
Sub RadioGroup_BeforeUpdate(cancel As Integer)
Select Case Me.RadioGroup.Value
case 1
Me.hiddenTextField.value = "Yes"
case 2
Me.hiddenTextField.value = "No"
case else
Me.hiddenTextField.value = "Don't Know"
End Select
End Sub
Now when you save the record, the human readable value will be available in the new field you added.
"since the radio buttons return a value of 1 for yes, 2 for no, or 3 for don't know, it makes generating a report or query that makes sense to the user very difficult."
Store those 3 pairs as rows in a Sensor_Values table:
sval descriptor
1 yes
2 no
3 don't know
Then you can join that table to the table which includes the stored Sensor numbers.
SELECT yt.Sensor, sv.descriptor
FROM
YourTable AS yt
INNER JOIN Sensor_Values AS sv
ON yt.Sensor = sv.sval;
If you're opposed to creating and joining a lookup table, you could use a Switch() expression in your queries to translate the numeric Sensor values to their text forms.
SELECT
Switch(
Sensor = 1, "yes",
Sensor = 2, "no",
Sensor = 3, "don't know"
) AS sensor_text
FROM YourTable;
The Switch() approach can work, but can be more challenging to maintain compared to the lookup table approach.
My intention here was to show you fairly simple methods to use the option group value as a number instead of "populate the table with data the way I want to see it (yes, no, don’t know) instead of populating it with 1’s 2’s or 3’s"
As a general rule, you will be better off working with Access controls as they were designed to be used. Break that rule whenever you have a compelling reason ... but breaking the rule then requires additional efforts from you ... like more VBA code. The approaches I suggested don't require any VBA.
you could use a Select Case when creating the select string, instead of having iif's nested in the SQL.
Select Case Me.rdoSensor
Case 1
sSQL=sSQL & " AND Sensor='Yes'"
Case 2
sSQL=sSQL & " AND Sensor='No'"
Case 3
sSQL=sSQL & " AND Sensor='Don''t know'"
End Select

How can I display a *foreign* field value in a text box?

How do I bind a text box with a field, which doesn't belong to form's "Record Source" table, through the Design View?
Example: I have "Order.cust_id" (Record Source=Order) and I want to display "Customers.name". I believe it is trivial but I have no experience with MS Access. I tried to use the text box "Control Source" property but no luck.
One method would be to convert the text box to a combo box. Then set the row source to include both the cust_Id and the Customer.Name from the customer table. SQL statement example
Select Cust_ID, Name From Customer
Order By Name;
By setting the number of columns to 2 and the column widths; the first column as zero (i.e. "0;6") then the foreign key would be hidden from the user and the customer name would be displayed.
Note this method does force you to have limit to list set to true.
Also you do end up with a drop down list which may not be what you want.
You can use DlookUp as the control source of a textbox:
=DlookUp("[Name]", "Customer", "ID=" & Cust_ID)
Syntax: What to look up, table name, where statement
The Where statement should follow the rules for Jet SQL, which means that you must use delimiters if the field is text or date format.
Note that Name is a very bad name indeed for anything. I suggest you rename the field immediately before things get worse.
It can be useful to know the error(s).
You could create a new View (e.g. OrdersAndCustomerNames), select all the columns you want to use in the form, then instead of using the Order table as Record Source, you would just switch to OrdersAndCustomerNames. You say you have no experience with MS Access, so I am guessing you are not building anything huge and overly complicated, so I would do it this way. I am quite sure it can be done more elegantly but this will do for now.