VB Form Refreshing

Terry Rogers
Terry Rogers used Ask the Experts™
on
How do I get a form to automatically refresh? I have it runing several databases SQL commands. The form initially executes a DOS batch file but when the focus is brought back to the form all it displays is a white background with none of my labels. I've tried using a timer and having it automatically refresh it but that doesn't seem to work, neither does the auto-redraw property. How can I make this work?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
To refresh a form, try: Form.Refresh.

But, for your situation, you can write the process of executeing a DOS batch as a sub (modularize), so that you will not depend on the form to do the process.
Terry RogersIT Technical Architect

Author

Commented:
That hasn't worked. Try again. I have tried setting up a timer that executes frmRefresh.refresh every 1 second but that makes no difference, I have also enabled auto-redraw and that hasn't made any difference.
We assume the process is still running. The problem is you only have one tread so you can create more threads or simply change the project type to ActiveX EXE, (Just same as a normal EXE with some bells and whistles) set the threadding model to "One Thread per Object".

How to start a thread so you main form will keep running:

You have 2 forms one main forma and form2 which calls the dos command.

On form 2 you have a timer and a Public Sub like this

Public Sub RunCommand(CommandLine)
SaveCommand=CommandLine
timer1.interval=100
End Sub ' contol is now returned to you form

Sub Timer1_Timer()
Timer1.interval=0

ok=Shell(CommandLine,vbHidden) ' now run you command

End Sub

The other way to do this is to shell so another exe which runs the task.

Also suggest that you run the sql commands using the Async method.

Hope this helps:~)
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Also use the same process to run the SQL queries.  Because eachform has its own thread the screen refresh should not get blocked.
Terry RogersIT Technical Architect

Author

Commented:
OK, Tried that but ... I get an error box when I try to compile. ActiveX EXE, ActiveX DLL, and ActiveX Control project types require that you have a least one public creatable class module.

I don't have one. What should I do now?

Note, I am a new VB programming needing to change an old project for my company. The original project was done by a much more experienced programmer.
Terry RogersIT Technical Architect

Author

Commented:
I got that working but the form is still the same, it won't refresh.
Terry RogersIT Technical Architect

Author

Commented:
I only have one form. 4 Modules and one designed. Everything is called from the initial load of the form.
Just add a class module and in the class properties make it type 5 muitiuse.

Did you create another form to run the sql?

You must ativate via a timer control on the second form.




What database is it Access/SQL. What version?
Terry RogersIT Technical Architect

Author

Commented:
Why do I have to add a second form? And I am useing Microsoft Access 97.
Terry RogersIT Technical Architect

Author

Commented:
Why do I have to add a second form? And I am useing Microsoft Access 97.
You have to move any code which is execuing to form2 so it can run in another thread.

Also in you form load event only fire the timer on the other form.

Sub Form_Load()

Perhaps do this first


Me.Show
Doevents

fdim frm as new form
set frm=form2
frm.RunCommand "nnnnn"

End Sub
Terry RogersIT Technical Architect

Author

Commented:
Ok, This appears to be more work than I initially thought. This project is quite complex and I don't want to have to start moving code around. I could do more damage than good. Is there no way I can make it work as it is? Perhaps there's a control of api or somthing I could use?
There is a way you could do it with a small change and that is to try to use Async. queries.

Are you using DAO or ADO?

If you are using ADO, if not you should be becuase it supports Async process but only ADO V2.7 works.

So the question is what is being done to the database.

Is it an update query or are you openning a recordset with a lot of data.  The other problem with an MDB is in an update query it will try tolock all of the reocrds first.  It is sometimes quicker to loop through the data and just update one record at a time.

If you are new to VB you may not know about DoEvents


DO While Not RS.Eof
   CN.BeiginTrans
   Do some processing
   CN>CommitTrans ' release record locks
   RS.MoveNext
   DoEvents ' always include in any loops
Loop



Terry RogersIT Technical Architect

Author

Commented:
Ok, This appears to be more work than I initially thought. This project is quite complex and I don't want to have to start moving code around. I could do more damage than good. Is there no way I can make it work as it is? Perhaps there's a control of api or somthing I could use?
Terry RogersIT Technical Architect

Author

Commented:
We use a Database Environment.

A typical query we use is :

        vStandard = DisOldTrans()
        vSQL = "Update tblActual Set Actual = '" & vStandard & "' where StandardName = 'OldTrans' and ACTKEY = '" & vDepkey & "'"
        dbMIS.MIS.Execute (vSQL)

Any ideas or help?

Commented:
hi terry

Try this

me.autoredraw = true (in form load event)

provide me.refresh after you have finished with the opeartions

Commented:
and don't you forget to use DoEvents!

Terry RogersIT Technical Architect

Author

Commented:
What is DoEvents ? I've seen it around but I have no idea what it does. Or how to use it.

Commented:
Just write DoEvents after your .refresh

Write DoEvents and press F1 for more help
Be very careful using DoEvents though, because the user will be able to click buttons and stuff on your form, and the code will execute when DoEvents is called, which may mess up your processing.

Zaphod.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial