我想按日期排序,這是我工作表的中間列。 IM從數據庫系統中得到我的數據,但我不能在該系統中它排序,我需要整理其到我這裏來,因爲這數據:如何在VB6中按日期對Excel數據進行排序
| A FIELD | B FIELD | C FIELD | DATE FIELD | E FIELD | F FIELD |
| Adata1 | Bdata | Cdata | 09.05.2011 | Edata | Fdata |
| Adata2 | Bdata | Cdata | 03.05.2011 | Edata | Fdata |
| Adata3 | Bdata | Cdata | 21.05.2011 | Edata | Fdata |
| Adata4 | Bdata | Cdata | 01.05.2011 | Edata | Fdata |
| Adata5 | Bdata | Cdata | 11.05.2011 | Edata | Fdata |
,我應該找到一種方法,讓這樣比貼以excel:
| A FIELD | B FIELD | C FIELD | DATE FIELD | E FIELD | F FIELD |
| Adata4 | Bdata | Cdata | 01.05.2011 | Edata | Fdata |
| Adata2 | Bdata | Cdata | 03.05.2011 | Edata | Fdata |
| Adata1 | Bdata | Cdata | 09.05.2011 | Edata | Fdata |
| Adata5 | Bdata | Cdata | 11.05.2011 | Edata | Fdata |
| Adata3 | Bdata | Cdata | 21.05.2011 | Edata | Fdata |
所以我怎麼能做到這一點在VB6到Excel?我可以使用它的助手,並從它讀取數據順序/排序比粘貼回到Excel,但哪些助手OLE?
Dim strcnn As String
Dim cnn As New ADODB.Connection
Dim Cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Private Sub Form_Load()
'Create database connection
strcnn = "MyConnectionToDb"
cnn.Open strcnn
Cmd.ActiveConnection = cnn
End Sub
Private Sub Command1_Click()
Dim i As Integer
Dim cek As String
Dim tarih As String
'Set excel
Set kitap = CreateObject("Excel.Application")
kitap.Workbooks.Add
'Data Query
cek = "SELECT * FROM DATATEST.trolololollololollololoo"
rs.Open cek, cnn
'If result is empty
If rs.EOF = True Then
'Report situation
Situation.Caption = "Situation : is under control!"
Else
'Start counter
i = i + 1
'Add headers
kitap.Cells(i, 1).Value = "SN"
kitap.Cells(i, 2).Value = "OP"
kitap.Cells(i, 3).Value = "HF"
kitap.Cells(i, 4).Value = "UC"
kitap.Cells(i, 5).Value = "HA"
kitap.Cells(i, 6).Value = "UA"
kitap.Cells(i, 7).Value = "IN"
'While not end of file
Do While Not rs.EOF
'Increase the Counter
i = i + 1
'Add the data
kitap.Cells(i, 1).Value = rs.Fields("SN")
kitap.Cells(i, 2).Value = rs.Fields("OP")
kitap.Cells(i, 3).Value = rs.Fields("HF")
kitap.Cells(i, 4).Value = rs.Fields("UC")
kitap.Cells(i, 5).Value = rs.Fields("HA")
kitap.Cells(i, 6).Value = dotdate(rs.Fields("UA")) 'UA is date field, this will be the key column
kitap.Cells(i, 7).Value = rs.Fields("IN") 'to sort all data is being saved to excel.
'Next record
rs.MoveNext
Loop
'Close data connection
rs.Close
End If
'Save data to excel
kitap.ActiveWorkbook.SaveAs(App.Path & "\troll.xls")
kitap.Application.Quit
'Report situation
Situation.Caption = "Situation : Excel Formatted Troll is Ready"
Exit Sub
Error:
'On error close connection
rs.Close
'Report situation
Situation.Caption = "Critical ERROR! : Connection has been trolled! Reset ur computer."
End Sub
如何從數據庫中讀取數據?當你收到它時,你將存儲什麼樣的結構? –
我給出了關於我的代碼的更多信息!我使用的ADODB記錄集 –