Mysql VB6.0有人能帮我写代码吗?

Mysql VB6.0有人能帮我写代码吗?,mysql,vb6,Mysql,Vb6,我正在使用我的项目清单系统,我想使用2 DTPicker在我的listview1中的mysql中显示我的books表中过滤的日期,并为其生成报告。我有一个错误,我在我的查询中的类模块idk,如果它只是一个查询,我真的很困惑我是一个乞丐在vb 6.0…请在需要帮助的家伙 我使用两个表,即书籍和供应商 “类模块”中的我的代码: Sub DisplayList(ListView1 As ListView, DateFrom As Date, DateTo As Date) Dim lstItem As

我正在使用我的项目清单系统,我想使用2 DTPicker在我的listview1中的mysql中显示我的books表中过滤的日期,并为其生成报告。我有一个错误,我在我的查询中的类模块idk,如果它只是一个查询,我真的很困惑我是一个乞丐在vb 6.0…请在需要帮助的家伙

我使用两个表,即书籍和供应商

“类模块”中的我的代码:

Sub DisplayList(ListView1 As ListView, DateFrom As Date, DateTo As Date)
Dim lstItem As ListItem, a As Integer
Dim rs As New ADODB.Recordset
Dim sql As String
If rs.State = adStateOpen Then rs.Close

sql = " SELECT supplier.category,books.title,books.dataAcquired,books.amount,books.quantity,books.accesionno,books.conditions" & _
      " From supplier INNER JOIN books" & _
      " ON supplier.code=books.code" & _
      " WHERE (((books.dataAcquired)>=#" & DateFrom & "#) and ((books.dataAcquired) <=#" & DateTo & "#))" & _
      " GROUP BY supplier.category,books.title,books.dataAcquired,books.amount,books.quantity,books.accesionno,books.conditions" & _
      " ORDER BY books.dataAcquired DESC;"
   rs.Open sql, cnn

   ListView1.ListItems.Clear
   Do While Not rs.EOF
    a = a + 1
        Set lstItem = ListView1.ListItems.Add(, , a, 1, 1)
            lstItem.SubItems(1) = rs(0).Value
            lstItem.SubItems(2) = rs(1).Value
            lstItem.SubItems(3) = rs(2).Value
            lstItem.SubItems(4) = rs(3).Value
            lstItem.SubItems(5) = rs(4).Value
            lstItem.SubItems(6) = rs(5).Value
            lstItem.SubItems(7) = rs(6).Value
            rs.MoveNext
            Loop

End Sub

format date how yyyy-MM-dd or yyyyMMdd

sql = " SELECT supplier.category,books.title,books.dataAcquired,books.amount,books.quantity,books.accesionno,books.conditions" & _
  " From supplier INNER JOIN books" & _
  " ON supplier.code=books.code" & _
  " WHERE (((books.dataAcquired)>='" & format(DateFrom,"yyyy-MM-dd") & "') and ((books.dataAcquired) <='" & format(DateTo,"yyyy-MM-dd") & "'))" & _
  " GROUP BY supplier.category,books.title,books.dataAcquired,books.amount,books.quantity,books.accesionno,books.conditions" & _
  " ORDER BY books.dataAcquired DESC;"
在为记录集清空添加验证时更改循环,一些方法

 if RecordsetIsClosed(rs) then exit sub

 While Not RecordSetIsEmpty(rs)
      a = a + 1
      Set lstItem = ListView1.ListItems.Add(, , a, 1, 1)
      lstItem.SubItems(1) = rs(0).Value
      lstItem.SubItems(2) = rs(1).Value
      lstItem.SubItems(3) = rs(2).Value
      lstItem.SubItems(4) = rs(3).Value
      lstItem.SubItems(5) = rs(4).Value
      lstItem.SubItems(6) = rs(5).Value
      lstItem.SubItems(7) = rs(6).Value
      rs.MoveNext
 wend

Public Function RecordSetIsEmpty(ByRef rs As ADODB.Recordset) As Boolean    
'   On Local Error GoTo RecordSetIsEmpty_Error    
'       RecordSetIsEmpty = True    
'       If rs Is Nothing Then
'           RecordSetIsEmpty = True
'           Exit Function
'       End If    
'       If RecordsetIsClosed(rs) = True Then
'           RecordSetIsEmpty = True
'           Exit Function
'       End If    
    RecordSetIsEmpty = (rs.BOF = True And rs.EOF = True)
'   RecordSetIsEmpty_Done:
'       Exit Function
'   RecordSetIsEmpty_Error:
'       Resume RecordSetIsEmpty_Done    
End Function


Public Function RecordsetIsClosed(ByRef rs As ADODB.Recordset) As Boolean    
On Local Error GoTo RecordsetIsClosed_Error
    RecordsetIsClosed = True    
    If rs Is Nothing Then
        RecordsetIsClosed = True
    End If        
    If rs.State <> adStateClosed Then
        RecordsetIsClosed = False
    End If
RecordsetIsClosed_Done:
    Exit Function
RecordsetIsClosed_Error:
    Resume RecordsetIsClosed_Done
End Function
别忘了打开数据库连接

更新的感谢Mark Bertenshaw

RecordSetIsEmpty用于解决执行下一步操作时出现的问题。。我记得

之所以使用RecordsetIsClosed,是因为在某些情况下,数据库管理器返回的不是记录集,或者记录集未正确初始化

例如,在执行movenext或读取值之前必须使用movefist进行访问

format date how yyyy-MM-dd or yyyyMMdd

sql = " SELECT supplier.category,books.title,books.dataAcquired,books.amount,books.quantity,books.accesionno,books.conditions" & _
  " From supplier INNER JOIN books" & _
  " ON supplier.code=books.code" & _
  " WHERE (((books.dataAcquired)>='" & format(DateFrom,"yyyy-MM-dd") & "') and ((books.dataAcquired) <='" & format(DateTo,"yyyy-MM-dd") & "'))" & _
  " GROUP BY supplier.category,books.title,books.dataAcquired,books.amount,books.quantity,books.accesionno,books.conditions" & _
  " ORDER BY books.dataAcquired DESC;"
在为记录集清空添加验证时更改循环,一些方法

 if RecordsetIsClosed(rs) then exit sub

 While Not RecordSetIsEmpty(rs)
      a = a + 1
      Set lstItem = ListView1.ListItems.Add(, , a, 1, 1)
      lstItem.SubItems(1) = rs(0).Value
      lstItem.SubItems(2) = rs(1).Value
      lstItem.SubItems(3) = rs(2).Value
      lstItem.SubItems(4) = rs(3).Value
      lstItem.SubItems(5) = rs(4).Value
      lstItem.SubItems(6) = rs(5).Value
      lstItem.SubItems(7) = rs(6).Value
      rs.MoveNext
 wend

Public Function RecordSetIsEmpty(ByRef rs As ADODB.Recordset) As Boolean    
'   On Local Error GoTo RecordSetIsEmpty_Error    
'       RecordSetIsEmpty = True    
'       If rs Is Nothing Then
'           RecordSetIsEmpty = True
'           Exit Function
'       End If    
'       If RecordsetIsClosed(rs) = True Then
'           RecordSetIsEmpty = True
'           Exit Function
'       End If    
    RecordSetIsEmpty = (rs.BOF = True And rs.EOF = True)
'   RecordSetIsEmpty_Done:
'       Exit Function
'   RecordSetIsEmpty_Error:
'       Resume RecordSetIsEmpty_Done    
End Function


Public Function RecordsetIsClosed(ByRef rs As ADODB.Recordset) As Boolean    
On Local Error GoTo RecordsetIsClosed_Error
    RecordsetIsClosed = True    
    If rs Is Nothing Then
        RecordsetIsClosed = True
    End If        
    If rs.State <> adStateClosed Then
        RecordsetIsClosed = False
    End If
RecordsetIsClosed_Done:
    Exit Function
RecordsetIsClosed_Error:
    Resume RecordsetIsClosed_Done
End Function
别忘了打开数据库连接

更新的感谢Mark Bertenshaw

RecordSetIsEmpty用于解决执行下一步操作时出现的问题。。我记得

之所以使用RecordsetIsClosed,是因为在某些情况下,数据库管理器返回的不是记录集,或者记录集未正确初始化


例如,访问是必需的,在执行movenext或读取值之前使用movefist

虽然我标记了SQL日期比较,这是代码的主要问题,但我真的不明白为什么需要RecordSetIsEmpty函数。一旦您检查了记录集不是空的,您就可以非常愉快地使用While not rs.EOF,甚至更好地使用Do Until rs.EOF作为循环条件。每个记录的这些检查是不必要的,而且效率低下。已更新。rs.eof有时失败抱歉,现在我没有使用.net的示例,但我等着为某人服务。虽然我为您标记了SQL日期比较,这是代码的主要问题,但我真的不明白您为什么需要RecordSetIsEmpty函数。一旦您检查了记录集不是空的,您就可以非常愉快地使用While not rs.EOF,甚至更好地使用Do Until rs.EOF作为循环条件。每个记录的这些检查是不必要的,而且效率低下。已更新。有时rs.eof会失败,对不起,现在我没有使用.net的示例,但我会等待为某人服务