Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
10 Dim MyExcel As Object
20 Dim MyWorkbook As Object
30 Dim MyWorksheet As Excel.Worksheet
40 Set MyExcel = New Excel.Application
50 Set MyWorkbook = MyExcel.Workbooks.Add
60 Set MyWorksheet = MyWorkbook.ActiveSheet
70 MyExcel.Visible = True
Private Sub ThisReportInExcel
10 On Error GoTo ThisReportInExcel_Error
' Declare our Excel objects.
Dim MyCMD As New ADODB.Command
Dim param As ADODB.Parameter
Dim Myrs As ADODB.Recordset
Dim MyExcel As Object
Dim MyWorkbook As Object
Dim MyWorksheet As Object
Dim i As Integer
20 Me.MousePointer = vbHourglass
30 MyCMD.ActiveConnection = Conn
40 MyCMD.CommandText = "myStoredProc"
50 MyCMD.CommandType = adCmdStoredProc
60 MyCMD.CommandTimeout = 0
70 Set param = MyCMD.CreateParameter("@ThisID", adInteger, adParamInput, , ThisID)
80 MyCMD.Parameters.Append param
90 Set Myrs = MyCMD.Execute
' See if there are records to even show a report on.
100 If Myrs.EOF And Myrs.BOF Then
110 MsgBox "There are no records found ", vbInformation
120 Me.MousePointer = vbNormal
130 Exit Sub
140 End If
150 Me.MousePointer = vbHourglass
160 Set MyExcel = CreateObject("Excel.Application")
170 Set MyWorkbook = MyExcel.Workbooks.Add
180 Set MyWorksheet = MyWorkbook.Sheets(1)
Dim lnSheets As Integer, lnCounter As Integer
190 With MyExcel
200 lnSheets = .Sheets.Count
210 For lnCounter = 2 To lnSheets
220 .Sheets(1).DELETE
230 Next
240 .Rows("1:1").Select
250 .Selection.DELETE '&& will delete header row
260 With .Sheets(1)
270 .Select
280 .Name = "This Report"
290 End With
300 MousePointer = vbHourglass
310 .Range("A1").Value = "Sample Report"
320 i = 1
330 .Range("A1:V1").MergeCells = True
340 .Range("A:V").Font.Name = "Arial"
350 .Range("A1:V1").Font.Size = 12
360 .Range("A1:V1").HorizontalAlignment = -4131
370 .Range("A1:V1").Font.Bold = True
380 With .Range("A" + CStr(1) + ":V" + CStr(1)).Borders(9)
390 .LineStyle = 1
400 .Weight = 4
410 .ColorIndex = -4105
420 End With
Dim strFYInvo As String
Dim curFYInvo As String
430 MousePointer = vbHourglass
440 .Range("A2").Value = Date
450 i = i + 2
460 .Range("A" + CStr(i)).Value = "Field1"
470 .Range("B" + CStr(i)).Value = "Field2"
480 .Range("C" + CStr(i)).Value = "Field3"
490 .Range("D" + CStr(i)).Value = "Field4"
500 .Range("E" + CStr(i)).Value = "Field5"
510 .Range("F" + CStr(i)).Value = "Field6"
520 .Range("G" + CStr(i)).Value = "Field7"
530 .Range("H" + CStr(i)).Value = "Field8"
540 .Range("I" + CStr(i)).Value = "Field9"
550 .Range("J" + CStr(i)).Value = "Field10"
560 .Range("K" + CStr(i)).Value = "Field11"
570 .Range("L" + CStr(i)).Value = "Field12"
580 .Range("M" + CStr(i)).Value = "Field13"
590 .Range("N" + CStr(i)).Value = "Field14"
600 .Range("O" + CStr(i)).Value = "Field15"
610 .Range("P" + CStr(i)).Value = "Field16"
620 .Range("Q" + CStr(i)).Value = "Field17"
630 .Range("R" + CStr(i)).Value = "Field18"
640 .Range("S" + CStr(i)).Value = "Field19"
650 .Range("T" + CStr(i)).Value = "Field20"
660 .Range("U" + CStr(i)).Value = "Field21"
670 .Range("V" + CStr(i)).Value = "Field22"
675 .Range("W" + CStr(i)).Value = "Field23"
680 With .Range("A" + CStr(i) + ":V" + CStr(i)).Borders(9)
690 .LineStyle = 1
700 .Weight = 4
710 .ColorIndex = -4105
720 End With
730 .Range("A:V").Font.Name = "Arial"
740 .Range("A2:V3").Font.Size = 8
750 .Range("B3:V3").HorizontalAlignment = -4108
760 .Range("A3:V3").Font.Bold = True
Dim cost As Variant
770 Do While Not Myrs.EOF
780 Me.MousePointer = vbHourglass
790 curFYInvo = Myrs!program + Myrs!FY + Myrs!SomeNo + CStr(Myrs!Field1)
800 cost = Myrs!cost
810 If strAnotherVar <> curOthervar Then
820 i = i + 1
830 .Range("A" + CStr(i)).Value = Myrs!Field2
840 .Range("B" + CStr(i)).Value = Myrs!Field3
850 .Range("C" + CStr(i)).Value = Myrs!Field4
860 .Range("D" + CStr(i)).Value = Myrs!Field5
870 .Range("E" + CStr(i)).Value = Myrs!Field6
880 .Range("F" + CStr(i)).Value = someVariable
890 .Range("G" + CStr(i)).Value = Myrs!Field7
900 .Range("H" + CStr(i)).Value = Myrs!Field8
910 .Range("I" + CStr(i)).Value = Myrs!Field9
920 .Range("J" + CStr(i)).Value = Myrs!Field10
930 .Range("K" + CStr(i)).Value = Myrs!Field11
940 .Range("L" + CStr(i)).Value = Myrs!Field12
950 .Range("M" + CStr(i)).Value = Myrs!Field13
960 .Range("N" + CStr(i)).Value = Myrs!Field14
970 .Range("O" + CStr(i)).Value = Myrs!Field15
980 .Range("P" + CStr(i)).Value = Myrs!Field16
990 .Range("Q" + CStr(i)).Value = Myrs!Field17
1000 .Range("R" + CStr(i)).Value = Myrs!Field18
1010 .Range("S" + CStr(i)).Value = Myrs!Field19
1020 .Range("T" + CStr(i)).Value = Myrs!Field20
1030 .Range("U" + CStr(i)).Value = Myrs!Field21
1040 .Range("V" + CStr(i)).Value = Myrs!Field22
1050 .Range("A" + CStr(i) + ":V" + CStr(i)).Font.Size = 8
1060 .Range("A" + CStr(i) + ":H" + CStr(i)).HorizontalAlignment = -4108
1070 End If
1080 strAnotherVar = curOthervar
1090 Myrs.MoveNext
1100 Loop
1110 Me.MousePointer = vbHourglass
1120 Myrs.Close
1130 Set Myrs = Nothing
1140 Set MyCMD = Nothing
1150 .Rows.Rows.Cells.Select
1160 .Rows.EntireColumn.AutoFit
1170 .Range("A1").Select
1180 End With
1190 MyWorkbook.Activate
1200 MyExcel.Visible = True
' cleanup object and release excel:
1210 Set MyWorksheet = Nothing
1220 Set MyWorkbook = Nothing
' If MyExcel.Visible = False Then MyExcel.Quit
1240 Set MyExcel = Nothing
1250 Me.MousePointer = vbNormal
1260 On Error GoTo 0
1270 Exit Sub
ThisReportInExcel_Error:
1280 Me.MousePointer = vbNormal
1290 MsgBox "Error on line: " & Erl & vbCrLf & vbCrLf & "Error number: " & Err.Number & vbCrLf & vbCrLf & "Error description: " & Err.Description &
vbCrLf & vbCrLf & "Error location: ThisReportInExcel of Form THIS_Form"