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

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

Related

Access Query Form with multiple Checkboxes

i have a newbie question and would appreciate every help.
I have a form that runs a query. The query is based on one single table.
In this table there are several Projects.(Project 1, Project2,Project3....etc)
In the form there are several checkboxes, where the user can choose which projects he wants to chek. the thing is with my code if i check 2 projects he shows only the records where both projects are selected in the table. But the I want it to show all the records where either of them is shown.
Ex. If i chech the boxes for Project1 and the checkbox for Project2 it should show the records where Project 1 is checked and also the records where Project 2 are also checked.
SELECT Vergabeumfang.S63T4, *
FROM Vergabeumfang
WHERE (
((Vergabeumfang.VSS_LAW) Like "*" & [Forms]![Vergabeumfang]![VSS_LAW_Box] & "*"
Or (Vergabeumfang.VSS_LAW) Is Null
)
And ((IIf([Forms]![Vergabeumfang]![S63T4_box]=-1,([Vergabeumfang].[S63T4])=True,([Vergabeumfang].[S63T4])=False
or ((Vergabeumfang.[S63T4])=True )))
)
And ((IIf([Forms]![Vergabeumfang]![S63T2_box]=-1,([Vergabeumfang].[S63T2])=True,([Vergabeumfang].[S63T2])=False
or ((Vergabeumfang.[S63T2])=True )))
)
AND ((Vergabeumfang.V_Commodity_Name) Like "*" & [Forms]![Vergabeumfang]![Commodity_Name_Box] & "*"
Or (Vergabeumfang.V_Commodity_Name) Is Null
)
The following query should work for you, assuming I'm understanding right, with a couple of caveats I'll mention at the end. Also, no need to select the field S63T4 specifically at the beginning since the * will retrieve it:
SELECT *
FROM Vergabeumfang
WHERE
(
Vergabeumfang.VSS_LAW Like "*" & [Forms]![Vergabeumfang]![VSS_LAW_Box] & "*" Or Vergabeumfang.VSS_LAW Is Null
)
AND
(
Vergabeumfang.V_Commodity_Name Like "*" & [Forms]![Vergabeumfang]![Commodity_Name_Box] & "*" Or Vergabeumfang.V_Commodity_Name Is Null
)
AND
(
(
Vergabeumfang.S63T2 = iif([Forms]![Vergabeumfang]![S63T2_box]=-1, True, -999)
)
OR
(
Vergabeumfang.S63T4 = iif([Forms]![Vergabeumfang]![S63T4_box]=-1, True, -999)
)
)
Access will reformat the query once you paste it in, but these indents make the logic more clear. The -999 is a dummy value which will never be true on a Yes/No field, to ensure that it always returns false if the criteria checkbox is unchecked.
I reconstructed your table based on the info given, does this look about right? Nonsense data added to the text fields for experimentation's sake:
When the form first opens, it won't show any data, like this:
When you put a checkmark for S63T4 and hit Requery (you'll need some way to tell Access to requery, since it won't do automatically--the button is the easiest way, with Me.Requery for the button's click event, but you could also add this to the AfterUpdate events for the criteria checkboxes/textboxes too), it will look like this:
It looks like you want to further filter on partial text entries, so this picture shows the further filtering when the letter "t" is typed in along with nulls in that field:
Here's a pic filtering on the 2nd checkbox:
Now, when you select both checkboxes, I wasn't sure if you wanted to show projects that are
a) either S63T2 or S63T4
or
b) only S63T2 and S63T4
It sounded to me like you wanted to be able to show projects that are either type, which is what this picture shows:
Now, a couple of tips:
It's easier to construct the SQL statement in code and then apply it to the Form's Recordsource property, especially when you start adding more criteria options. Trying to control all of this with the limited IIF statements is going to get completely unmanageable after a point, because you start having to nest them.
If a project can have multiple types (I'm assuming "S63T4" and "S63T4" are project types), and might acquire more in the future, so that a project might have 10 types someday, it'll be much more flexible to create a new table of project types linked on the ID key, like this:
The criteria form can now look like this:
And now your form doesn't need to be edited when you add a new project type; it'll just appear automatically in the list box.
Anyway, good luck with the project!

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.

Ms access: Autocomplete field with values from another table

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

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.

Access 2007 option group on report

I'm new to Access so please consider this when forming your response. This has been driving me crazy and I've looked high and low on the 'net for a solution. I look forward to your response.
I have a form with an option group. I've wish to have this display on my report. Take for instance this "test" scenario:
Options
a, b, c
I've created a field in my table to accept the data from the form. In my table, I see 1, 2, 3 when I save a record. Good enough. Now, in my report, I have checkboxes representing options a, b, and c. I wish to have a checkmark within the box corresponding to the option selected on the form.
There's no technical limitation preventing you from displaying output on a report using an Option Group and check boxes.
In the design view of the report, add an option group control from the controls toolbox.
Add 3 checkbox controls to the option group control. When you select the check box control and hover the pointer over the option group, it will change color to indicate that the check box will become a part of the group when placed.
I added three check boxes to an option group on a report and they defaulted to values of 1, 2, and 3, so this should go pretty easily for what you're trying to do.
In the Property Sheet with the option group selected, make sure the Control Source property is set to the column with the 1, 2, 3 value in the underlying data source.
You might want to set the border style to hide the box around the checkboxes and also delete the label control that is automatically generated for the option group. I'm not sure what kind of look you're going for, but I'm sure you can handle the formatting details.
An option group is a user interface object, and UI objects don't belong on reports.
Your data field stores digits, but each of those digits has a meaning. On a report, you want the meaningful data displayed. That means that you need a data table that maps 1, 2 and 3 to something, and then join that table to the field you're storing the option group value in.
Another approach would be to use Switch(), but that means that you'd have to edit the report any time you add another option. A data table makes that a lot easier, as you just add a new record to add a new value.