SQL Server query for a VB dropdown list - sql

I am working on administering an "erp type" VB program which is running on SQL Server 2008 R2. This program allows users to have a card for each file stored in its vault. Those cards are built into administration tools and u can add elements like comboboxes, dropdownlists etc. You can also add an SQL query into a dropdown list. So i have these 2 tables in SQL Server database:
USERS:
USERID / NAME / USERGROUPID
and
USERGROUPS:
USERGROUPID / USERGROUPNAME
I want to add 2 dropdown lists in my cards connected to SQL with query but i want the 2nd one to be connected with the selection from the 1st one. The 1st one asks for the usergroup name and the 2nd one asks for the name in the previously selected usergroup.
I found out that the 1st dropdown list should be:
SELECT USERGROUPNAME
FROM USERGROUPS
The 2nd one should be like:
SELECT NAME
FROM USERS
INNER JOIN USERGROUPS ON USERS.USERGROUPSID = (USERGROUPS.USERGROUPID FROM PREVIOUS SELECTION)
How can I modify the 2nd query in order to get the data I need?

You can use the following query for second one. It will select all NAME that matches with the GroupName you selected
SELECT NAME
FROM USERS usrs
INNER JOIN USERGROUPS usrgrps ON usrs.USERGROUPSID=usrgrps.USERGROUPID
WHERE usrgrps.USERGROUPNAME='VALUE FROM DROPDOWN 1';

Related

How can I make a SQL (Query) that is set as a record source in a form to be called once?

Background: working with MS Access as a front end and SQL Server as a back end to organize data of every student found in a school.
Well I've been working on MS Access forms and I've set the record source as a query and set the intended text boxes , combo boxes , etc... to the property that was received.
Now the problem is that the queries for every student is being executed while on the form load or changes from one record to the other. I've come to this conclusion by viewing the SQL profiler and noticed that there is a large amount of queries being executed (It take approx. 14 seconds to load one record).
Is there a way to go around this?
Here is the SQL script that is being used to retrieve the the data (Set as a record source on the form itself).
SELECT
PERSON.id, PERSON.id_number,
Student.Student_Status, PERSON.name,
PERSON.surname, PERSON.dob, PERSON.address_1, PERSON.address_2,
PERSON.town, PERSON.mobile, PERSON.telephone, PERSON.postcode,
PERSON.id, PERSON.nationality, PERSON.dual_nationality,
PERSON.gender, Student.student_type, Student.mcast_email,
PERSON.euCitizen, PERSON.email, PERSON.NI_no,
PERSON.next_of_kin, Student.Form, course.course_name,
course.course_code, CourseYear.Year, Institute.Institute_name
FROM
((((PERSON
LEFT JOIN
Student ON PERSON.ID = Student.person)
LEFT JOIN
CourseYear ON Student.id = CourseYear.student)
LEFT JOIN
Yearlyprogramme ON CourseYear.course = Yearlyprogramme.id)
LEFT JOIN
course ON Yearlyprogramme.course = course.id)
LEFT JOIN
Institute ON course.Institute = Institute.id;
These properties that are retrieved are set to the text boxes and and combo boxes to show data. Is there a way to call this query once for each student without having to the a WHERE clause on the ID?
In my experience, Access queries with multiple LEFT JOINs on linked SQL Server tables often behave very badly.
If you can't avoid the LEFT JOINs, create a SQL Server view from your query, link it in Access, and use the linked view as record source of your form.

Crystal Reports- Passing one parameter value to multiple database fields

I have a table in the database that has 10 application fields (App#0 to App#9) and their corresponding application versions (appversion#0 to appversion#9)
I have created a new parameter using dynamic cascading that prompts the user to Select an Application: and select the corresponding Application Version:
The value for this fields are taken from App#0 and Appversion#0 respectively.
I want to select all records in the database where application corresponds to the selected App and Appversion. So in the select expert I have used this parameter and it shows me the correct records.
The problem now is- The app that user selects can be in any of the database fields from app#0 to app#9. It is not necessary that it will always be in the app#0 field. So, I want crystal report to check all the database fields from app#0 to app#9 with the parameter that the user selects. How can I do that without creating multiple parameters?
Select Formula expert query
SELECT issue1.reference_id, Software_Products_Anomaly1.product, issue1.description, issue1.last_mod_date, issue1.owner_id,
Software_Products_Anomaly1.application0, Software_Products_Anomaly1.application_version0, Software_Products_Anomaly1.priority, state1.name,
FROM
(splatt.issue issue1
INNER JOIN splatt.Software_Products_Anomaly Software_Products_Anomaly1
ON issue1.id=Software_Products_Anomaly1.issue_id
)
INNER JOIN splatt.state state1
ON issue1.status=state1.id
WHERE
( state1.name='assigned'
OR state1.name='open'
OR state1.name='reassigned'
OR state1.name='submitted'
)
AND issue1.owner_id<>113
AND Software_Products_Anomaly1.product='PHX'
AND
( Software_Products_Anomaly1.application0='Package1' AND Software_Products_Anomaly1.application_version0<='2.2.2.2')
ORDER BY Software_Products_Anomaly1.priority

MS Access 2013 - How do I select unique values from another table?

I have a database with three tables; tbl_Room, tbl_Guest and tbl_RoomGuest (a bit simplified).
`tbl_Room` has information about a certain room
`tbl_Guest` has information about a certain guest
`tbl_RoomGuest` has information about what guest stayed at which room,
and the dates they stayed.
I am making a form where I can enter information about a room, and I want to display a list of the guests that have stayed in that room.
How can I select the names from tbl_Guest, when I want to display unique names of only the guests that stayed in that room?
I want the field to be non-editable.
The query to get the unique names could be written using distinct. Here is an example:
select distinct g.GuestName
from tbl_RoomGuest as rg inner join
tbl_Guest g
on rg.GuestId = rg.GuestId
where rg.RoomId = YOURROOMIDHERE;

how can I do this select query

I have in my SQL Server, a table called FavoriteUsers with a list of users and a table called Users with all users.
In my ASPX page, I have a TextBox where my user can put the ID of an User to list in my GridView.
How it works
By default, I fill my GridView with all data of my FavoriteUsers table, and if the user of my system put some user code in the textbox field and click on my button to find these users, I'll reload the GridView with the FavoriteUsers and the users that he put on TextBox field that come from the table Users.
How can I do this SELECT query ?
select * from FavoriteUsers
UNION
select * from Users
Where userid = 'TextBox.text'
Of course you can't use TextBox.text in there. But you only needed the query so you get the point.
NOTE: The UNION will leave out any records from Users that already exist in FavoriteUsers. So you won't have duplicates.
Get all favorite users plus the user whose username matches that entered into the textbox:
SELECT Users.*
FROM Users
LEFT JOIN FavoriteUsers ON Users.UserID = FavoriteUsers.UserID
WHERE (FavoriteUsers.UserID IS NOT NULL) OR (Users.Username = #Username)

How to show values from two different queries?

I have one database that contains all of user information including name. Then there is a second database that contains notes from the users and it contains the #id but not the name. The query i am doing to retrieve user notes doesn't have name so all its doing is showing the notes, then right under it i am doing another query to retrieve the name from the first database using the common #id. But it won't show.
Is there a way I can do this query in one? Please help. Thanks.
Use:
SELECT u.name,
n.*
FROM DB2.NOTES n
LEFT JOIN DB1.USERS u ON n.id = u.id
ORDER BY u.name
Assuming the connection credentials has access to both databases, you prefix the database name in front of the table name and separate with a period.
The LEFT JOIN will show both users, and notes without users associated. Here's a good primer on JOINs.
You might need to show your code, but you can write queries against two databases (or schemas) on the same host, just qualify the table names with the database name, e.g.
SELECT db1.user.id, db1.user.name, db2.userinfo.notes
FROM db1.user
INNER JOIN db2.userinfo ON(db1.user.id=db2.userinfo.id)
The credentials you are connecting with must have access to both databases for this to work of course.