Visual Basic Classic
--
Questions
--
Followers
Top Experts
CAN YOU HELP ME WITH MY PROB. ..hOW CAN I EXPORT A QUERY OR DATA REPORT IN EXCEL APPLICATION? IM USING vb6.
--WHICH IS MORE AVISABLE TO MAKE A REPORT..IM DOING A PAYROLL SYSTEM...DATA REPORT OR CRYSTAL REPORT?!
PLSSS...HELP!!!!
JETO
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Dim rs_Data As ADODB.Recordset
Dim str_Select As String
Dim rs_Cols As ADODB.Recordset
Dim str_Cols As String
Dim objExcel As Excel.Application
Dim numRows As Integer
Dim numCols As Integer
Dim row As Integer
Dim col As Integer
On Error GoTo Err_SS_Recordset
Screen.MousePointer = vbHourglass
'
' Create recordset
'
' str_Select = "Put your select string here"
str_Select = "SELECT * FROM myTable"
Set rs_Data = New ADODB.Recordset
rs_Data.CursorLocation = adUseClient
rs_Data.Open str_Select, cn, adOpenStatic, adLockReadOnly
If (rs_Data.RecordCount <= 0) Then
MsgBox "No data to export to Excel for your filter.", vbOKOnly, "No Data"
rs_Data.Close
Set rs_Data = Nothing
Exit Sub
End If
On Error GoTo Err_Create_Rpt_Spreadsheet
'
' Initialize number of rows and columns
'
numCols = rs_Data.Fields.count
numRows = rs_Data.RecordCount
'
' Create Excel Spreadsheet
'
Set objExcel = New Excel.Application
objExcel.Workbooks.Add
'
' Set defaults for the spreadsheet
'
objExcel.Cells.Select
With objExcel.Selection.Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 9
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
'
' Open form showing export progress
' FOrm has a progress bar on it. Code can be deleted
'frm_Export2Excel.Show
'frm_Export2Excel.lbl_Exp2
'
' Load column headers
' Special code to add column headers from a table I have
' Delete but note how the headers are added
'
'str_Cols = "SELECT * FROM view_Excel_Col_Info where txt_ViewName = '" & VIEW_NAME & "'"
'Set rs_Cols = New ADODB.Recordset
'rs_Cols.Open str_Cols, cn, adOpenStatic, adLockReadOnly
'row = 1
'
' Use the information from the Excel Column Information table if it exists.
' Use the view field name otherwise
'
'If (rs_Cols.RecordCount > 0) Then
' For col = 1 To numCols
' rs_Cols.MoveFirst
' rs_Cols.Find "txt_FieldName = '" & rs_Data.Fields(col - 1).Name & "'"
' objExcel.Cells(row, col) = Trim(rs_Cols!txt_ColName)
' objExcel.Range(objExcel.Ce
' objExcel.Selection.ColumnW
' If (InStr(rs_Cols!txt_ColName
' objExcel.Range(objExcel.Ce
' objExcel.Selection.NumberF
' End If
' Next col
'Else
'
' Column Headers using the table column names
'
For col = 1 To numCols
objExcel.Cells(row, col) = Trim(rs_Data.Fields(col - 1).Name)
objExcel.Range(objExcel.Ce
objExcel.Selection.ColumnW
Next col
End If
frm_Export2Excel.pb_Exp2Ex
'
' Close the column information recordset
'
'rs_Cols.Close
'Set rs_Cols = Nothing
'
' Load selected records into rows
'
rs_Data.MoveFirst
For row = 2 To numRows + 1
frm_Export2Excel.lbl_Exp2E
frm_Export2Excel.pb_Exp2Ex
For col = 1 To numCols
objExcel.Cells(row, col) = rs_Data.Fields(col - 1).Value
Next col
objExcel.Rows(row).Select
objExcel.Selection.RowHeig
rs_Data.MoveNext
Next row
'
' Activate the Excel spreadsheet
'
Unload frm_Export2Excel
objExcel.Range("A1").Selec
objExcel.Visible = True
Screen.MousePointer = vbDefault
Exit_btnCancel_Click:
rs_Data.Close
Set rs_Data = Nothing
Set objExcel = Nothing
Exit Sub
Err_SS_Recordset:
MsgBox Err.Description
Set rs_Data = Nothing
Screen.MousePointer = vbDefault
Unload frm_Export2Excel
Exit Sub
Err_Create_Rpt_Spreadsheet
MsgBox Err.Description
objExcel.Visible = True
Unload frm_Export2Excel
Resume Exit_btnCancel_Click
I'll add code to build a word table in the next comment
Accept Dhaest's comment(s) as an answer.
DanRollins -- EE database cleanup volunteer






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Visual Basic Classic
--
Questions
--
Followers
Top Experts
Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.