Alright, so I have a table called Colors, within that table I have 5 records (Red, Blue, Green, Yellow and Orange). The color table currently has two fields (ID and Color Name). My overall goal is to randomly select a color and mark this color as being used. Rinse and repeat until all colors are used and them mark all colors as being unused.
Here is the SQL on the RandomColorsQuery:
SELECT TOP 1 Colors.[Color Name]
FROM Colors
ORDER BY Rnd(ColorID);
So far, I've been able to select a random color by using the following within VBA and works fine:
Dim RanColor As DAO.Recordset
Set RanColor = CurrentDb.OpenRecordset("RandomColorsQuery")
'MsgBox (RanColor.Fields(0))
Text1.SetFocus
Text1.Text = RanColor.Fields(0)
Obviously I would need to add a new field to the Colors table, say a field called "Used". I'd rather not use a Yes/No field and just add an "X" in the "Used" field when the color is used.
Any suggestions or similar examples on how to accomplish this?
Add a boolean (Yes/No) field.
Modify query to return only those that haven't been used:
SELECT TOP 1 Colors.[Color Name]
FROM Colors
WHERE USED=False
ORDER BY Rnd(ColorID);
In VBA, if the recordset returns a record, grab your color value, and set Used field to True.
If recordset returns doesn't return a record, ie rs.EOF=True, then update Used field in all records to false, and rerun the query to start over.
Related
I would like to change the row colours in the attached image so all rows match with the value in NAME column.
So I want the related 2 rows in CUST CODE, DEL TO, CUST NAME and then the related 3 rows in ORDER NO to be the same colour.
Then i would like the colours to alternate for the next NAME value and so on.
Is this possible? I know how to alternate row colour when each result is one row only, but unsure how to do it with this kind of result.
So to clarify, for NAME (AHe) all rows to be 'lightgrey', NAME (AHO) all rows to be 'whitesmoke', NAME (JH) all rows to be 'lightgrey' etc...
I've used something similar to the following in the background color expression:
=IIF(RUNNINGVALUE(Fields!Name.Value, COUNTDISTINCT,"MainDataSet") MOD 2 = 0,
"LightGrey",
"WhiteSmoke")
There may be another way to do this using ROWNUMBER() or another alternative as well.
A SQL Table has a field with the name:
image_colors
The value of this field can be a row of different colors - example:
green red blue white
When I search for a specific color, I use:
SELECT *
FROM `gallery_images`
WHERE `image_colors` LIKE '%green%'
In this case, the value:
green red blue white
contains green and will be selected.
Question:
Is it possible with SQL to find only values with only 1 Word:
green
Yes, use simple equality comparison to select only values matching green:
select *
from gallery_images
where image_colors = 'green'
Notes:
backticks are not necessary in your case and should be avoided when not needed
you should probably change your data model for many-to-many with colors dictionary table and junction table between gallery_images and colors to normalize your data and make lookups faster
Try to use
SELECT *
FROM gallery_images
WHERE trim(image_colors)= 'green'
Your existing query will work for single word also. No need to do any change for single word value in search field.
But of course this query will degrade performance of your application.
Instead of storing multiple values in single column, it is better to create another table to store colors in form of Integer value as multiple rows.
Do not use Like operator when you want records with only 'green' color.
select *
from gallery_images
where image_colors = 'green'
I have a contact form and one of the fields in a the form is a Contact_Type_ID. This field is a number field which also corresponds to a text field in another table (e.g. 1 = expatriate).
When I cycle through the contacts, their Contact_Type_ID is 1, 2, 3... instead of Non-profit, CEO, Vice-president, etc. This is a problem because one has no idea what number 3 means.
I would like to a combobox that only displays the corresponding text.
I can't get the two columns and 0;1 format to work. My hunch is that it's because I'm drawing information from two different tables. I can generate the correct list, but then the main entry doesn't change as I cycle through the contacts to reflect the current contact's [Contact_Type_ID].
I can't edit any of the current tables because I am supposed to apply this application to a much larger scale database.
I also tried setting the SQL for the row source:
'Populate the connection combo box '
Dim typeSQL As String
typeSQL = "SELECT DISTINCT Contacts.[ContactTypeID], Contact_Types.[ContactType] " & _
"FROM Contacts, Contact_Types " & _
"ORDER BY Contact_Types.[ContactType];"
Me.cbo_ContactType.RowSource = typeSQL
However, I then have the same problem: the combobox won't update as I cycle through the contacts. I don't understand the difference between the rowsource and the controlsource. I feel that this distinction might be key here.
Assuming I understand correctly:
In the combo box properties, go to the Data tab.
Set the Control Source to Contact_Type_ID.
This means that when you go through the records, the combo box will correspond to the Contact_Type_ID field from your data.
Set the Row Source to "SELECT Contacts.[ContactTypeID], Contact_Types.[ContactType] FROM Contacts, Contact_Types ORDER BY Contact_Types.[ContactType];"
The Row Source indicates the data that the combo box has access to, which will help it determine how to display the control source value.
Set the Bound Column to 1
Set Limit to List to Yes
Now in the Format tab. Change Column Count to 2.
Next set the column widths to the 0;1 you mentioned in your question.
Now try looking at form view, and it should behave as you expected.
If it does not work, create a new control with these instructions.
As I understand your question, you have a form with contacts based on a table Contacts and that table contains a field called either Contact_Type_ID or ContactTypeID. You wish to display the description of the contact type from a table Contact_Types.
You do not need to join tables for your combo, it should be very simple. The key, as you suspected, is the Control source, which is the property that relates one table to another.
Control source : ContactTypeID '' or Contact_Type_ID, choose from the list
Row source : SELECT ContactTypeID, ContactType FROM Contact_Types ORDER BY ContactType
Bound column : 1
Column count : 2
Column widths : 0,2
As an aside, you can join tables in a combo and still set the first column to 0 and it will still work, it is all in the correct set up.
I need the drop down box above to display the date as shown and the ClassTypeDesc as you can see above the dropdown list shows 1/12/2010 twice. They have different ClassTypes Assigned to them.
The drop down list should show:
1/12/2010 ACLS-I Day One AM
1/12/2010 ACLS-I Day One PM
I need to know the statement to put in the Row Source Box on the lookup tab in the Field Properties to make this work.
Related Question on Making a drop down list
There's no need to concatenate the two columns. Based on the diagram, the SQL for your lookup combo box should look like this:
SELECT tblClassSession.SessionID, tblClassSession.Date, tblSessionType.ClassTypeDesc
FROM tblClassSession INNER JOIN tblSessionType
ON tblClassSession.SessionTypeID = tblSessionType.SessionTypeID;
Then in the properties for your lookup combo box, change ColumnCount to 3, and Column Widths to 0 (if you want to size the other columns, change Column Width sto something like 0";.75";1.5", and the List Width property to 2.25").
I may have gotten some of the field names wrong, but that's the basic idea.
(also, you probably really ought to rename tblClassSession.Date to tblClassSession.SessionDate so you don't run into problems with the fact that Date is a reserved word)
As you already have the right number of rows, you just need to concatenate enough fields to make it more useful, so your query would be something like:
SELECT c.Date + ' ' + s.ClassTypeDesc AS YourFieldName
FROM...
What is the purpose of BOUND COLUMN property of listbox?
The bound column is a number that represents what column from the row source will be used to set the value of the Control Source (if the list box is bound).
Note that you can’t use a column name here. So you don't set the bound column to a column name, but you must use a column number.
Another issue here is that the column number starts at 1 (not zero). Note that OFTEN the 1st column length is set to zero. That allows you to have a list box with something like
select PartNumber, PartDescripton from tblParts
The list box will display the part description, but if you set the bound column = 1, then the listbox will return the PartNumber despite the fact that the listbox is displaying Descriptions (since you set the length of the 1st column = 0. If you set the bound column = 2, then the listbox will return description. Note that you can grab any column value from the list box by using
([lstBox1].Column)
Note that in the above, the column feature is zero based. So, 1 = 2nd column
It's the column of the data set that is used to set the value of the listbox. For example, if it's bound to a dataset with the query:
select firstname,lastname,userid from users;
then setting the bound column to userid (3 in the above example) will cause the user ID information to be returned as the listbox value.
A bound column is the data that the form is going to save. For instance, if you have a list box or combo box that lists employeeID and employeeName and you set the bound column to 0, the form will save the employee ID number from
the selection and insert that value into the corresponding table.
You can test which value you are referencing this using this vba:
Private Sub ComboBoxName_AfterUpdate()
MsgBox ("bound column is: " & Me.ComboBoxName.BoundColumn & ". value is: " & Me.ComboBoxName.Column(0))'change 0 to whatever number column is bound
End Sub
The bound column rule applies even if the first column is hidden on the form. For instance, the user could select "Mike Jones" from the employee list, but the form is going to save Mike Jones' employeeID for data uses (this ID could be stored in a table of sales records, etc.).