What am i missing?

Sam Summers

Registered User.
Local time
Today, 12:47
Joined
Sep 17, 2001
Messages
939
Hi Guys,

I have two comboboxes "TOSubSection" and "DROPSSubSection" that are both not visible until the user selects a client from another Combobox "ClientName".

If they select the name Transocean then "TOSubSection" becomes visible and if they select any other name then "DROPSSubSection" becomes visible. Depending on the client selected either the form "EnterItemTransocean" opens or the "EnterItemDROPS" form opens.

I am using the code below which works fine for Transocean but nothing happens for the rest?

I think I am just missing something but cant seem to crack it?

Many thanks in advance

Code:
Private Sub TOEnterByAll_Click()
On Error GoTo TOEnterByAllBtn_Click_Err

    Dim stDocName As String
    Dim stLinkCriteria As String
    
    ClientName.SetFocus
        If Forms!Main!SetClient.Value = "Transocean" Then
        If IsNull(SelectArea) Or IsNull(SelectSection) Or IsNull(TOSubSection) Then
        DoCmd.Beep
        MsgBox "You must select all the locations first.", vbExclamation, "Incomplete options selected!"
        Else
        If IsNull(SelectArea) Or IsNull(SelectSection) Or IsNull(DROPSSubSection) Then
        DoCmd.Beep
        MsgBox "You must select all the locations first.", vbExclamation, "Incomplete options selected!"
        End
    End If
    
    SetClient.SetFocus
    If Forms!Main!SetClient.Value = "Transocean" Then
    DoCmd.OpenForm "EnterItemTransocean", , , stLinkCriteria
    Else
    stDocName = "EnterItemDROPS"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
End If

TOEnterByAllBtn_Click_Exit:
    Exit Sub

TOEnterByAllBtn_Click_Err:
    MsgBox Error$
    Resume TOEnterByAllBtn_Click_Exit
    End If
End If
End Sub
 
You have the End IF's in the wrong places.

The last 2 are not needed and you need another End IF before SetClient.SetFocus.

I have colour coded the If groups below and you will see they are out of sync.

Code:
Private Sub TOEnterByAll_Click()
On Error GoTo TOEnterByAllBtn_Click_Err

    Dim stDocName As String
    Dim stLinkCriteria As String
    
    ClientName.SetFocus
        [COLOR="Red"]If Forms!Main!SetClient.Value = "Transocean" Then[/COLOR]
        [COLOR="SandyBrown"]If IsNull(SelectArea) Or IsNull(SelectSection) Or IsNull(TOSubSection) Then[/COLOR]
        DoCmd.Beep
        MsgBox "You must select all the locations first.", vbExclamation, "Incomplete options selected!"
        Else
        [COLOR="Olive"]If IsNull(SelectArea) Or IsNull(SelectSection) Or IsNull(DROPSSubSection) Then[/COLOR]
        DoCmd.Beep
        MsgBox "You must select all the locations first.", vbExclamation, "Incomplete options selected!"
       [COLOR="olive"] End[/COLOR]
    [COLOR="SandyBrown"]End If[/COLOR]
    
    SetClient.SetFocus
   [COLOR="MediumTurquoise"] If Forms!Main!SetClient.Value = "Transocean" Then[/COLOR]
    DoCmd.OpenForm "EnterItemTransocean", , , stLinkCriteria
    Else
    stDocName = "EnterItemDROPS"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
[COLOR="mediumturquoise"]End If[/COLOR]

TOEnterByAllBtn_Click_Exit:
    Exit Sub

TOEnterByAllBtn_Click_Err:
    MsgBox Error$
    Resume TOEnterByAllBtn_Click_Exit
    [COLOR="SandyBrown"]End If[/COLOR]
[COLOR="Red"]End If[/COLOR]
End Sub

Always try to stagger (tab) everything that comes after IF (or ELSE or ELSEIF) so you can easily see your nested IF statements, like;
Code:
Private Sub TOEnterByAll_Click()
On Error GoTo TOEnterByAllBtn_Click_Err

    Dim stDocName As String
    Dim stLinkCriteria As String
    
    ClientName.SetFocus
        If Forms!Main!SetClient.Value = "Transocean" Then
            If IsNull(SelectArea) Or IsNull(SelectSection) Or IsNull(TOSubSection) Then
                DoCmd.Beep
                MsgBox "You must select all the locations first.", vbExclamation, "Incomplete options selected!"
            Else
                If IsNull(SelectArea) Or IsNull(SelectSection) Or IsNull(DROPSSubSection) Then
                    DoCmd.Beep
                    MsgBox "You must select all the locations first.", vbExclamation, "Incomplete options selected!"
                End If
            End If
        End If
        
    SetClient.SetFocus
        If Forms!Main!SetClient.Value = "Transocean" Then
            DoCmd.OpenForm "EnterItemTransocean", , , stLinkCriteria
        Else
            stDocName = "EnterItemDROPS"
            DoCmd.OpenForm stDocName, , , stLinkCriteria
        End If

TOEnterByAllBtn_Click_Exit:
    Exit Sub

TOEnterByAllBtn_Click_Err:
    MsgBox Error$
    Resume TOEnterByAllBtn_Click_Exit

End Sub
 
Last edited:
Brilliant! It works! Thank you SO much and also thank you for the advice. Much appreciated
 

Users who are viewing this thread

Back
Top Bottom