Re: Loading a form




<James@xxxxxxx> wrote in message news:rjnsp2pddb8cbee7kll4r7hhpcnom3o3ku@xxxxxxxxxx
I have a form that takes a while to load (about 3 seconds). How do i
get it to load in background whiile a splash screen displays?

Show the splash screen first. Just don't show the splash screen modally. In your main form, I'd recommend moving all code in Form_Load code into a Public procedure (if you're not doing it that way already). Load the main form (don't show it yet) and call that procedure immediately after showing the splash screen. When that public procedure returns, unload the splash screen and show your main form. This is easier if you're using Sub Main as your startup object because you'd do all of this from there. Here's an example. Start a new Standard EXE project. Add 2 forms and keep default names (Form2 will be the splash screen). Move and size the forms however you want. You can add controls to them, but it's not necessary for this example. Add a standard code module (.bas file). Open Project Properties and make Sub Main the Startup Object (on the General tab). Copy and paste this code into the .bas file:

-----BEGIN CODE
Option Explicit

Private Sub Main()

Load Form1
Load Form2
Form2.Show vbModeless
DoEvents 'make sure splash screen is drawn before continuing

If Form1.Setup Then
Unload Form2
Form1.Show
Else
'Problem setting up main form; close app
Unload Form2
Unload Form1
End If

End Sub
-----END CODE

Copy and paste this code into Form1 (the main form)

-----BEGIN CODE
Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public Function Setup() As Boolean

'Write all your initialization code here.
'I'm using the Sleep API function to simulate
'taking 3 seconds for all the initialization
'code to execute. Ideally, there should be no
'code AT ALL in the Form_Load event.
On Error GoTo EH

Sleep 3000
Setup = True

Exit Function

EH:
Setup = False

End Function
-----END CODE


Make sure you've written housekeeping or other clean up code as necessary, either in the Form_Unload events (your splash screen shouldn't have any clean up to do, though) or in the error handler of the Setup function as appropriate. If there's a problem in the Setup function, it should return False. This will allow the form to be properly unloaded in Sub Main.

--
Mike
Microsoft MVP Visual Basic


.



Relevant Pages

  • Re: RecLength for Random File
    ... Firstly have a look at your routine Public Sub Load. ... Public MyRec As Fields ' Record variable ... The stuff you have written belongs in the (declarations) section of your Form. ...
    (microsoft.public.vb.general.discussion)
  • Re: UserForm Is A Class
    ... assume the using "Load" is the correct in a situation like your example. ... Load myForm ... > Private Sub UserForm_Activate ... > Private Sub UserForm_Deactivate ...
    (microsoft.public.word.vba.general)
  • Re: Does loading additional forms drain resources?
    ... I also hadn't thought of loading a tab's subform until you click on the tab. ... One great tip is to not load sub forms until you need them. ...
    (microsoft.public.access.forms)
  • Re: Does loading additional forms drain resources?
    ... costs is your additional coding and developer time. ... One great tip is to not load sub forms until you need them. ... loads only ONE sub-form loads (the one on the 1st tab). ...
    (microsoft.public.access.forms)
  • Re: Only some websites will open - Ubuntu
    ... Linux drivers..so its weird that that works, ... network setup in vmware, the virtual machine is assigned it's own IP ... Slashdot won't load, vmware.com won't load, ... Linksys RV082 router with basically the default settings. ...
    (comp.os.linux.misc)