Access IIf statement retrieving a value using a Check Box - ms-access

I am attempting to have a single List Box pull up two possible values based on whether or not a Check Box is filled. The Row Source I have for the List Box is:
IIf([Forms]![frmPDPRate]![chkTrue], [qryPDPRate]![Initial], [qryPDPRate]![Renewal])
The specifics are that I'm trying to pull a rate, either Initial or Renewal, based on if [chkTrue] is true or false.
Another note: The query that the list box should be pulling from has been paired down to one row based on cascading combo boxes. So there is only one possible choice per column in the query.
EDIT:
I think I'm going about this all wrong but I don't know how to get it to do what I want at this point.
Yawar, nothing is happening when I run the form but that makes sense as I can't force the rowsource to choose between two possibilities.

I think you need the following code in VBA:
Private Sub chkTrue_AfterUpdate()
If chkTrue =TRUE then
listbox.RowSource = "SELECT Initial FROM qryPDRRate"
Else
listbox.RowSource = "SELECT Renewal FROM qryPDRRate"
End If
End Sub

I was able to use the Query Builder in the RowSource Property using an IIf statement. The RowSource Data Looks as such.
SELECT IIf([Forms]![frmPDPRate]![chkTrue],[qryPDPRate]![Initial],[qryPDPRate]![Renewal]) AS Rate FROM qryPDPRate;

Related

Trying to populate two comboboxes based upon selection of another combobox

I've been searching this issue on Microsoft, Stackoverflow, techinthenet.com and several others, with no real solution that I can find.
I am trying to get two comboboxes (cboCourse and cboVols) to populate based on the selection of cboTrainee_Name. The code for the first combobox is:
SELECT DISTINCT [qryBooks].[PName] FROM qryBooks WHERE qryBooks.Complete=No ORDER BY [qryBooks].[PName];
Everywhere I've searched gives assistance and samples to populate the first combobox, then the second combobox based upon what is selected in the first. I need both cboCourse and cboVols to populate based on the result of the same query. What I have so far is:
Private Sub cboTrainee_Name_AfterUpdate()
cboCourse.RowSource = "SELECT DISTINCT [qryBooks].[Book] FROM qryBooks WHERE [qryBooks].[PName]=[Forms]![frmIntro]![cboTrainee_Name] ORDER BY [qryBooks].[Book];"
cboVol.RowSource = "SELECT DISTINCT [qryBooks].[Vols] FROM qryBooks WHERE [qryBooks].[PName]=[Forms]![frmIntro]![cboTrainee_Name] ORDER BY [qryBooks].[Vols];"
End Sub
This works for cboCourse, but cboVols will not populate. I know there has to be a simple explanation. Any suggestions?
ADDED! I used SELECT DISTINCT because each column in the query contains multiple instances of the same data.
I don't see any problems with your SQL.
You might need to call the Requery method on both combo boxes to get them to refresh after cboTrainee_Name is changed:
cboCourse.Requery
cboVol.Requery
Note that you don't have to set the RowSource property in code for this to work. You should be able to plug in your SQL statements as is in the RowSource property and just the Requery method on the dependent combo boxes in the AfterUpdate event of your cboTrainee_Name combo box.

Populating listbox from another listbox selection

I am pretty much a newbie to using VBA in Access and I'm having trouble with something that seems like it should be quite simple.
I have two listboxes (called LB1_ID and LB2_ID) on my form (MainForm) that I want to list related IDs from their respective Row Sources. I need LB2 to be populated based on the selection in LB1. They both have Row Sources from the same Table (Table1) and it is a many to many relationship of Requirement IDs ("Req ID1" and "Req ID2"). My current form, which is not working, has the Row Source of LB1 as:
SELECT Table1.ID, Table1.[Req ID1] FROM Table1 ORDER BY Table1.ID;
and the Row Source of LB2 as:
SELECT Table1.ID, Table1.[Req ID2] FROM Table1 WHERE ([Forms]![MainForm]![LB1_ID]=Table1.[Req ID1]);
When I make a selection in LB1, nothing happens in LB2. The column widths are formatted correctly and I can get it to work if I use Me.[Forms]![MainForm]![LB1_ID] but I have to type out the LB1 selection manually in a popup box if I use that.
What am I missing?
If your listbox is multi-select, you cannot use a simple form reference as query criteria. If it is not multi-select, keep in mind that its value may be a hidden column (usually an ID field), so there are two possible issues and solutions:
Possible Issues:
Single-Select listbox has an ID field that is hidden (column width = 0") and you are matching it to the wrong field in your table. To check the output of the listbox, open the VBE and type ?[Forms]![MainForm]![LB1_ID] into the immediate window and press enter when your form is open in form view and a row is selected in LB1_ID. If the returned line is what you expect, then the problem must be elsewhere.
Multi-Select listbox property is enabled. In this case, your query will not work, because the listbox will only return Null. You will need to write some VBA to loop through the rows and figure out which ones are selected, which is a bit of a pain. Ultimately you'll build some code that will alter your query with the specific criteria for each selected row. Instead of explaining here, take a look at this article for a tutorial.
The .Requery method is still important to put in the AfterUpdate event of your first listbox to refresh the second.
Your query seems to work, but you need to refresh your listbox2 whenever you make selection into listbox1, so if both listbox are in the same form add this event handler :
Private sub LB1_ID_Change()
Me.LB2_ID.Requery
End sub
Without this, your listbox2 will only get populated once on load based on the initial value of listbox1.
Also, if you have not already done it, I would recommend to add your listbox1 control as a parameter into your listbox2 query (in query builder, right click -> parameters).

Microsoft Access 2010 filtering data based on tempvar

i have a web database and im trying to filter a datasheet, based on the contents of a tempvar. Im trying to use the record source property of the datasheet to do this.
I need to do this because, every employee that logs in should only be able to see a given subset of data in the products table. In the employee table, i have an extra column with a string value which is the data that particular employee should see.
I have a login form that on clicking login, adds this string to the tempvars collection.I can see the tempvar has been added in the immediate window as shown below:
?tempvars!tmpgrpdsc -> "IAMS"
i use the query builder option to complete the record source property as shown below.
The problem is, nothing is returned !
But when i enter the string "IAMS", i get records returned.
However, i have done this with another datasheet and it has worked, the tempvar here held a number ! See below:
What am i missing or is there a better way to filter records based on the login. Thanks
What you showed should work.
However, have you tried to change the criteria to ="""" & [Tempvars]![tmpGrdsc] & """"
Also, to make sure that your tempvar is actually containing the data during the query, you could show it as a field, just to check exactly what data is being returned during the query:
SELECT Orders.*,
[Tempvars]![tmpGrdsc] AS TmpGrdsc
FROM Orders

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.

How do I get the values of the RecordSource in an Access combobox

I have a form in Access with a combobox on it.
The combobox gets the displayed values through an SQL select statement by
means of the RowSource property which is set to something like
select description, id_of_table, col_foo from tblTable
Since I usually need the selected id_of_table, the property BoundColumn is set to 2. So, I can access this value (for example in an AfterUpdate Event) like
var_id_of_table = me.cboWhatever
which works with no problems.
Now, at times, I do not only need id_of_table but also the value of col_foo. So, the question is, how would I go about getting this desired value. I thought I could get it by
something like
var_foo = me.cboWhatever("col_foo")
but it didn't work.
I'd appreciate any input on this matter.
Thanks / Rene
You need:
var_foo = me.cboWhatever.column(n)
Where n is the column number starting from 0.