PMFJI,
Originally Posted by
Phred
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