数据网格在 vb6 中显示错误输出
The datagrid is show wrong output in vb6
当我从第 10 个月选择相同的日期时(比如 dtpckr1 = 02-10-2019 和 dtpckr2 = 02-10-2019)..data datagrid 不打印任何内容并显示 msgbox not record found which i code for convinence ...但是当我选择本月最后一个月的开始日期和结束日期时(比如 dtpckr1 = 30-09-2019 和 dtpckr2 = 02-10-2019 )它显示了第 9 个月的所有数据而没有第 10 个月的数据... and the strange this is when choose date which is from moth 09 even if it is same(say dtpckr1 = 13-09-2019 and dtpckr2 = 13-09-2019 or 22-09-2019) it works perfectly ..所以请尝试通过参考以下代码来帮助我。到目前为止,我发现我在 datagridview 中获取的数据是按天 (dd) 计算的,而不是按整个日期计算的...意味着如果我选择 date1 = 31/09/2019 和 date2 = 01/10/2019 然后它将仅显示从 09 月开始的日期 01 到 31 的数据....我还检查了数据库的日期格式和我的输入,它们是相同的...在数据库中,日期数据类型为 "date/time",格式为 "short date"。 ...如果有任何其他解决方案,请告诉我...我会尝试...我的目的是在 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:\OrderMania\ordermania.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")) & "#)"
但是将 SQL 字符串与变量值连接起来被认为是不好的做法,正如@GSerg 提醒我的那样,因为 SQL 可能会注入恶意代码。您应该使用参数。如果你想研究这个,这里是一个起点:https://docs.microsoft.com/fr-fr/office/client-developer/access/desktop-database-reference/createparameter-method-ado
当我从第 10 个月选择相同的日期时(比如 dtpckr1 = 02-10-2019 和 dtpckr2 = 02-10-2019)..data datagrid 不打印任何内容并显示 msgbox not record found which i code for convinence ...但是当我选择本月最后一个月的开始日期和结束日期时(比如 dtpckr1 = 30-09-2019 和 dtpckr2 = 02-10-2019 )它显示了第 9 个月的所有数据而没有第 10 个月的数据... and the strange this is when choose date which is from moth 09 even if it is same(say dtpckr1 = 13-09-2019 and dtpckr2 = 13-09-2019 or 22-09-2019) it works perfectly ..所以请尝试通过参考以下代码来帮助我。到目前为止,我发现我在 datagridview 中获取的数据是按天 (dd) 计算的,而不是按整个日期计算的...意味着如果我选择 date1 = 31/09/2019 和 date2 = 01/10/2019 然后它将仅显示从 09 月开始的日期 01 到 31 的数据....我还检查了数据库的日期格式和我的输入,它们是相同的...在数据库中,日期数据类型为 "date/time",格式为 "short date"。 ...如果有任何其他解决方案,请告诉我...我会尝试...我的目的是在 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:\OrderMania\ordermania.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")) & "#)"
但是将 SQL 字符串与变量值连接起来被认为是不好的做法,正如@GSerg 提醒我的那样,因为 SQL 可能会注入恶意代码。您应该使用参数。如果你想研究这个,这里是一个起点:https://docs.microsoft.com/fr-fr/office/client-developer/access/desktop-database-reference/createparameter-method-ado