access中,怎样实现如图查询。文本框中输入条件,下表列出满足条件内容?我初学,希望详细步骤,谢谢
1个回答
展开全部
这里提供详细实现方案供参考
首先假设主窗体的四个文本框、两个命令按钮和子窗体的名称分别是:
车牌号码,维保内容,维保厂家,维保日期,Command1,Command2,子窗体1
主窗体名:维保查询
假设数据表结构为:
维保记录(ID,车牌号码,维保内容,维保厂家,维保日期)
将子窗体1的记录源属性设置为:
select * from 维保记录 where false;
目的是让子窗体于窗体打开时暂时不显示记录
'为查询命令按钮编写下列单击事件过程:
Private Sub Command1_Click()
Dim strSql As String
'因为有4个条件框可产生2^4=16种查询组合,
'下面用代码一次性将这16种查询方式的SQL全部拼接起来,
'从而实现输入什么条件就按什么条件查询相关记录
strSql = "select * from 维保记录 where "
'四个条件均为空时输出所有维保记录
strSql = strSql & "(Forms!维保查询!车牌号码 is null and "
strSql = strSql & "Forms!维保查询!维保内容 is null and "
strSql = strSql & "Forms!维保查询!维保厂家 is null and "
strSql = strSql & "Forms!维保查询!维保日期 is null) or "
'只有“维保日期”一个条件时,单独按“维保日期”精确筛选
strSql = strSql & "(Forms!维保查询!车牌号码 is null and "
strSql = strSql & "Forms!维保查询!维保内容 is null and "
strSql = strSql & "Forms!维保查询!维保厂家 is null and "
strSql = strSql & "Forms!维保查询!维保日期 is not null and "
strSql = strSql & "维保日期=Forms!维保查询!维保日期) or "
'只有“维保厂家”一个条件时,单独按“维保厂家”模糊查找
strSql = strSql & "(Forms!维保查询!车牌号码 is null and "
strSql = strSql & "Forms!维保查询!维保内容 is null and "
strSql = strSql & "Forms!维保查询!维保厂家 is not null and "
strSql = strSql & "Forms!维保查询!维保日期 is null and "
strSql = strSql & "维保厂家 Like '*' & Forms!维保查询!维保厂家 & '*') or "
'双条件,按维保厂家模糊查找、维保日期精确筛选
strSql = strSql & "(Forms!维保查询!车牌号码 is null and "
strSql = strSql & "Forms!维保查询!维保内容 is null and "
strSql = strSql & "Forms!维保查询!维保厂家 is not null and "
strSql = strSql & "Forms!维保查询!维保日期 is not null and "
strSql = strSql & "维保厂家 Like '*' & Forms!维保查询!维保厂家 & '*' and "
strSql = strSql & "维保日期=Forms!维保查询!维保日期) or "
'只有“维保内容”一个条件时,单独按“维保内容”模糊查找
strSql = strSql & "(Forms!维保查询!车牌号码 is null and "
strSql = strSql & "Forms!维保查询!维保内容 is not null and "
strSql = strSql & "Forms!维保查询!维保厂家 is null and "
strSql = strSql & "Forms!维保查询!维保日期 is null and "
strSql = strSql & "维保内容 Like '*' & Forms!维保查询!维保内容 & '*') or "
'双条件,按维保内容模糊查找、维保日期精确筛选
strSql = strSql & "(Forms!维保查询!车牌号码 is null and "
strSql = strSql & "Forms!维保查询!维保内容 is not null and "
strSql = strSql & "Forms!维保查询!维保厂家 is null and "
strSql = strSql & "Forms!维保查询!维保日期 is not null and "
strSql = strSql & "维保内容 Like '*' & Forms!维保查询!维保内容 & '*' and "
strSql = strSql & "维保日期=Forms!维保查询!维保日期) or "
'双条件,按维保内容模糊查找、维保厂家模糊查找
strSql = strSql & "(Forms!维保查询!车牌号码 is null and "
strSql = strSql & "Forms!维保查询!维保内容 is not null and "
strSql = strSql & "Forms!维保查询!维保厂家 is not null and "
strSql = strSql & "Forms!维保查询!维保日期 is null and "
strSql = strSql & "维保内容 Like '*' & Forms!维保查询!维保内容 & '*' and "
strSql = strSql & "维保厂家 Like '*' & Forms!维保查询!维保日期 & '*') or "
'3条件,按维保内容模糊查找、维保厂家模糊查找、维保日期精确筛选
strSql = strSql & "(Forms!维保查询!车牌号码 is null and "
strSql = strSql & "Forms!维保查询!维保内容 is not null and "
strSql = strSql & "Forms!维保查询!维保厂家 is not null and "
strSql = strSql & "Forms!维保查询!维保日期 is not null and "
strSql = strSql & "维保内容 Like '*' & Forms!维保查询!车牌号码 & '*' and "
strSql = strSql & "维保厂家 Like '*' & Forms!维保查询!维保厂家 & '*' and "
strSql = strSql & "维保日期=Forms!维保查询!维保日期) or "
'只有“车牌号码”一个条件时,单独按“车牌号码”精确筛选
strSql = strSql & "(Forms!维保查询!车牌号码 is not null and "
strSql = strSql & "Forms!维保查询!维保内容 is null and "
strSql = strSql & "Forms!维保查询!维保厂家 is null and "
strSql = strSql & "Forms!维保查询!维保日期 is null and "
strSql = strSql & "车牌号码=Forms!维保查询!车牌号码) or "
'双条件,按车牌号码和维保日期精确筛选
strSql = strSql & "(Forms!维保查询!车牌号码 is not null and "
strSql = strSql & "Forms!维保查询!维保内容 is null and "
strSql = strSql & "Forms!维保查询!维保厂家 is null and "
strSql = strSql & "Forms!维保查询!维保日期 is not null and "
strSql = strSql & "车牌号码=Forms!维保查询!车牌号码 and "
strSql = strSql & "维保日期=Forms!维保查询!维保日期) or "
'双条件,按车牌号码精确筛选、维保厂家模糊查找
strSql = strSql & "(Forms!维保查询!车牌号码 is not null and "
strSql = strSql & "Forms!维保查询!维保内容 is null and "
strSql = strSql & "Forms!维保查询!维保厂家 is not null and "
strSql = strSql & "Forms!维保查询!维保日期 is null and "
strSql = strSql & "车牌号码=Forms!维保查询!车牌号码 and "
strSql = strSql & "维保厂家 Like '*' & Forms!维保查询!维保厂家 & '*') or "
'3条件,按车牌号码精确筛选、维保厂家模糊查找、维保日期精确筛选
strSql = strSql & "(Forms!维保查询!车牌号码 is not null and "
strSql = strSql & "Forms!维保查询!维保内容 is null and "
strSql = strSql & "Forms!维保查询!维保厂家 is not null and "
strSql = strSql & "Forms!维保查询!维保日期 is not null and "
strSql = strSql & "车牌号码=Forms!维保查询!车牌号码 and "
strSql = strSql & "维保厂家 Like '*' & Forms!维保查询!维保厂家 & '*' and "
strSql = strSql & "维保日期=Forms!维保查询!维保日期) or "
'双条件,按车牌号码精确筛选、维保内容模糊查找
strSql = strSql & "(Forms!维保查询!车牌号码 is not null and "
strSql = strSql & "Forms!维保查询!维保内容 is not null and "
strSql = strSql & "Forms!维保查询!维保厂家 is null and "
strSql = strSql & "Forms!维保查询!维保日期 is null and "
strSql = strSql & "车牌号码=Forms!维保查询!车牌号码 and "
strSql = strSql & "维保内容 Like '*' & Forms!维保查询!维保内容 & '*') or "
'3条件,按车牌号码精确筛选、维保内容模糊查找、维保日期精确筛选
strSql = strSql & "(Forms!维保查询!车牌号码 is not null and "
strSql = strSql & "Forms!维保查询!维保内容 is not null and "
strSql = strSql & "Forms!维保查询!维保厂家 is null and "
strSql = strSql & "Forms!维保查询!维保日期 is not null and "
strSql = strSql & "车牌号码=Forms!维保查询!车牌号码 and "
strSql = strSql & "维保内容 Like '*' & Forms!维保查询!维保内容 & '*' and "
strSql = strSql & "维保日期=Forms!维保查询!维保日期) or "
'3条件,按车牌号码精确筛选、维保内容模糊查找、维保厂家模糊查找
strSql = strSql & "(Forms!维保查询!车牌号码 is not null and "
strSql = strSql & "Forms!维保查询!维保内容 is not null and "
strSql = strSql & "Forms!维保查询!维保厂家 is not null and "
strSql = strSql & "Forms!维保查询!维保日期 is null and "
strSql = strSql & "车牌号码=Forms!维保查询!车牌号码 and "
strSql = strSql & "维保内容 Like '*' & Forms!维保查询!维保内容 & '*' and "
strSql = strSql & "维保厂家 Like '*' & Forms!维保查询!维保厂家 & '*') or "
'4条件,按车牌号码精确筛选、维保内容模糊查找、维保厂家模糊查找、维保日期精确筛选
strSql = strSql & "(Forms!维保查询!车牌号码 is not null and "
strSql = strSql & "Forms!维保查询!维保内容 is not null and "
strSql = strSql & "Forms!维保查询!维保厂家 is not null and "
strSql = strSql & "Forms!维保查询!维保日期 is not null and "
strSql = strSql & "车牌号码=Forms!维保查询!车牌号码 and "
strSql = strSql & "维保内容 Like '*' & Forms!维保查询!维保内容 & '*' and "
strSql = strSql & "维保厂家 Like '*' & Forms!维保查询!维保厂家 & '*' and "
strSql = strSql & "维保日期=Forms!维保查询!维保日期);"
Me!子窗体1.Form.RecordSource = strSql '输出查询结果
End Sub
'为清除命令按钮编写下列单击事件过程:
Private Sub Command2_Click()
车牌号码 = Null
维保内容 = Null
维保厂家 = Null
维保日期 = Null
Me!子窗体1.Form.RecordSource = "select * from 维保记录 where false;"
End Sub
题主请按自己窗体和数据表的实际情况自行调整上述代码中涉及的窗体、控件、数据表和字段名。由于代码很长,要细心调整,不要出错漏,否则查错会比较困难。上述代码已经测试无误,上机试一试效果吧。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询