Results 1 to 12 of 12
  1. #1
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246

    Block IF End IF Confusion

    Access 2007, Windows 7, not compiled, not split, skill level medium

    I'm having trouble placing the Block if and End if statements located correctly and keep getting Block IF without End IF errors.

    The user selects a date to schedule a meal and clicks a button. The code below runs. The DFirst statement checks to see if the date is already scheduled. If the date has NOT been scheduled the code should jumb to the stSql Insert statement and write the record.

    If the DFirst finds that a date HAS already been scheduled the user should get a message box allowing them to make a choice to stop or proceed. If they stop they exit the code. IF they proceed they should jump to the stSql Insert statement.

    (One date can have several meals scheduled for it. The user just needs a choice.)



    I am having trouble getting the block if end if statements correct.

    Code:
    Dim Msg, Style, Title, Response, MyString
    
    If IsNull(DFirst("DayAssigned", "tbl_createscheduledmeal1", "DayAssigned = #" & AssignedDate & "#")) = False Then
    ' If isnull=true then the date DOES NOT EXIST in the table and the code should jump to the SQL Update statement
    ' If isnull=false then the date DOES EXIST in the table the code should give the user a choice to proceed or not.
       Msg = "A meal has already been scheduled for this date." & vbCr & vbCr & "Do you want to continue?"
       Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
       Title = "Scheduled Meal Exists"
       Response = MsgBox(Msg, Style, Title)
       If Response = vbNo Then
       End If
       Exit Sub
       Else
          If Response = vbYes Then
               'if yes then Go to stSql Insert Into statement
          Else
                '_____________________________________________ This code works fine.
                stSql = "INSERT INTO Tbl_CreateScheduledMeal1" _
                & "( CategoryID, CategoryName, MealID, MealName,DayAssigned )" _
                & "VALUES (" & IntCatID & ", '" & stCatName & "', " & intMealid & ",'" & stMealName & "', #" & dtScheduledDate & "#);"
                DoCmd.SetWarnings False
                DoCmd.RunSQL stSql
                Me.Requery
                Me.Refresh
                DoCmd.SetWarnings True
                '____________________________________________ This code works fine. 
          End If
    End Sub
    Thanks Phred
    Last edited by June7; 03-09-2012 at 03:33 PM. Reason: Mod edit: add code tags and indenting as best I could

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Instead of
    If IsNull(DFirst("DayAssigned", "tbl_createscheduledmeal1", "DayAssigned = #" & AssignedDate & "#")) = False Then

    I would try

    If Not IsNull(DFirst("DayAssigned", "tbl_createscheduledmeal1", "DayAssigned = #" & AssignedDate & "#")) Then

    Also, I'm not sure what you mean/meant by this line
    Dim Msg, Style, Title, Response, MyString
    All of these variable will be Variant type

    You must explicitly dim variables to get specific data types

    Dim Msg as String, Style as String or
    Dim Title as String
    Dim Response as Integer
    Dim MyString as String.....

  3. #3
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246
    Thanks Orange but the "Not IsNull" statement doesn't seem to work. On testing, it gives me the same result if the Meal Name exists or not. Also I am still getting the BlockIF without EndIF error.

    The "dim msg, style, title, response, my string" I got from another post and it seems to work so I used it exactly as presented.

    Aside from the block if end if problem, I just don't think I have the right test going here to see if the Meal Name already exists. I seem to have problems testing its existance no matter what I do.

    Is there another approach to testing this?

    Thanks Fred

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Post the whole Sub so we can see the entire If End if...

    Your Dim is inconsistent with what you think you are doing.
    Explicitly Dim your variables --- In fact use the Option Explicit for all your modules -- a very good practice.

  5. #5
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    PMFJI,

    Quote Originally Posted by Phred View Post
    Thanks Orange but the "Not IsNull" statement doesn't seem to work. On testing, it gives me the same result if the Meal Name exists or not. Also I am still getting the BlockIF without EndIF error.
    "BlockIF without EndIF error" --- You are missing an "End If" before "End Sub"

    The "dim msg, style, title, response, my string" I got from another post and it seems to work so I used it exactly as presented.
    The message box example in HELP declares the variables the same way. (not the way I would do it, but it works)

    Aside from the block if end if problem, I just don't think I have the right test going here to see if the Meal Name already exists. I seem to have problems testing its existance no matter what I do.

    Is there another approach to testing this?

    Thanks Fred
    Put a breakpoint in the code and single step through it. IMHO the logic was wrong... the response = vbTrue was in the false part of the first IF() statement - response will always be false. Should have been in the True (top part) as part of "If Response = vbNo".
    So I tried rewriting the code - hope you don't mind.



    Here is my attempt:
    Code:
    'Public Sub AddMeal()
    'The user selects a date to schedule a meal and clicks a button. The code below runs.
    'The DFirst statement checks to see if the date is already scheduled.
    'If the date has NOT been scheduled the code should jumb to the stSql Insert statement and write the record.
    
    'If the DFirst finds that a date HAS already been scheduled the user should get a message box allowing them
    ' to make a choice to stop or proceed. If they stop they exit the code.
    'IF they proceed they should jump to the stSql Insert statement.
    
       Dim MealScheduled As Boolean
       Dim Msg, Style, Title, Response, MyString
    
       MealScheduled = Not IsNull(DFirst("DayAssigned", "tbl_createscheduledmeal1", "DayAssigned = #" & AssignedDate & "#"))
       ' If MealScheduled = TRUE   then the date DOES EXIST in the table the code should give the user a choice to proceed or not.
       ' If MealScheduled = FALSE then the date DOES NOT EXIST in the table and the code should jump to the SQL Update statement
    
       If MealScheduled Then
          'MealScheduled True, then ASK
          Msg = "A meal has already been scheduled for this date." & vbNewLine & vbNewLine & "Do you want to continue?"
          Style = vbYesNo + vbCritical + vbDefaultButton2   ' Define buttons.
          Title = "Scheduled Meal Exists"
          Response = MsgBox(Msg, Style, Title)
    
          If Response = vbYes Then   'chose Yes
             'add a meal
             MealScheduled = False
          Else                      'chose No
             'MealScheduled = true so do nothing
          End If
       End If
    
       If MealScheduled Then  ' True
          'do nothing
       Else                            'False
          'Schedule a meal
          '_____________________________________________ This code works fine.
          stSql = "INSERT INTO Tbl_CreateScheduledMeal1" _
                  & "( CategoryID, CategoryName, MealID, MealName, DayAssigned )" _
                  & "VALUES (" & IntCatID & ", '" & stCatName & "', " & intMealid & ",'" & stMealName & "', #" & dtScheduledDate & "#);"
          CurrentDb.Execute stSql, dbFailOnError
          Me.Requery
          Me.Refresh
          '____________________________________________ This code works fine.
       End If
    'End Sub

  6. #6
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Personally I would do the following In the code where you have the Exit Sub I would change it to Goto Exit_Handler.Then at the bottom after you've done all your processingExit_Handler: Exit SubEnd SubI always use that and put any clean up code in the Exit Handler. For example if I'm going through a recordset I will include the rs.close and set rs = nothing. Every Sub Procedure or Function I put into code has the following framework.Public sub sub1() On error goto Err_HandlerExit_Handler: Exit SubErr_Handler: Msgbox err.description, vbokonly,"Sub Procedure sub1 Error" Resume Exit_Handler:End Sub

  7. #7
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Anybody know how I can get these stupid messages to look the way I typed them after I've posted them?????????????????????????????????????????????

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  9. #9
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    no let's see:
    Code:
    Public sub sub1() On error goto Err_HandlerExit_Handler:     Exit SubErr_Handler:     Msgbox err.description, vbokonly,"Sub Procedure sub1 Error"     Resume Exit_HandlerEnd Sub

  10. #10
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,071
    Ok a little better. Still a ways to go. Are those are html tags?

  11. #11
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    no, not specifically but they are similar and used on forums.

    There's also [ quote ] and [ /quote ] which doesn't preserve leading spaces. Do you have CrLf at the end of Lines?

    I think this is what you were going for

    Code:
    Public sub sub1() 
    On error goto Err_Handler
    
    Exit_Handler:    
    Exit Sub
    
    Err_Handler:     
    Msgbox err.description, vbokonly,"Sub Procedure sub1 Error"     
    Resume Exit_Handler
    End Sub
    On this forum the code tags are done by means of the #. JUst highlight the code and hit that # in the formatting area.

    Are you aware of MZTools? It is a free utility at http://www.MZTools.com
    It does many things, one of which is to add an Error handler... but there are many other features.

  12. #12
    Phred is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2009
    Posts
    246

    ThanksThanksThanksThanksThanksThanksThanksThanksTh anksThanks!

    I need to study your code. I don't fully understand it but I do understand that it works. Thanks I was sweating bullets.

    I have marked this thread as solved.

    Thanks

    Phred

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

Similar Threads

  1. Confusion with table relations
    By BusDriver3 in forum Database Design
    Replies: 13
    Last Post: 08-21-2011, 01:48 PM
  2. VB and SQL confusion
    By Wayne311 in forum Programming
    Replies: 29
    Last Post: 01-31-2011, 10:27 PM
  3. Events Confusion?
    By homerj56 in forum Programming
    Replies: 5
    Last Post: 10-27-2010, 01:45 PM
  4. Combo box lookup confusion
    By redpenner in forum Forms
    Replies: 5
    Last Post: 08-19-2010, 08:45 PM
  5. Confusion
    By colenzo in forum Access
    Replies: 4
    Last Post: 07-21-2009, 06:38 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