How to display the selected records

2019-09-24 12:13发布

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?

标签: sql vb6
1条回答
劳资没心,怎么记你
2楼-- · 2019-09-24 12:27

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.

查看更多
登录 后发表回答