SQL to include condition in Where if not null

隐身守侯 提交于 2019-11-29 13:11:27

If I understand correctly, you have a form with several unbound controls that the user will fill in. You, then want to filter your subform according to what the user has entered. In that case, the method you are trying to use will become very complex and difficult to maintain, not to mention slow with a lot of data. The best bet is to build the filter in code. What I often do is name the controls the same as the field name and put the field type in the control's tag property. Then loop through the controls to build the filter. This is air code, but should give you a start. Dim strFilter as string Dim ctL as Access.control For Each ctL in Me.Controls If Not ISNull(ctL) And ctL.Tag <> "" Then strFilter = strFilter & " AND " & ctl.Name & " = " If ctL.Tag = "Text" Then strFilter = strFilter & "'" & Replace(ctl, "'","''") & "'" ElseIf ctL.Tag = "Date" Then strFilter = strFilter & "#" & ctl & "#" Else strFilter = strFilter & ctlEnd If Next If strFilter <> "" Then strFilter = mid(strFilter, 6) strFilter = " Where " & strFilter End If Me.yourSubForm.Form.Recordsource = "Select * From <yourQuery> " & strfilter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!