Results 1 to 9 of 9
  1. #1
    justair07 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2013
    Location
    Flordia
    Posts
    63

    Complie error "End If without Block If

    Hello,



    I'm attempting to write my first code in access but am running into an issue. I'm getting an "End IF without Block IF" error. Can someone please take a look at my short code and tell me what I'm doing wrong. I'm trying to learn.

    Code:
    Private Sub Command159_Click()
    MsgBox "Any data you entered will not be saved!", vbOKCancel
    If vbOKCancel = 1 Then Undo
    If vbOKCancel = 2 Then DoCmd.Close acForm, "frmDataEntry", acSaveNo
    Exit Sub
    End If
    End Sub
    Thank you,

  2. #2
    justair07 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2013
    Location
    Flordia
    Posts
    63
    Sorry I typed in my code wrong and just realized it. It's backwards

    This is more like it

    Code:
    Private Sub Command159_Click()
    MsgBox "Any data you entered will not be saved!", vbOKCancel
    If vbOKCancel = 1 Then Undo
    DoCmd.Close acForm, "frmDataEntry", acSaveNo
    If vbOKCancel = 2 Then
    Exit Sub
    End If
    End Sub

  3. #3
    justair07 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2013
    Location
    Flordia
    Posts
    63
    for some reason when I click cancel (2) the form still closes. Can someone please tell me what I did wrong?

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Sometimes the "Block If" error can be misleading. In this case you can take it literally.
    Also, looks like you may need a bucket to collect the value of your msgbox.

    Code:
    Private Sub Command159_Click()
    
    Dim intResponse as integer
    
    intResponse = MsgBox "Any data you entered will not be saved!", vbOKCancel
    
    If intResponse = vbOKCancel Then
     Undo
    DoCmd.Close acForm, "frmDataEntry", acSaveNo
    
    elseIf intResponse = vbOKCancel Then
    Exit Sub
    
    End If
    
    End Sub
    Try that and see if it works

    Edit, noticed some other stuff too. Deleted a copy things

  5. #5
    justair07 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2013
    Location
    Flordia
    Posts
    63
    Perfect, all of it makes sense as well except one thing. Why does it work without stating vbOkCancel = 1 or VBOkCancel = 2 statements?

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Not sure what results you are getting. I am not currently at an IDE to test it. I will guess that all you are doing is asking Access to evaluate constants and one of the evaluations/validations is executing.

    If you look at the VBA help files for message box constants you can see that vbOK, vbYes, vbNo all have number equivilants. These are considered constants. In other words vbOkCancel always equals a value that is of the integer data type, a constant. Not sure what it is. Let's say it is = to 1. When you evaluate that using your If Then statement, the user input has nothing to do with it. You can not change the value of the constants in the vb library.

  7. #7
    justair07 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2013
    Location
    Flordia
    Posts
    63
    Ok, thank you. The code is working perfect.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    If that is confusing, consider this.....

    Look up what a data type "Integer" is. Then ask yourself how a variable declared as an integer can equal something like vbOK. After all, isn't vbOK text? It looks like text.

    In the code I proveded to you I declared a variable. This line here
    Dim intResponse as integer

    because intResponse is an integer it can only equal numbers. It can only equal numbers that "fit into' an integer data type. Even though intResponse is a variable and is available to get assigned a value, you can not assign the wrong data type to it. You can not assign "Cat" for instance. Cat is text and does not fit in a number type.

    So how does intResponse = vbOK ? ?

    The answer is vbOK is really an integer as far as Access is concerned. Behind the scenes Access is told that vbOK is really 0 and vbOKCancel = 1 and vbYesNo = 4. They are all constants. VBA does this because it is easier for us humans to remember and type vbYesNo than to remeber, "Use the value 4 when assigning a message box as a Yes or No type message box."

  9. #9
    justair07 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2013
    Location
    Flordia
    Posts
    63
    Makes perfect sense when you put it that way. Thank you or taking the time to make it clear to me. Huge help!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 03-14-2013, 12:39 PM
  2. Replies: 4
    Last Post: 07-25-2012, 04:01 AM
  3. Replies: 13
    Last Post: 06-12-2012, 09:52 PM
  4. "Group By" causes "ODBC--Call Failed" error
    By kaledev in forum Queries
    Replies: 1
    Last Post: 03-09-2011, 02:43 PM
  5. Replies: 2
    Last Post: 12-02-2010, 02:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums