数据网格在 vb6 中显示错误的输出



当我从第 10 个月选择同一日期时(假设 dtpckr1 = 02-10-2019和 dtpckr2 = 02-10-2019 (。数据数据网格不打印任何内容,并显示 msgbox 未记录找到,我编码以说服...但是当我从最后一个飞蛾中选择开始日期和本月结束日期时(比如 dtpckr1 = 30-09-2019 和 dtpckr2 = 02-10-2019 (,它显示了第 09 个月的所有数据,而第 10 个月没有任何数据......奇怪的是,当选择来自飞蛾 09 的日期时,即使它是相同的(例如 dtpckr1 = 13-09-2019 和 dtpckr2 = 13-09-2019 或 22-09-2019(它完美工作..因此,请尝试通过参考以下代码来帮助我..到目前为止,我发现我在 DataGridView 中获得的数据是按天 (dd( 而不是按整个日期...意味着如果我选择 date1 = 31/09/2019 和 date2 = 01/10/2019,那么它将仅显示从 01 月到 31 个月的 09 的数据。我还检查了数据库的日期格式和我的输入,它们是相同的...在 DataBSE 中,日期数据类型为"日期/时间",格式为"短日期"。如果有任何其他解决方案,请告诉我...我将尝试。。。我的目的是在 DatagridView 中显示按日期排列的食品订单,然后计算总销售额......我是 VB6 的新手...因此,如果您可以编辑我的代码并重新发布它..会很棒...因为我想在明天之前提交这个项目。这是唯一困扰我的事情...谢谢

Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub cmdSearch_Click()
Dim date1 As Date
Dim date2 As Date
If IsNull(DTPicker1.Value And DTPicker2.Value) Then
MsgBox "You must select date", vbCritical, "Warning"
Exit Sub
End If
DTPicker1.Value = Format(DTPicker1.Value, "dd-mm-yyyy")
DTPicker2.Value = Format(DTPicker2.Value, "dd-mm-yyyy")
date1 = DTPicker1.Value
date2 = DTPicker2.Value
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:OrderManiaordermania.mdb;Persist Security Info=False"
rs.CursorLocation = adUseClient
If DTPicker2.Value < DTPicker1.Value Then
MsgBox "End Date Cannot Be Lesser Then Start Date", vbCritical, "Wrong Input"
Exit Sub
Else
Adodc1.RecordSource = "select * from order1 where (date between #" & date1 & "# and #" & DTPicker2.Value & "#)"
Adodc1.Refresh
If Adodc1.Recordset.EOF Then
MsgBox "Please Enter Another Date", vbCritical, "No Record Found"
Else
Adodc1.Caption = Adodc1.RecordSource
End If
End If
con.Close
Call sale
End Sub

Public Sub sale()
Dim i As Integer
Dim Tot, gst, gtot As Double
For i = 0 To Adodc1.Recordset.RecordCount - 1
Tot = Tot + CDbl(DataGrid1.Columns(5).Text)
Adodc1.Recordset.MoveNext
Next i
Text1.Text = Tot
gst = Tot * 0.05
Text2.Text = gst
gtot = Tot + gst
Text3.Text = gtot
End Sub

尝试在 between 子句中反转月份和日期:

..."between #" & Format(date1, "mm-dd-yyyy") & "# and #" & Format(date2, "mm-dd-yyyy")) & "#)"

但是,正如@GSerg提醒我的那样,将SQL字符串与变量值连接被认为是不好的做法,因为可能会发生恶意代码的SQL注入。您应该使用参数。如果你想研究这个,这里有一个起点: https://learn.microsoft.com/fr-fr/office/client-developer/access/desktop-database-reference/createparameter-method-ado

相关内容

最新更新