+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    02-26-2009
    Location
    India
    MS-Off Ver
    Access
    Posts
    69

    Getting problem in If Then Statement

    Hi everyone,

    I have the following code in the After Update event of my combo box and the On Current event of my main form.

    If I try to open the main form, I get an error message that reads: "Compile error, Block If without End If" (it also highlights the End Sub in blue). If I take the code out of the main form, I still get the same message after an update to my combo box. What have I done wrong?

    Here is my code:

    Code:
    Private Sub cboresponsetype_AfterUpdate()
    If Me.cboresponsetype = "A" Then
    Me.sbfAAdd.Visible = True
    Me.sbfBAdd.Visible = False
    Me.sbfCAdd.Visible = False
    Else
    If Me.cboresponsetype = "B" Then
    Me.sbfAAdd.Visible = False
    Me.sbfBAdd.Visible = True
    Me.sbfCAdd.Visible = False
    Else
    If Me.cboresponsetype = "C" Then
    Me.sbfAAdd.Visible = False
    Me.sbfBAdd.Visible = False
    Me.sbfCAdd.Visible = True
    End If
    End Sub
    Thanks in advance

  2. #2
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: Getting problem in If Then Statement

    ElseIf rather than Else <BR> If

    Code:
    Private Sub cboresponsetype_AfterUpdate()
    If Me.cboresponsetype = "A" Then
        Me.sbfAAdd.Visible = True
        Me.sbfBAdd.Visible = False
        Me.sbfCAdd.Visible = False
    ElseIf Me.cboresponsetype = "B" Then
        Me.sbfAAdd.Visible = False
        Me.sbfBAdd.Visible = True
        Me.sbfCAdd.Visible = False
    ElseIf Me.cboresponsetype = "C" Then
        Me.sbfAAdd.Visible = False
        Me.sbfBAdd.Visible = False
        Me.sbfCAdd.Visible = True
    End If
    End Sub

  3. #3
    Registered User
    Join Date
    02-26-2009
    Location
    India
    MS-Off Ver
    Access
    Posts
    69

    Re: Getting problem in If Then Statement

    Hi DonkeyOte,

    Thanks for your reply but It is showing me syntax error.why this happens?

  4. #4
    Forum Guru mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,984

    Re: Getting problem in If Then Statement

    It is occuring because you have 3 If statements and only 1 end if statement

    You need an End if for every If statement



    By using ElseIf it continues instead of If you are continuing the command from the 1st If statement and not starting 2nd & 3rd If statements - This is what DonkeyOte is showing you in the code he posted

    You do not need to use the Else statement


    As you are testing the same item for multiple results, it is more efficent to use Select Case

    Code:
    Private Sub cboresponsetype_AfterUpdate()
    Select Case Me.cboresponsetype
    Case "A"
       Me.sbfAAdd.Visible = True
       Me.sbfBAdd.Visible = False
       Me.sbfCAdd.Visible = False
    Case "B"
    
       Me.sbfAAdd.Visible = False
       Me.sbfBAdd.Visible = True
       Me.sbfCAdd.Visible = False
    Case "C"
       Me.sbfAAdd.Visible = False
       Me.sbfBAdd.Visible = False
       Me.sbfCAdd.Visible = True
    End Select
    End Sub
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  5. #5
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: Getting problem in If Then Statement

    I think you could probably go one step further...

    Code:
    Private Sub cboresponsetype_AfterUpdate()
    Me.sbfAAdd.Visible = Me.cboresponsetype = "A"
    Me.sbfBAdd.Visible = Me.cboresponsetype = "B"
    Me.sbfCAdd.Visible = Me.cboresponsetype = "C"
    End Sub

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0