开发者

MS ACCESS query using form to build WHERE clause

I need to be able to read a combo box to determine to column to use for a where clause. Simple example would be SELECT * FROM TABLE WHERE [Forms]![frmNameWhatever]![ComboTime] BETWEEN [b开发者_如何学运维lah]![blah]![blah] AND [blah]![blah]![blah]

blah blah blah works... The first part, right after the where, returns zero rows... Am i using the wrong syntax? I've tried this w/ a text box as well and it still returns zero rows... Sorry someone might have to re-write this but i'm tired.. its the end of the day

Thanks for any help ^^


Try putting pound signs around your BETWEEN values.

BETWEEN #8:00 AM# and #12:00 PM#

To create a dynamic SQL string:

strSQL = _
   "Select myColumns FROM myTable WHERE " & Me.myComboBox & " BETWEEN #" & _
   Me.MyFirstTextBoxDate & "# AND #" & Me.MySecondTextBoxDate & "#"


You can concatenate an sql statement and run it with RunSQL like so:

DoCmd.RunSQL("(SELECT * FROM TABLE WHERE " & Forms("frmNameWhatever").ComboTime.Value & " BETWEEN [blah]![blah]![blah] AND [blah]![blah]![blah]);")


It may not be a good idea to hard code your Form's control names within your SQL code. Consider a PROCEDURE with strongly-typed parameters e.g. ANSI-92 Query Mode syntax:

CREATE PROCEDURE GetOrdersByPeriod
(
 :start_date DATETIME, 
 :end_date DATETIME
)
AS 
SELECT OrderID, CustomerID, OrderDate
  FROM Orders
 WHERE OrderDate BETWEEN :start_date AND :end_date;

You would then EXECUTE this proc by passing in your controls' values as parameters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜