我正在尝试为报告生成表单执行日期过滤。我有一个带有此复选框的DateTimePicker,如果选中复选框,查询将添加一个WHERE子句。
目前我这样做: -
If DateTimePicker1.Checked Then
query = "SELECT payment_type, sales_payment_dtl.payment_amt, payment_remark, sales_payment_dtl.created_date, sales.inv_no
FROM sales_payment_dtl
INNER JOIN sales_payment
ON sales_payment_dtl.payment_id = sales_payment.payment_id
INNER JOIN sales
ON sales_payment.sales_id = sales.sales_id
WHERE sales_payment_dtl.created_date = '"& DateTimePicker.Text &"'"
Else
query = "SELECT payment_type, sales_payment_dtl.payment_amt, payment_remark, sales_payment_dtl.created_date, sales.inv_no
FROM sales_payment_dtl
INNER JOIN sales_payment
ON sales_payment_dtl.payment_id = sales_payment.payment_id
INNER JOIN sales
ON sales_payment.sales_id = sales.sales_id"
End If
是否可以缩短代码?因为我将来需要添加更多的过滤器,那么代码会变得很长,也许是这样的?
query = "SELECT payment_type, sales_payment_dtl.payment_amt, payment_remark, sales_payment_dtl.created_date, sales.inv_no
FROM sales_payment_dtl
INNER JOIN sales_payment
ON sales_payment_dtl.payment_id = sales_payment.payment_id
INNER JOIN sales
ON sales_payment.sales_id = sales.sales_id
IF '"& DateTimePicker.Checked = True &"' THEN
WHERE sales_payment_dtl.created_date = '"& DateTimePicker.Text &"'
ELSE
do nothing"
答案 0 :(得分:0)
简单的逻辑。
query = "SELECT payment_type, sales_payment_dtl.payment_amt, payment_remark, sales_payment_dtl.created_date, sales.inv_no " & _
"FROM(sales_payment_dtl) " & _
"INNER JOIN sales_payment " & _
"ON sales_payment_dtl.payment_id = sales_payment.payment_id " & _
"INNER JOIN sales " & _
"ON sales_payment.sales_id = sales.sales_id"
If DateTimePicker1.Checked Then
query &= " WHERE sales_payment_dtl.created_date = '" & DateTimePicker.Text & "'"
End If