3

I have a code with a recursive function that checks into a folder for folders and writes the name, ID, and depth of the folder in my database. The process is repeated until all folders are in the database (usually 200 folders per projects).

When I run the code with my code window open, I can see what the code is doing because of debug.print, but since users never have the code window open, they can't see what's going on. I thought about 2 solutions.

  1. Open the "immediate window" as a pop-up over my form.
  2. Create a form with a text box.

I searched google but did not find a working solution to do the immediate window pop-up.

As for the second idea is there a way to just send the .print to a textbox or is there something like a console object in vba?

I was using

Form_PrintWindow.PrintWindow.Text = xmlNode3.Attributes.getNamedItem("id").Text & " " & xmlNode3.Attributes.getNamedItem("name").Text & vbNewLine & Form_PrintWindow.PrintWindow.Text

But string gets full halfway in the process.

trixrabbit
  • 259
  • 7
  • 22
  • I've used a small userform with a textbox before. Bonus is you can set it to allow selection/scrollbars. But are you wanting this to be updated realtime? – enderland Nov 10 '14 at 20:02
  • How much do you know about classes and interfaces in VBA? And are you still looking to print to the immediate window when you're debugging? – RubberDuck Nov 10 '14 at 20:02
  • 1
    I've been doing VBA for about 2 months, I know how most stuff works. BeforeI update the .mdb on our server, I usually comment de debug.print in the code. Everything seemed perfec with my textbox form until I got an error saying the string is full about halfway in the process. I need to find another method. – trixrabbit Nov 10 '14 at 20:17
  • Ahhhhhhh now I understand better. What about adding each line to a listbox instead? That way you only have one line at a time. I don't think you really want to get into [hijacking the IDE](http://www.mztools.com/articles/2006/mz2006007.aspx). – RubberDuck Nov 10 '14 at 20:28
  • I was thinking about using a listbox for alternative, but since I just want a real time "console" to monitor what's happening I tought maybe there was an object in vba for that. Doy uo think listbox will work with over 200 lines ? I think I will also try to clear my textbox every 50 lines or so... – trixrabbit Nov 10 '14 at 20:35
  • I don't understand that maxlength is set to no limit and maxlines is 2,147,483,647 by default, but I get Run-time Error '2176'-The setting for this property is too long..Maybe it's not related to the length of the string but how I put it. – trixrabbit Nov 10 '14 at 20:53

2 Answers2

1
  1. Create a form with an empty listbox control the appropriate size. (I named my form DebugOutput and the listbox OutputList)
  2. Add an UpdateProgress sub to that form. It will add the output of your other process as new items to the listbox and select the most recently added item.

    Public Sub UpdateProgress(text As String)
        'add item and select it
        With Me.OutputList
            .AddItem text
            .Selected(.ListCount - 1) = True 'zero based index
        End With
    
        DoEvents 'this frees up the OS to repaint the screen
    End Sub
    
  3. In your existing code, create a new instance of Form_DebugOutput (or whatever you named your form. Note that access automatically prepends forms with Form_.)

  4. Instead of Debug.Print call the UpdateProgress method of the form instance we created.

    Public Sub testit()
        Dim output As New Form_DebugOutput
        output.Visible = True
    
        Dim i As Long
        For i = 1 To 1000
            output.UpdateProgress "I've said hello " & i & " times."
        Next
    
        Stop
    End Sub
    

And it will look something like this.

form with process output

Outputting the results in real time like this will slow the code down, so carefully consider if you really need to display this information. Also, if you want the form to remain on screen you will need to declare the Form variable at a global scope. For more on this you may want to read about my progress bar.

RubberDuck
  • 11,933
  • 4
  • 50
  • 95
  • 1
    I used something very similar to this. I changed my textbox for a listbox and I just use docmd.openform to open the form. It might slow the process a bit but it's not noticeable considering I'm download files recursively and analysing them. A progress bar would be useful too but is not really required...Also the fonction is recursive and is called 10 to 300 times so it's hard to guess the percentage. Thank you. – trixrabbit Nov 12 '14 at 19:16
0

My suggestion would be to use the SysCmd() to update the status bar.

The reason for this is because everytime I have tried to output to a form while code is running it has been hit and miss one what gets displayed to the user. I have never had an issue with the status.

 Function FolderImport()
 Dim statBAR as variant
 Dim fldName as string
 statBAR = SysCmd(acSysCmdInitMeter, "Processing Import of Folders", 200)
 For i = 1 to 200
    statBAR=SysCmd(acSysCmdUpdateMeter, i)
    DoCmd.RunSQL "INSERT INTO tblFOLDERS VALUES ('" & fldName & "');"
 Next i
 Forms.frm_NAV.lstFOLDERS.requery()
 End Function

This code is not 100% complete but you get an idea of what is going on. While the code is running the user cannot do anything anyhow so displaying the folders in the list box as the code runs is the same as running the requery() after the code runs. The status bar lets the user know that something is happening so they don't think the program froze.

AxGryndr
  • 2,274
  • 2
  • 22
  • 45
  • This would not help at all if the strings are as long as trix indicates and make it even worse.. – enderland Nov 10 '14 at 20:04
  • 1
    [Do Events is the trick to displaying progress mid-stream.](http://christopherjmcclellan.wordpress.com/2014/03/08/progress-bar-for-ms-access/) You have to give the OS time to repaint the screen. – RubberDuck Nov 10 '14 at 20:07
  • Status bar would only show the last one though, I would like some kind of log in real time. I tought I managed to do it but string gets full halfway. – trixrabbit Nov 10 '14 at 20:20