MDI form Error

I'm getting the following error occasionally when I load my initial MDI form:

"Object reference not set to an instance of an object".

It happens on the following line of code:

            frmMain.ActiveForm.Text = "Centre : " & BranchName & " (" & UserCentre & "); User: " UserFullName & " (" & LCase(Username) & ")"

It doesn't happen all the time the application is loaded which makes it very hard to figure out why the eroror is occuring. Does anyone have any ideas?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

break the line down into
frmMain.ActiveForm.Text = "Centre : "
frmMain.ActiveForm.Text = frmMain.ActiveForm.Text + BranchName
frmMain.ActiveForm.Text = frmMain.ActiveForm.Text + " (" + UserCentre
.... etc

to find out which variable isnt getting initialised properly.
Mike TomlinsonMiddle School Assistant TeacherCommented:
The ActiveForm property may be returning Nothing if the application is still loading and no form is yet active:

    "A Form that represents the currently active form, or a null reference (Nothing in Visual Basic) if there is no active form."

Where are you executing this line of code from?

    Dim f As Form = frmMain.ActiveForm
    If Not (f Is Nothing) Then
        f.Text = "Centre : " & BranchName & " (" & UserCentre & "); User: " UserFullName & " (" & LCase(Username) & ")"
        MsgBox "No Active Form"
    End If

Instead of changing the form text is this manner, why not place code in that forms load event?


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PeterErhardAuthor Commented:
Thanks for that - appears to have worked!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.