如何显示选中的记录 [英] How to display the selected records
问题描述
使用 VB6
列表框
checkbox EmpID
Selected 001
unSelected 002
Selected 003
....
....
我想查看仅属于选定员工的记录.
I want to view the records belonging only to selected employees.
查询
Select * from table where empid = "checkbox selected employees"
预期输出
EmpID Name Dept.
001 Rajan IT
003 Vijayan Accounts
在列表框中选择多个员工需要什么代码?
What code do I need to select multiple employees in the list box?
推荐答案
您可以通过建立 WHERE 条件来做到这一点.
You can do this by building up a WHERE condition.
因为最终的 SQL 需要遵循以下几点:
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.
这篇关于如何显示选中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!