Design an Access Form with Dynamic SQL as Recordsource

核能气质少年 提交于 2019-12-06 14:40:48

With both designs you should build the datasource dynamically, something like this:

sql = "SELECT * FROM MyTableOrQuery WHERE 1=1"
If Not IsNull(textBox1) Then
  sql = sql & " And Field1 = '" & textBox1 & "'"
End If
If Not IsNull(textBox2) Then
  sql = sql & " And Field2 = '" & textBox2 & "'"
End If

And, finally, assign the sql to the form datasource.

Design1

The below code goes in the main form, just next to the above:

SubFormControlName.Form.RecordSource = sql

Design2

To show the controls in the header section you need to set the property DefaultView to running forms (I don't know the extact translation, I work with Access in spanish) In this case, the datasource is assigned to the main form

Me.recourdsource = sql

For your third point: You can build calculated field using

Iif(fieldA<5,"Early",Iif(fieldA>10,"Late","OnTime"))

or you can use conditional formatting. I think this option is most clear

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