Because I am a rookie, I wrote the article to see the VB newcomers just started. Oh, there is nothing depth. Welcome everyone!
If you want to export the query results to Excel, if you want to view or print, then what I said is how to export the query results to Excel. Let's first write a function FillDataArray, the main role of this function is to import the field names in the query statement and the records found to Excel.
Public Function FillDataArray (AsaArray (), Adors as adoDb.recordset) AS Long
'Send data to Excel functions
Dim nrow as in
DIM NCOL AS INTEGER
ON Error Goto Fillerror
Redim asarray (100000, adorse.fields.count)
NROW = 0
For ncol = 0 to adors.fields.count - 1
AsaRray (ncol) = adors.fields (ncol) .Name
Next ncol
NROW = 1
Do While Not Adors.eof
For ncol = 0 to adors.fields.count - 1
Asarray (ncol) = adors.fields (ncol) .value
Next ncol
Adors.MoveNext
NROW = nrow 1
Loop
NROW = nrow 1
FillDataArray = nrow
EXIT FUNCTION
Fillerror:
MsgBox Error $
EXIT FUNCTION
Resume
END FUNCTION
Then write a process PrintList to call the previous function.
Private sub printlist ()
DIM STRSOURCE, STRDESTINATION AS STRING
DIM astempaRay ()
DIM inumrows as long
Dim Objexcel as Excel.Application
Dim Objrange as Excel.Range
ON Error Goto Excelerror
Set Objexcel = New Excel.Application 'New an Excel
DIM RS As New Adodb.Recordset
SET RS = conn.execute (SQLALL) 'SQLALL is a query statement
IF not r.
Objexcel.Workbooks.open app.path & "/vvv.xls"
After the Query result is exported, save it as a .xls file, so that the contents of the vvv.xls are empty, ensuring the correct export of the results of the latter query. "
Inumrows = FillDataArray (AstemParray, RS) 'Pixabur Press Group Function
Objexcel.cells (1, 1) = "Query Results" Fill the head
Set Objrange = Objexcel.Range (Objexcel.cells (2, 1), Objexcel.cells (Inumrows, Rs.fields.count))
ObjRange.Value = astemparray 'filled data
END IF
Objexcel.visible = true 'Shows Excel
Objexcel.displayalerts = true 'prompts save Excelexit Sub
Excelerror:
IF Err <> 432 and Err> 0 THEN
MsgBox Error $
Set objexcel = Nothing
EXIT SUB
Else
Resume next
END IF
End Sub
The vvv.xls used in it must be the XLS file that is built first. Do not save directly after the result is exported, but save it as a .xls file, so that the contents of the vvv.xls are empty, ensuring the correct export of the results of the later query results.