Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

Starting Application from Module - how to show main form

I have researched and tried the methods I found regarding using a module
as the startup object.  I am apparently missing something.

ModuleBV is my startup object; Form1 is the main form I want to show.  No matter what I try, I
get err 91 - object reference not set to an instance of an object when I get to line frmMain.Show()

Thank you,

Ruth


Here is my code:

Option Explicit On
Option Strict On
Imports System.windows.forms

Module ModuleBV

    ' make all forms public can then use show and hide from whereever
    Public frmMain As New Form1
    Public Review As New frmReview

    ' make users dataset public
    Public dsUsers As DataSet
    Public Sub Main()

       Try
           ' build connection
            'populate dataset
       catch
             messagebox.show(err.number)
             messagebox.show(err.description)    
       Finally
             'close connection
        Dim frmMain As New Form1
        frmMain.Show()
    End Sub
End Module

0
RuthD2004
Asked:
RuthD2004
  • 3
  • 2
1 Solution
 
RonaldBiemansCommented:
you define frmmain twice

Module ModuleBV

    ' make all forms public can then use show and hide from whereever
    Public frmMain As New Form1  '  **** <-- HERE
    Public Review As New frmReview

    ' make users dataset public
    Public dsUsers As DataSet
    Public Sub Main()

       Try
           ' build connection
            'populate dataset
       catch
             messagebox.show(err.number)
             messagebox.show(err.description)    
       Finally
             'close connection
        Dim frmMain As New Form1 '****** AND HERE
        frmMain.Show()
    End Sub
End Module
0
 
bramsquadCommented:
you realize you initialize frmMain twice?

once before the call, and the other under your public declarations...
0
 
RonaldBiemansCommented:
Sorry, that shouldn't matter (although it is wrong).

Are you sure this is the exact module you are using, because it is full of mistakes
0
 
bramsquadCommented:
RonaldBiemans - your absolutely right it shouldnt matter.  

your also right this code has to be incomplete....becuase there is no 'End Try' clause

in the following code i added that, and changed Show() to ShowDialog, and it works for me...

Module ModuleBV

    ' make all forms public can then use show and hide from whereever
    Public frmMain As New Form1
    Public Review As New frmReview

    ' make users dataset public
    Public dsUsers As DataSet
    Public Sub Main()
        Try
            ' build connection
            'populate dataset
        Catch
            messagebox.show(err.number)
            messagebox.show(err.description)
        Finally
            'close connection
            frmMain.ShowDialog()
        End Try
    End Sub

End Module

~b
0
 
RonaldBiemansCommented:
Hi Bramsquad,

The end try isn't the only mistake, it look like this code was copied from a vb6 application.

RuthD2004,

This is the proper way to start from a module

Imports System.Windows.Forms

Module MainModule
    Public mainForm As mainForm
    Private appContext As ApplicationContext

    Sub Main()
        StartApplication()
        ShowMainForm()
        ReadEvents()
        Cleanup()
    End Sub

    Sub StartApplication()
        appContext = New ApplicationContext()
    End Sub


    Sub ShowMainForm()
        mainForm = New MainForm()
        mainForm.Show()
    End Sub

    Sub ReadEvents()
        appContext.MainForm = mainForm
        Application.Run(appContext)
        ' Application will exit when closing mainForm
    End Sub

    Sub Cleanup()
        ' Garbage collection here
    End Sub
End Module
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now