Question about program startup and connecting to a DB

I have a program that will contain one main form and many children forms attached to it. Before I even get that far as launching the forms I need to have a routine that will verify that it connect to the database. No sense of even displaying a form if I cannot get to the DB. My main form is called frmMain. Should I have a little routine in there to check and verify that I can connect to the DB. If not, show the error message and exit the form and end the program OR should I have another mechanism to check the DB connectivity. If OK, then show the main form.

I have a class module with all of my ADO calls and procedures. I'm just trying to figure out the best place to place this initially so I can make the references to all of these procedures from any form in my app, which I'll have about 20. Just trying to get this ironed out before I continue because I'll be referencing a ton of DB calls from where ever I decide to initially create the class module.

So my question is where should I place the code to access the class - in my main form frmMain or somewhere else like some sort of form that gets launched and then hidden for the remainder of the execution of the program. Just looking for the best way to do this for future projects and the reasons why or why not doing it one way or another.

Thanks in advance.
LVL 13
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

ahammarConnect With a Mentor Commented:
If your main objective is to simply make sure your app is connected to the database, then I would put in a little routine in the main forms open event to check that.  You could set the forms visible property to false so nothing is seen, then have it become visible only after a connection is made, or bring up a message box indicating an error or to retry and have your app end or whatever you want if the connection does not succeed....
Another way to do it would be to have all your controls hidden except for a label that just something like:
Connecting to main data....Please wait....
After the connection was made, have the label disappear and then show your controls, or have the labels caption change to indicate to retry or whatever you want in the case that a connection does not succeed.
You could also use the forms caption instead of a label if you prefer...

Just a little info that will hopefully help you out...


samic400Author Commented:
So if I decide to run this check DB code in my frmMain form then it would be logical to create my instance of my class in frmMain and reference everything out of there?

I should have read your question a little better as my one track mind this morning was only taking into conseration the connectivity issue.....but I don't see a problem with that if your main form is going to remain open, but as far as it being the BEST way to create an instance of your class, I can't say....I have little experience in that area (I think I'm going to change that now though).  Someone else may come along with better advice than what I can give you on that matter....sorry about that.  I probaby should have just skipped over this question, but I saw it had 0 comments, so I just wanted to see if I could lend a helping hint....hopefully someone else will be by soon...

The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

dbbishopConnect With a Mentor Commented:
I would start the application in a Sub Main() in a module. Have your database class defined as Public in the module and instanciate it in Sub Main(). If the class was properly instantiated and the database connection was made, then show your form. Otherwise, tyou can dispay a messagebox and exit the application.
I assume you are using VB6. As an alternative, I would still make my startup procedure Sub Main() in a module. Have it perform myForm.Load. You can actually code properties to a form:

Private mblnDBRunning As Boolean

Public Property Get IsDBRunning() As Boolean
  IsDBRunning = mblnDBRunning
End Property

In your form Load() method, write the code to instantiate your class and check the database connection. If connected, set mblnDBRunning = True

Then, in your module code:
Private Sub Main()
    If myForm.IsDBRunning Then
        ' Code to display message and exit app.
End Sub
samic400Author Commented:
Thanks for your input.
Thanks for the points and the grade...I hope it helped you out...

All Courses

From novice to tech pro — start learning today.