2013-08-06 57 views
0

我很有意思。看了很多论坛,但没有找到。在VB 2010 MS访问查询涉及日期范围

我试图使用VB 2010 System.Data.OleDb对象从Access数据库查询数据。 下面是代码:

Dim AccessCommand As New System.Data.OleDb.OleDbCommand("SELECT format(dateadd(""m"", -e.tenure, e.expirydate), ""yyyy-mm-dd"") as Expired, n.customerid , e.type as Description, (select s.type from servicetype s where s.servicecode = e.servicecode) as CodedType, (select v.servicecode from servicetype v where v.servicecode = e.servicecode) as ServiceCode, e.totalamount INTO [Excel 12.0 Xml;DATABASE=d:\BookTest.xlsx;HDR=Yes;].[Sheet1] " _ 
     & " From notification n, notificationservice e where " _ 
     & " e.notificationid = n.notificationid " _ 
     & " and mid(n.notificationid, 1,2) ='RN' " _ 
     & " and jn_duplicate = 0 " _ 
     & " and dateadd(""m"", -e.tenure, e.expirydate) between @1 and @2 " _ 
     & " and (n.salesid is null or (n.salesid is not null and not exists 
      (select paymentid from payment p where p.invoiceid = n.salesid))) ", _ 
     m_conn) 


     AccessCommand.Parameters.Add("@1", OleDbType.Date).Value = New DateTime(2013, 7, 21) '"#20-jul-2013#" '(InputBox("Enter Expiry DateFrom in dd-mon-yyyy", "", "", 100, 100)) 
     AccessCommand.Parameters.Add("@2", OleDbType.Date).Value = New DateTime(2013, 7, 31) '"'#30-jul-2013#" '(InputBox("Enter Expiry DateTo in dd-mon-yyyy", "", "", 100, 100)) 

但是,像指定等没有数据尝试尽可能多的组合,我总是得到错误..

我的目标是让用户键入两个日期,将形成日期从和日期到在查询之间有日期。

将不胜感激任何帮助。

感谢 卓

+0

“...错误就像没有指定的数据等”有点不清楚,你能发布确切的错误信息吗? –

+0

错误是“没有给出一个或多个所需参数的值”。这个错误被我包含的异常捕获所困住。 –

回答

0

the docs on configuring parameters;

用于OLE DB和.NET Framework数据提供程序的ODBC的.NET Framework数据提供程序不支持将参数传递给SQL语句或存储过程的命名参数。在这种情况下,您必须使用问号(?)占位符,如下例所示。

换句话说,对于OLEDB您需要between ? and ? "更换between @1 and @2 ",只是依靠参数的顺序。

+0

非常感谢你,但仍然没有解决我的问题,因为#字符追加到inputbox获得的日期时仍然是空白。但wehn我把查询全日期...日期#07/21/2013#它的工作原理... –

+0

我不认为'#'应该在参数化查询时,尝试与日期。 –