If statements not working! Troubleshooters needed...

Joined
Mar 23, 2004
Messages
241
Hi guys.

I don't know what's going wrong with this. I've been toying with it for a few days, and can't figure it out. It's the first bit of code which I'm struggling with.

I want it to first look at the ProductType box, and if it hasn't changed from the default, to prompt the user to enter a valid choice.

Then I want it to go through all the mandatory required fields on the form and check that they all have values in, and again alert them if any are missing (I've only done the FirstName field so far, but I need to do about 8).

It does the first bit okay, warning people about the ProductType , but if the ProductType entry is okay, but the first name is missing, I don't get a message box, it just carries on with the rest of the code.

Could someone a little more experienced than me suggest where I'm going wrong? I'm very new to VBA, so I might have done it fairly clumsily...

Code:
Private Sub FinishedButton_Click()
On Error GoTo Err_FinishedButton_Click

If Me.ProductType = "Please Select One" Then
    MsgBox "You haven't selected a product type. Please select the required product and try again.", 48, "Insufficient Information"
    Me.ProductType.SetFocus
    
ElseIf Me.FirstName = Null Then
    MsgBox "You haven't entered a first name. Please do so and try again.", 48, "Insufficient Information"
    Me.FirstName.SetFocus

' This bit looks at the product name and assigns a code accordingly:
ElseIf Me.ProductType = "Unit Trust" Then
    ProductCode = "UT"
    GoTo FinishRoutine
        ElseIf Me.ProductType = "ISA" Then
        Me.ProductCode = "ISA"
        GoTo FinishRoutine
            ElseIf Me.ProductType = "Bond" Then
            Me.ProductCode = "B"
            GoTo FinishRoutine

' This bit assigns a number and makes the relevant cells visible:

FinishRoutine:
Me.Finished = "Finished"
Me.AutoNumberZeroes = Format(Me.AutonumberID, "0000000")
Me.PolicyNo = Me.ProductCode & Me.AutoNumberZeroes
Me.DBInput = Now()
Me.UserName = Forms![MainMenu]![NameBox]
Me.PolicyNo.Visible = True
Me.PolicyNo_Label.Visible = True
Me.DBInput.Visible = True
Me.DBInput_Label.Visible = True
Me.UserName.Visible = True
Me.UserName_Label.Visible = True
Me.NextRecordButton.Visible = True
Exit_FinishedButton_Click:

End If

    Exit Sub

Any help greatly appreciated... :oops:
 
The entirety of my problem was solved by this simple alteration. Note the cunning use of "IsNull". Brilliant.

Many thanks to my mate Conan for sorting this one out. Hope it's of use to anyone experiencing the same problems in the future.

Code:
If IsNull(Me.FirstName) Then
        MsgBox "You haven't entered a first name. Please do so and try again.", 48, "Insufficient Information"
        Me.FirstName.SetFocus
        Exit Sub
   End If

Huge thanks as always to everyone on this board for trying to help so tirelessly. Keep on caring, keep on sharing. I'm not worthy. :pray:
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,221,852
Messages
6,162,431
Members
451,765
Latest member
craigvan888

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top