How to display multiple fields in a list box? - sql

What I am trying to accomplish is a single list box with two columns. In each column, results from a field will be displayed. Take this query for example:
SELECT FirstName, LastName FROM Names WHERE FirstName = 'Tom'
The list box would have two columns, with the first column holding all first names selected from the query. The second column would have all last names selected from the query, but each column would be matching. Ie. in the database, a "Tom A" record would not be mixed with a "Tom B" record when displayed in the list box columns.
I've tried searching before, but all I can find is using listbox.AddItem, which will not work in my case since I am querying a database to populate the listbox.

Put that SQL in the RowSource property of the list box. Make sure your Column Count property is set to 2, and that the Column Width property is set to a value. Empty values or "0" will result in the column not being seen. So, your Column Widths property should be set to "2; 2" (without the quotes, and you can adjust those as necessary).

Related

PostgreSQL/Metabase: How to create variable to filter by multiple numbers

I would like to filter by multiple ids (e.g. item ids of purchases). The following line of code does not seem to do the trick either for number or text variables (e.g. user names). How should I write this line of code so in the filtering field I could type my values separated by a comma and look for entries that contain either of the item ids I have provided? How would that line of code differ if wanting to apply a filter by a text variable instead (i.e. user name)?
[[where "User name" like concat('%',{{user_name}},'%')]]
Use a field filter parameter, mapping it to the id column and it's respective table
Then structure your query like below:
select * from profiles where user_name ({{PARAM}})
Documentation: https://www.metabase.com/learn/sql-questions/field-filters

SQL check if row value in one column is in another column that is a list of strings

I am pulling from a table that looks something like this:
Name
List
John
'John,Mary,Fred'
Mary
'Jack, John'
I need to pull only the rows where the value in the Name column is in the comma-separated list of names in the List column. In the example above, it would return just the first row. How can I do this using SQL (AWS Redshift version)?
This work?
SELECT *
FROM <table>
WHERE list LIKE '%'||name||'%'
This has a hazard that 'jon' will match 'jonas'. You can fix this will a bit extra conditions. Also do you need this to be case insensitive? Then use ILIKE
If you need to match complete names you need to cover the 4 cases of how a name can show up in a list - first in list, middle of list, end of list, or all of the list. Like so:
SELECT *
FROM <table>
WHERE list LIKE name||',%'
OR list LIKE '%,'||name||',%'
OR list LIKE '%,'||name
OR list LIKE name

VB.net windows application retrieve values based on multiple unique row values

I am working on VB.net Windows application. I have a database table with unique Employee Ids. If the user enters multiple employee Id in text box.I want to display the result of the query with text box input( Ex 001, 002,003) concatenated with where IN query to display the result into gridview.
Example:
Select * from Employees where Employeeid IN(textbox values)
You can use directly the values from your text box if the value in the text box are comma separated and the field is numeric in the table.
sql = "Select * from Employees where Employeeid IN("+ textbox.text +")"
If the user dint add comma separated then you can use Replace function
sql = "Select * from Employees where Employeeid IN( "+ textbox.text.Replace(" ",",") +")"
If your EmployeeId column is not numeric then you have to use single quotes with every value entered in the text box. i.e In('121','123','124')
Please do not do what you are doing. It is incredibly vulnerable to SQL Injection. If you do not know what this is, then please Google it. What would happen if someone entered in your TextBox "1,2); DELETE FROM Employees WHERE 1 IN (1,2"?
If you must allow multiple selection it is much better to do so via e.g. a ListBox showing all possible values, with SelectionMode MultiExtended. You are then in control of arranging the choices into a safe SQL command to send to the database.
Edit
As a secondary point, if your employee ids are in the form 001, 002, 003 etc then they are some form of string/varchar/text, in which case all the elements in the IN () will need to be surrounded by single quotes. Again far easier not to rely on your users knowing this.

Using Lookup in MS Access Forms

I have a MS Access Form that has Student ID,
This is ok, I can see student ID as it should be
What I want is to display Student Name instead of Student ID
I used SELECT StID, StName From Students in "Row Source"
"Row Source Type" = Table/Query
"Bound Column" = 1
I could not find any property where I can decide the Display Column
Help is much appreciated.
Thanks
Go to the "FORMAT" tab of the Form Properties Dialog Window (the same one you used to set the Row Source and Row Source Type properties)
You'll see two fields (Column Count and Column Widths)
Set Column Count to 2.
Set Column Widths to 0";1"
This works in Access 2007. Any version later then 2007 should also work.
By setting the column count to '2' you're displaying the StudentId and the Name. Then by setting StudentId column width to '0' you are hiding the column.
When you switch to Form View you will see the Student Names, but the value stored underneath is really the StudentId. Neat trick.
Hope this helps.

MS Access Query-By-Form Issue

I have a form (fCen1-20) containing two combo boxes. The first combo box is called Lookup Value and the dropdown contains the field Lookup_Value which serves as the primary key for every table in the database. The second combo box is called Category and the dropdown contains the fields Category, Code, and Table.
I would like for the user to select the Lookup Value and Category and for those selections to inform a query which returns the value of the selected Category for the selected Lookup Value. The complicating factor is that each Lookup Value is associated with over 1500 unique categories of information which are each assigned a unique code -- the code serves as the field name.
For your reference, I have pasted my code, along with my rationale, below:
SELECT [Forms]![fCen1-20]![Category 1].Code
' Rationale: Get the value for the Code associated with a given category
FROM [Forms]![fCen1-20]![Category 1].Table
' Rationale: Reference the Table where the selected Category/Code is housed
ON [Forms]![fCen1-20]![Category 1].Table.Lookup_Value = _
[Forms]![fCen1-20].[Lookup Value];
' Rationale: Select only those records in the table
' for which the Lookup_Value field matches the Lookup Value
' selected in the form
When I run this code, I'm given a "Syntax error in FROM clause" error. Any suggestions on how to make this work? Please let me know if you'd like any additional detail or clarification. Thanks!
If you use this in a query, it will probably work assuming the form fCen1-20 is open in Form View.
SELECT [Forms]![fCen1-20]![Category 1]
The value returned will be from the bound column of the currently selected combo box row. The fact that [Category 1] includes 3 columns does not matter. The db engine only sees the column which is "bound". (Check the combo's Bound Column property on the Data tab of the combo's property sheet.) The bound value is the only combo value available in a query.
You can not append a column name to the combo name to retrieve the values from those columns, so these will both fail:
[Forms]![fCen1-20]![Category 1].Code
[Forms]![fCen1-20]![Category 1].Table
That was my explanation for why I believe your approach is not working. However, I don't know what to suggest instead. In general, if you use a table's primary key as a combo's bound value, you can use that bound value with a DLookup expression in a query. As an example, assuming all values are numeric ...
SELECT fld1, fld2, etc
FROM YourTable
WHERE some_field = DLookup(
"lookup_field",
"AnotherTable",
"pkey_field = " & [Forms]![fCen1-20]![Category 1]
);
Unfortunately I don't know whether that suggestion is useful for your situation because I don't clearly understand what you're trying to accomplish.