Link to home
Start Free TrialLog in
Avatar of bowen18
bowen18

asked on

Add text file to Data Report

Using Data Report Designer to create reports from an Access 97 database.  However I have some information stored in a text file that I would like to display in the Data Report.  How can I have the Data Report display the text file?

Thanks in advance for any and all help.

-Jeff
Avatar of QJohnson
QJohnson

If the data is small enough to fit in a text box (less than 32K I believe), you can just use the rptTextBox control.

The pain about this is that you can't simply assign to it.  You have to know the index of the control to do that.

So force your app to tell you the index of the control you want.  I always paste this function into my reports for this purpose:

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

I call it in the report's intialize event and comment it out after I have the values I need from it during my report design.  If I need to add any controls, I just uncomment it and let it run again.

Once you have the index number, you can use code like this to set the caption of your control:

   MySectionName.Controls(IndexNumber).Caption = sBuffer

where sBuffer is the string you have populated by reading your text file.

Do you need code for reading the file?
Avatar of bowen18

ASKER

Thanks for the quick response.  This is what I've been using on my forms to display the text file.  Are you saying this will work in some form on the Data Report?

Dim MyData As String
    Open "H:\time\data\" & deTime.rscomTime![Name] & ".txt" For Input As #1
    Do Until EOF(1)
        Input #1, MyData
        txtComments.Text = txtComments.Text & vbCrLf & MyData
    Loop
    Close #1
Yes, that will work, as long as you substitute properly for the clunky way you have to reference controls on the report.

You could probably avoid some screen flicker and refresh time, by the way, by just using some buffer variable and making a single assignment to the textbox.

i.e.,

sBuffer = ""
Do Until EOF(1)
      Input #1, MyData
      sBuffer = sBuffer & MyData
Loop
Close #1
txtComments.Text = sBuffer

Avatar of bowen18

ASKER

I apologize, but I'm not sure I understand.  At what point/procedure would I add my text file?  If its not too much trouble, could you possibly provide an example?

Thanks!

-Jeff
Avatar of bowen18

ASKER

Also, its giving me a varible not defined error on the txtComments.text part.  I double checked and txtComments is the correct name of the text box on the report.  Any ideas?

Thanks,
-Jeff
ASKER CERTIFIED SOLUTION
Avatar of QJohnson
QJohnson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK - OOPS!
please get rid of the reference to #1 that I left in the code in that last post and substitute #nHandle.  Sorry about that... I hit submit too fast.

The offending line should read:

    Input #nHandle, MyData

Sorry.
Avatar of bowen18

ASKER

Wow, that's alot of great information.  Your going above and beyond what I expected!  I do have one issue with the supplied code.  When I run the first part to get the index, it gives me a varible not defined error on rptStatement.Sections.  Any help is very much appreciated.

Thanks,
-Jeff
Please type that line of code that give the error.
Avatar of bowen18

ASKER

> For Each mySect In rptStatement.Sections

the debugger highlights rptStatement.Sections as the culprit.
Just replace rptStatement with the name of YOUR report object.
Avatar of bowen18

ASKER

Thanks for all your help but I'm still having a problem.  Now its with the last line of the code.

You wrote
PageHeader.Controls(IndexForTxtCommentsGoesHere).Caption = sBuffer

I changed it to
Section1.Controls(21).Caption = sBuffer

because Section1 is the Details section which is where txtComments is.  And 21 is the index of the txtComments text box.

The error I'm getting is a "varible not defined" and its looking at Section1 of the Section1.Controls(21).Caption = sBuffer line.  I've used Details.Controls(21).caption and even drUserInfo.Sections(1).Controls(21).Caption with no luck.

Probably another one of my stupid mistakes but would love some help :)

Thanks,
Jeff
in my code i'm expecting you have the text you want on the report in a variable called sBuffer.  Is it there?

I actually used rptLabel controls in my app and they certainly had a .Caption property.  Perhaps the text box you are using doesn't.  Perhaps it has a .Text property like standard textboxes.  

So you can try two things.  

(1)  Change the code to

     Section1.Controls(21).Text = sBuffer

If that fails,

(2)  change it back to .Caption, set a breakpoint (hit F9 while you edit the line in the code window), and let the project run.

It will stop at the breakpoint and you can let your mouse hover over the sBuffer part of the code.  The contents of sBuffer will become visible.   Are the contents there???

Obviously you can do these in reverse order if you want.
Avatar of bowen18

ASKER

I had to modify the code you sent me eariler to grab the section name.  Its as follows:

Dim curSect As Object

For Each curSect In drUserInfo.Sections
        If curSect.Name = "Section1" Then
            curSect.Controls(21).Caption = sBuffer
        End If
Next

Good news is its working this way.  I can't thank you enough for all your help!  If I had a million points, I'd give them to you.

-Jeff
You're very welcome.  

Does it make you wonder if more powerful reporting tools might be useful??? <grin>

FYI, I am a REAL fan of VS View Reporting Edition from ComponentOne.  Among MANY other features, the two I most appreciate are: it allows me to build a report definition in Access, import to this tool and use it in VB to produce a report with three lines of code AND it supports writing PDF files as output (as well as MANY export formats).  

Very cool and worth the money.  But for real value, you probably shouldn't buy just THAT tool.  Their whole suite of tools (Call ComponentOne Studio for ActiveX - or .NET - pick one) is just a little more than the price of the single tool and you get so many other fine controls (including two of the most popular grids in the VB world: TrueDBGrid and VSFlexGrid).

Just one developer's opinion.