Hi Penta,
I have successfully created a report programatically using the below -
CODE STARTS
Option Compare Database
Option Explicit
Private rpt As Report
Private lngCurWidth As Long 'Tracks cumulative width of controls
Const CONSTHEIGHT = 270 'Default values for controls
Const CONSTWIDTH = 2700
Private Sub cmdBuild_Click()
On Error GoTo ErrTrap
'----------------------------
lngCurWidth = 10
'*** REPORT ***
Set rpt = CreateReport
With rpt
.RecordSource = "qry_SubRecords" 'Set record source
.PageHeaderSection.Height = 300 'tentar mudar altura do cabeçalho
.Section("Detail").Height = 265
End With
'*** GROUPING ***
Dim strGrpFld As String
strGrpFld = "Name" 'Change to your grouping level field
CreateGroupLevel rpt.Name, strGrpFld, True, False 'Report,Field,Header,Footer
rpt.Section(5).Height = 265 'Set height of Group Header
'Add group control for 'Name'
CreateTextBox "Name", "txtName", acGroupLevel1Header
'Reset lngCurWidth for Detail controls
lngCurWidth = 10
'*** DETAILS ***
CreateLabel "Field1", "lblField1", acPageHeader, 1000
CreateTextBox "Field1", "txtField1", acDetail, 1000
'Add More Controls
CreateLabel "Field2", "lblField2", acPageHeader
CreateTextBox "Field2", "txtField2", acDetail
'Add More Controls
CreateLabel "Field3", "lblField3", acPageHeader
CreateTextBox "Field3", "txtField3", acDetail
'Add More Controls
CreateLabel "Field4", "lblField4", acPageHeader
CreateTextBox "Field4", "txtField4", acDetail
'*** FORMATTING ***
With rpt
.Section(0).Height = 280
End With
'----------------------------
ExitTrap:
'Destroy Report?
Exit Sub
ErrTrap:
Select Case Err.Number
Case Is <> 0
MsgBox "An unexpected error has occurred: " & Err.Description _
, vbExclamation + vbOKOnly _
, "Error " & Err.Number & " occurred" _
, Err.HelpFile _
, Err.HelpContext
Resume ExitTrap
'Add handled errors here with case
End Select
End Sub
Private Sub CreateTextBox(ctlSource As String, ctlName As String, lngSection As Integer, Optional lngWidth As Long)
Dim txt1 As TextBox 'Declare textbox variable
If lngWidth < 1 Then lngWidth = CONSTWIDTH 'Set default width if not specifieD
Set txt1 = CreateReportControl(rpt.Name, acTextBox, lngSection, , , lngCurWidth)
With txt1 'Set ptys
.Name = ctlName
.Width = lngWidth
.ControlSource = ctlSource
.Height = CONSTHEIGHT
If lngSection = acGroupLevel1Header Then
.FontSize = 12
.FontBold = True
.Height = CONSTHEIGHT + 100
End If
End With
lngCurWidth = lngCurWidth + txt1.Width + 50 'Store cumulative width of controls including gap
End Sub
Private Sub CreateLabel(strCaption As String, ctlName As String, lngSection As String, Optional lngWidth As Long)
Dim lbl1 As Label 'Declare label control variable
If lngWidth < 1 Then lngWidth = CONSTWIDTH 'Set default width if not specified
Set lbl1 = CreateReportControl(rpt.Name, acLabel, acPageHeader, , , lngCurWidth, , lngWidth)
With lbl1 'Set ptys
.Name = ctlName
.Caption = strCaption
.Width = 2700
.Height = CONSTHEIGHT
.FontBold = True
End With
End Sub
CODE ENDS
Let us know how you get on,;)
Jon
|