使用具有两个日期条件的查询和另一个日期条件从ms访问表中选择记

时间:2018-03-22 14:02:24

标签: vb.net

我使用select语句从两个日期之间的ms访问表中检索记录,并使用另一个条件来检查客户名称

我收到错误“没有给出一个或多个必需参数的值”

代码如下

Try
            If Conn.State = ConnectionState.Closed Then
                Conn.Open()
            End If

        Dim dtDate1 As DateTime = DateTime.Parse(dtpDateFrom.Text)
        Dim dtDate2 As DateTime = DateTime.Parse(dtpDateTo.Text)

        ''''SQL_PaymentsP = "SELECT InvoiceID,CustomerName,InvoiceDate,InvoiceAmount,PaymentDesc,PaidAmount,DatePaid,CurrentBalance FROM Payments WHERE [DatePaid] BETWEEN #" & dtDate1.ToString("MM/dd/yyyy") & "# AND #" & dtDate2.ToString("MM/dd/yyyy") & "# " & "OR [CustomerName] = " & txtCustomer.Text & ""

                  SQL_PaymentsP = "SELECT Payments.PaymentID, Payments.InvoiceID, Payments.CustomerName, Payments.InvoiceDate, Payments.InvoiceAmount, Payments.PaymentDesc, Payments.PaidAmount, Payments.DatePaid, Payments.CurrentBalance, Payments.Status FROM Payments WHERE Payments.DatePaid Between #" & dtDate1.ToString("MM/dd/yyyy") & "# And #" & dtDate2.ToString("MM/dd/yyyy") & "# " & " OR " & "Payments.CustomerName =" & txtCustomer.Text & ""

        ' SQLInvoicesP = "Select DateInvoice,IDInvoices,InvoiceAmount,CustomerName,Mode from Invoices where DateInvoice between #" & dtDate1.ToString("MM/dd/yyyy") & "# and #" & dtDate2.ToString("MM/dd/yyyy") & "#"

        DataSet_PaymentsP.Clear()

        Dim DataAdapter_PaymentsP As New OleDbDataAdapter(SQL_PaymentsP, Conn)
        DataAdapter_PaymentsP.Fill(DataSet_PaymentsP, "Payments")



        Conn.Close()



        Application.DoEvents()
        Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo
        ConInfo.ConnectionInfo.ServerName = Application.StartupPath & "\DB.mdb"
        ConInfo.ConnectionInfo.DatabaseName = "kuku.mdb"
        ConInfo.ConnectionInfo.UserID = "Admin"
        ConInfo.ConnectionInfo.Password = ""


        Dim rpt1 As New CrystalReport5
        rpt1.Database.Tables(0).ApplyLogOnInfo(ConInfo)
        rpt1.SetDataSource(DataSet_PaymentsP)

        rpt1.SetParameterValue("Start_Date", dtDate1)
        rpt1.SetParameterValue("End_Date", dtDate2)
        rpt1.SetParameterValue("CustomerName", txtCustomer.Text)
        rpt1.SetParameterValue("Author", FormMain.XN.Text)


        Dim frm As New FormPrint

        frm.CrystalReportViewer1.ReportSource = rpt1
        frm.CrystalReportViewer1.LogOnInfo(0).ConnectionInfo.Password = "yazsys.com1234"
        frm.ShowDialog()
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

1 个答案:

答案 0 :(得分:0)

我刚刚添加了如下所示的单引号,而select查询现在可以检索ms访问的记录

SQL_PaymentsP =" SELECT Payments.PaymentID,Payments.InvoiceID,    Payments.CustomerName,Payments.InvoiceDate,Payments.InvoiceAmount,    Payments.PaymentDesc,Payments.PaidAmount,Payments.DatePaid,Payments.CurrentBalance,Payments.Status FROM Payments WHERE Payments.DatePaid'#" &安培; dtDate1.ToString(" MM / dd / yyyy")& "#'并且'#" &安培; dtDate2.ToString(" MM / dd / yyyy")& "#'或者" &安培; " Payments.CustomerName ='" &安培; txtCustomer.Text& "'"