Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

sql - How to display the selected records

Using VB6

Listbox

checkbox EmpID

Selected 001
unSelected 002
Selected 003
....
....

I want to view the records belonging only to selected employees.

Query

Select * from table where empid = "checkbox selected employees"

Expected Output

EmpID Name Dept.

001 Rajan IT 
003 Vijayan Accounts

What code do I need to select multiple employees in the list box?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do this by building up a WHERE condition.

As the final SQL needs to be something along these lines:

SELECT EmpID, Name, Dept FROM Employees WHERE EmpID='001' OR EmpID='003';

Or, if your Database supports it:

SELECT EmpID, Name, Dept FROM Employees WHERE EmpID IN ('001', '003');

You just need to go through all your checkboxes and create the string using something like:

'Find each checked item
For Index = 0 to CheckListBox.ListCount - 1
  If CheckListBox.Selected(Index) Then
    'Append to an ID list string
    If IDList <> "" Then IDList = IDList & ", "
    IDList = IDList & "'" & Format(CheckListBox.ItemData(Index), "000") & "'"
  End IF
Next

'Create the final SQL statement
If IDList <> "" Then 
  Query = "SELECT EmpID, Name, Dept FROM Employees WHERE EmpID IN (" & IDList & ");"
End If

Being any more specific than this is difficult without knowing what Database engine and library you're using, the checkbox control structure, or the database schema.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...