Results 1 to 21 of 21

Thread: Loop through text fields in Active reports

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    916

    Loop through text fields in Active reports

    Hello everyone
    I'm sorry to poste here as "Reporting Forum" seemed abondoned.
    I need to loop through some text fields in the Activereport but I can't find a way.
    Code:
    Dim t As Integer
    For t = 1 To 12
        With ActiveReport1.Sections("Detail").Controls.Item("Field" & t)
    '     With ActiveReport1.Sections("Detail").Controls("Field" & t)
             .Top = Form1.Text1(t).Top
              .Left = Form1.Text1(t).Left
             .Width = Form1.Text1(t).Width
             .Height = Form1.Text1(t).Height
        End With
    Next
    thank you all

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Loop through text fields in Active reports

    Try something like this

    Code:
    Dim oCtl As Object
    Dim oFld As Field
    
    For Each oCtl In ActiveReport1.Sections("Detail").Controls
        If TypeOf oCtl Is Field Then
            Set oFld = oCtl
            '--- do something with oFld
        End If
    Next
    (air code)

    cheers,
    </wqw>

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    916

    Re: Loop through text fields in Active reports

    wqweto
    thank you very much
    unfortunately your code didn't work.
    Besides, I don't want to loop through all the textfields but 12 only.
    thank you

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Loop through text fields in Active reports

    Honestly I have never tried looping through the fields on a report. You can easily set the properties by referring to the fields directly.

    MyReport.Field1. whatever

    That said I would imagine you may get different results than you expect by setting a field location = to the location of a text box on a form as that location will refer to the location within the detail section rather than the page.

    What is it you are trying to accomplish?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    916

    Re: Loop through text fields in Active reports

    MyReport.Field1. whatever
    this is the only method that isworking up to now.
    but it's a long process for 12 fields.
    What is it you are trying to accomplish?
    I'm trying to set the position, size, fonts... of the report fields the same as the ones on the form
    thanks
    Last edited by newbie2; Oct 16th, 2021 at 04:21 PM.

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Loop through text fields in Active reports

    Quote Originally Posted by newbie2 View Post
    unfortunately your code didn't work.
    It works here ¯\_(ツ)_/¯

    Quote Originally Posted by newbie2 View Post
    Besides, I don't want to loop through all the textfields but 12 only.
    You can set the control's Tag propertry at design time. Then filter by checking its content with InStr inside the For Each loop.

    Edit: More sample code. This 110% works here (tested)

    Code:
    '--- code in ActiveReport1
    Option Explicit
    
    Friend Sub frInit()
        Dim oCtl As Object
        Dim oFld As Field
        
        For Each oCtl In Detail.Controls
            If TypeOf oCtl Is Field Then
                Set oFld = oCtl
                If InStr(1, oFld.Tag, "blue", vbTextCompare) > 0 Then
                    Debug.Print oFld.Name
                End If
            End If
        Next
    End Sub
    Code:
    '--- code in Form1
    Option Explicit
    
    Private Sub Form_Load()
        With New ActiveReport1
            .frInit
        End With
    End Sub
    cheers,
    </wqw>

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    916

    Re: Loop through text fields in Active reports

    wqweto
    thank you again
    your code is working
    but my problem is :
    I'm trying to set the position, size, fonts... of the report fields the same as the ones on the form
    Code:
    With Field1
    .Top = Form1.Text1(1).Top
    .Left = Form1.Text1(1).Left
    .Width = Form1.Text1(1).Width
    .Height = Form1.Text1(1).Height
    .Font.Size = Form1.Text1(1).Font.Size
    .Font.Name = Form1.Text1(1).Font.Name
    .Alignment = Form1.Text1(1).Alignment
    .ForeColor = Form1.Text1(1).ForeColor
    .Font.Bold = Form1.Text1(1).Font.Bold
     .Font.Underline = Form1.Text1(1).Font.Underline
          End With
    I want to do that for 12 textfields.

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Loop through text fields in Active reports

    Why position the fields, set the fonts and so on in the designer? That is the way you would normally do it.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    916

    Re: Loop through text fields in Active reports

    Quote Originally Posted by DataMiser View Post
    Why position the fields, set the fonts and so on in the designer? That is the way you would normally do it.
    the fonts and position of the form textoxes are set at runtime.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    916

    Re: Loop through text fields in Active reports

    any idea please?

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Loop through text fields in Active reports

    Quote Originally Posted by newbie2 View Post
    the fonts and position of the form textoxes are set at runtime.
    So are you saying they are set by the user at runtime?

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Loop through text fields in Active reports

    btw if you just set each field directly it would be done already, Sure it would be several lines of code but that is not a big deal and is easy enough to do. Take a look at the raw .frm file in a text editor sometime. You will see there is a separate line in there for every setting on every control.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    916

    Re: Loop through text fields in Active reports

    Quote Originally Posted by DataMiser View Post
    So are you saying they are set by the user at runtime?
    yes that's it

  14. #14
    Lively Member
    Join Date
    Nov 2020
    Posts
    67

    Re: Loop through text fields in Active reports

    Quote Originally Posted by newbie2 View Post
    Hello everyone
    I'm sorry to poste here as "Reporting Forum" seemed abondoned.
    I need to loop through some text fields in the Activereport but I can't find a way.
    Code:
    Dim t As Integer
    For t = 1 To 12
        With ActiveReport1.Sections("Detail").Controls.Item("Field" & t)
    '     With ActiveReport1.Sections("Detail").Controls("Field" & t)
             .Top = Form1.Text1(t).Top
              .Left = Form1.Text1(t).Left
             .Width = Form1.Text1(t).Width
             .Height = Form1.Text1(t).Height
        End With
    Next
    thank you all
    1) Is the report external to the VBP project or is it incorporated into the project?
    2) In which event of the report did you enter the code?
    3) Can you post the report with code?

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    916

    Re: Loop through text fields in Active reports

    Is the report external to the VBP project or is it incorporated into the project?
    incorporated into the project
    In which event of the report did you enter the code?
    in ActiveReport_Initialize event
    Can you post the report with code?
    no other code in the report other than the one you have quoted.
    thank you

  16. #16
    Lively Member
    Join Date
    Nov 2020
    Posts
    67

    Re: Loop through text fields in Active reports

    Have you tried to use the Detail_Format() event?

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    916

    Re: Loop through text fields in Active reports

    Quote Originally Posted by LeoFar View Post
    Have you tried to use the Detail_Format() event?
    sorry no success

  18. #18
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Loop through text fields in Active reports

    Just curious here but if you want the report to look like the form why not just use the printform method?
    Also if you want to use a report what is the reason that you don't just set the controls directly? Sure a loop would be less code but if you are having trouble getting a loop to work then the logical thing to do would be forget the loop and do a direct assignment. At runtime it is going to be about the same speed, possibly a little faster.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2015
    Posts
    916

    Re: Loop through text fields in Active reports

    Just curious here but if you want the report to look like the form why not just use the printform method?
    I want to care about the quality of printing. The quality of printing differs a lot between the two methods.
    Also if you want to use a report what is the reason that you don't just set the controls directly?
    The controls on the form can be resized and formatted by the user at runtime
    if you are having trouble getting a loop to work then the logical thing to do would be forget the loop
    It seems this is my remaining choice.
    thank you

  20. #20
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Loop through text fields in Active reports

    Quote Originally Posted by newbie2 View Post
    The controls on the form can be resized and formatted by the user at runtime
    There is no reason you can't directly set the field properties at runtime. No loop required.

  21. #21
    Lively Member
    Join Date
    Nov 2020
    Posts
    67

    Re: Loop through text fields in Active reports

    Quote Originally Posted by newbie2 View Post
    sorry no success
    If you post a sample project that reproduces the problem we can take a look.

    I could do it for you, but I would never know how you set up your project, form and report, with the risk of not guessing your need.
    Last edited by LeoFar; Oct 21st, 2021 at 02:29 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width