vb and datareport

Hi,

  I've made a vb program with a datareport that I use to show stats to my client.
  I know how to change data on run time and I do it like this
      DR3.Sections("Section3").Controls.Item(1).Caption = "Something from my database"

  This work fine with textbox object but now I want to chage a label object that is in the Header of my report, how can I do it ?
deschenespAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QJohnsonCommented:
I change several rptlabel objects in my DataReport in one of my apps.  In my particular case, I read the report header items from a data source and populated some module-level string variables with them.  Then I assigned their values to the rptlables with the code shown below:  

   Dim mySection  As Object
   Dim iControl   As Integer
   Dim sCustID    As String
   Dim sCustName  As String
   Dim sCustAddr1 As String
   Dim sCustAddr2 As String
   Dim sMsg       As String
   
   fnPopulateReportLabels = True
   On Error GoTo FPRLErr
   
   'Call WriteOutIndex     ' for getting control's index
   
   For Each mySection In rptStatement.Sections
      Select Case mySection.Name
     
         Case "PageHeader"
            mySection.Controls(1).Caption = msCompany
            mySection.Controls(2).Caption = msAddr1
            mySection.Controls(3).Caption = msAddr2
            mySection.Controls(4).Caption = msAddr3
           
         Case "cmDeRpt_Grouping_Header"
            mySection.Controls(21).Caption = _
               frmStmtSetup.txtBegDate & " - " & frmStmtSetup.txtEndDate
           
         Case "cmDeRpt_Grouping_Footer"
            mySection.Controls(4).Caption = msCompany
            mySection.Controls(5).Caption = msAddr1
            mySection.Controls(6).Caption = msAddr2
            Debug.Print sCustID
     
         Case "PageFooter"
            mySection.Controls(1).Caption = frmStmtSetup.txtComment.Text
            Debug.Print "comment "; frmStmtSetup.txtComment.Text

      End Select
     
   Next   ' rptStatement Section

<<<<< end of code for changing rptlabel captions at run time  <<<<<

The nasty part, of course, is figuring out which controls have which index numbers.  I actually wrote a short sub to help me with this.  I copied the text from the Debug window into notepad and printed it for my project's documentation.  Very helpful.  When I added new objects to the report, I re-ran it, of course:

   Dim iCtrl   As Integer
   Dim mySect  As Object
   
   For Each mySect In rptStatement.Sections
      Debug.Print " --> " & mySect.Name
      For iCtrl = 1 To mySect.Controls.Count
         Debug.Print "             " & mySect.Controls(iCtrl).Name & " i = " & iCtrl
      Next
   Next
   Set mySect = Nothing
0
deschenespAuthor Commented:
Ok first where do I put this : fnPopulateReportLabels = True and what is it for.

The label I want to change is in Report Header,
and I can see how to make work this code, but your code seems great. Just need some little more help on this one.
0
QJohnsonCommented:
Sorry if this was confusing - I didn't include the function declaration and it was
       Private Function fnPopulateReportLabels() as Boolean
so I just put in that line of code so it would generate a return value.  YOU CAN IGNORE IT - so just delete it from whatever routine you place the rest of the code.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

QJohnsonCommented:
Please note I also omitted the error trapping code.  So the on error goto won't work for you.  

Here is the routine in its entirety>>>>>>

Private Function fnPopulateReportLabels() As Boolean
'*************************************************************
'Procedure:    fnPopulateReportLabels()
'Parameters:   None
'Created on:   08/25/00 By:   QJ
'Description:  We need to generate information on the report that
'              are not linked to any data members.
'Modified On:
'Mod Notes:
'*************************************************************
   Dim mySection  As Object
   Dim iControl   As Integer
   Dim sCustID    As String
   Dim sCustName  As String
   Dim sCustAddr1 As String
   Dim sCustAddr2 As String
   Dim sMsg       As String
   
   fnPopulateReportLabels = True
   On Error GoTo FPRLErr
   
   'Call WriteOutIndex     ' for getting control's index
   
   For Each mySection In rptStatement.Sections
      Select Case mySection.Name
     
         Case "PageHeader"
            mySection.Controls(1).Caption = msCompany
            mySection.Controls(2).Caption = msAddr1
            mySection.Controls(3).Caption = msAddr2
            mySection.Controls(4).Caption = msAddr3
           
         Case "cmDeRpt_Grouping_Header"
            mySection.Controls(21).Caption = _
               frmStmtSetup.txtBegDate & " - " & frmStmtSetup.txtEndDate
           
         Case "cmDeRpt_Grouping_Footer"
            mySection.Controls(4).Caption = msCompany
            mySection.Controls(5).Caption = msAddr1
            mySection.Controls(6).Caption = msAddr2
            Debug.Print sCustID
     
         Case "PageFooter"
            mySection.Controls(1).Caption = frmStmtSetup.txtComment.Text
            Debug.Print "comment "; frmStmtSetup.txtComment.Text

      End Select
     
   Next   ' For Each mySection In rptStatement.Sections

FPRLDone:

   Set mySection = Nothing
   Exit Function

FPRLErr:
   fnPopulateReportLabels = False
   Set mySection = Nothing
   sMsg = "Error while generating report labels. " & vbCrLf & _
      "System's number for the error is: " & Err.Number & vbCrLf & _
      "System's description for the error is: " & vbCrLf & Err.Description
   MsgBox sMsg, vbInformation, gsAppName

End Function

<<<<< end of pasted code.

You obviously will not have gsAppName.  But you probably want to use your own error trapping.  If you want to use mine, just substitue App.Title for gsAppName and it will work nicely.

Also, you can eliminate the Cases that use my custom grouping from the DataEnvironment I used.  If you find it convenient to insert your own name there, fine.  Likely you can ignore the group levels since you are just working with the Page Header section.

I hope this pasted code formats well here.  Ask again if you still have any questions.

Q
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deschenespAuthor Commented:
while running your function a get the error number 438
Description : object doesn't support this property or methode
0
deschenespAuthor Commented:
ok I don't have a control(4) so I've just deleted this line but it doesn't change the labels at all.
I did it like this and it dont want to work

   Case "Section4"
           MsgBox ("ALLO") 'the function pass here but the rest dont work
           mySection.Controls(1).Caption = "msCompany"
           mySection.Controls(2).Caption = "msAddr1"
           mySection.Controls(3).Caption = "msAddr2"
0
QJohnsonCommented:
You only want to change a label in the header right?

If so,  just delete the bottom three cases in the select case statement and remove all the lines of code in the PageHeader case except the first one.

That leaves the Select Case looking like:


     Select Case mySection.Name
     
        Case "PageHeader"
           mySection.Controls(1).Caption = msCompany

     End Select

Now change that only line of code in the PageHeader to:

       mySection.Controls(X).Caption = "YourTextHere"

where X is the control's index that you either already know, or have determined by running the CallWriteIndex subroutine.  A call to it is commented out in the code.  

That routine is the one whose code I posted at the bottom of my first reply here (again, without its subroutine declaration, so add that)... in fact, here's a pastable version of that code to make it easier.

Private Sub WriteOutIndex()
   Dim iCtrl   As Integer
   Dim mySect  As Object
   
   For Each mySect In rptStatement.Sections
      Debug.Print " --> " & mySect.Name
      For iCtrl = 1 To mySect.Controls.Count
         Debug.Print "             " & mySect.Controls(iCtrl).Name & " i = " & iCtrl
      Next
   Next
   Set mySect = Nothing

End Sub
0
deschenespAuthor Commented:
it's still not working ...
here's what I get ...

   with the sub WriteOutIndex() I get

    --> Section4
             Label1 i = 1
             Label2 i = 2
             Label3 i = 3
             Line1 i = 4
             lbtest i = 5

lbtest is the label I want to change.
 
   so I call the function this way from my function:

        a = fnPopulateReportLabels()


     and here's your function adapted to my need's

   Private Function fnPopulateReportLabels() As Boolean
'*************************************************************
'Procedure:    fnPopulateReportLabels()
'Parameters:   None
'Created on:   08/25/00 By:   QJ
'Description:  We need to generate information on the report that
'              are not linked to any data members.
'Modified On:
'Mod Notes:
'*************************************************************
  Dim mySection  As Object
  Dim iControl   As Integer
  Dim sCustID    As String
  Dim sCustName  As String
  Dim sCustAddr1 As String
  Dim sCustAddr2 As String
  Dim sMsg       As String
 
  fnPopulateReportLabels = True
  On Error GoTo FPRLErr
 
  For Each mySection In DR.Sections
     Select Case mySection.Name
     
        Case "Section4"
             mySection.Controls(5).Caption = "msCompany"
      End Select
     
  Next   ' For Each mySection In rptStatement.Sections

FPRLDone:

  Set mySection = Nothing
  Exit Function

FPRLErr:
  fnPopulateReportLabels = False
  Set mySection = Nothing
  sMsg = "Error while generating report labels. " & vbCrLf & _
     "System's number for the error is: " & err.Number & vbCrLf & _
     "System's description for the error is: " & vbCrLf & err.Description
  MsgBox sMsg, vbInformation, gsAppName

End Function

what is wrong with the way I do it. I don't get any error and I know that the command mySection.Controls(5).Caption = "msCompany" as been executed but no change in my report.
0
QJohnsonCommented:
Change the key section of code a bit to help us... try:


       Case "Section4"
            msgbox "Control's name: " & mySection.Controls(5).Name
            msgbox "Current Caption: " & mySection.Controls(5).Caption
            msgbox "Setting new caption to: " & "msCompany"
            mySection.Controls(5).Caption = "msCompany"
            msgbox "After changing caption, it is: " & mySection.Controls(5).Caption


by the way, have you compiled with Cntrl-F5 yet?  I still see gsAppName in the error trap code and I'll bet you don't have it declared any where.  Are you using Option Explicit?
     
0
deschenespAuthor Commented:
I finnaly found the problem,

The datareport needed a refresh (DR.refresh)
That's all.

Thank you for your time an your help.

You save me a lot of time.

I'll give you an A+++ for your help.

Thank's again
0
deschenespAuthor Commented:
Excellent support...
0
QJohnsonCommented:
No problem - glad to help.

It's really a pain that we can't use the controls' name properties to do something as simple as

    reportcontrols("MyControlName").Caption = "something"

and its a pleasure to save someone else the frustration I had until I figured out this solution.

That's enough work for a weekend....enjoy yourself <grin>
0
QJohnsonCommented:
thanks for the comment, deschenesp!
0
deschenespAuthor Commented:
no problem
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.