criterion on close command button

G

Guest

I have entered an if statement with a msgbox if the value of a text field is
outside of a specified range (between 0.5 and 5.0) and the memo box
"Corrective Action Taken" is null. The msgbox is working, but as soon as you
press OK on it, the form closes. What I need for it to do is set the focus
back on the memo box "Corrective Action Taken" and not close the form until
the criterion is met.

My code is as follows:


Private Sub Closecmd_4_Mill_Click()
On Error GoTo Err_Closecmd_4_Mill_Click

If ((Stack_No_18_Air < 0.5) Or (Stack_No_18_Air > 5#)) And
(Stack_No_18_Action = "") Then
MsgBox "Corrective Action must be entered if reading is less than 0.5 or
greater than 5.0."
Closecmd_4_Mill.Enabled = False
Stack_No_18_Action.SetFocus
End If
If ((Stack_No_16_Air < 0.5) Or (Stack_No_16_Air > 5#)) And
(Stack_No_16_Action = "") Then
MsgBox "Corrective Action must be entered if reading is less than 0.5 or
greater than 5.0."
Closecmd_4_Mill.Enabled = False
Stack_No_16_Action.SetFocus
End If
If ((Stack_No_17_Air < 0.5) Or (Stack_No_17_Air > 5#)) And
(Stack_No_17_Action = "") Then
MsgBox "Corrective Action must be entered if reading is less than 0.5 or
greater than 5.0."
Closecmd_4_Mill.Enabled = False
Stack_No_17_Action.SetFocus
Else
DoCmd.Close
End If

Exit_Closecmd_4_Mill_Click:
Exit Sub

Err_Closecmd_4_Mill_Click:
MsgBox Err.Description
Resume Exit_Closecmd_4_Mill_Click
End Sub

Thank you in advance for your help!
 
G

Guest

Hi Sherrik,

to do this quickly, I just created a boolean, set it to true. If any of
these IF statements run, I set it to False, and only run the close command if
it is true.

Private Sub Closecmd_4_Mill_Click()
On Error GoTo Err_Closecmd_4_Mill_Click
Dim blnClose As Boolean

blnClose = True

If ((Stack_No_18_Air < 0.5) Or (Stack_No_18_Air > 5#)) And
(Stack_No_18_Action = "") Then
MsgBox "Corrective Action must be entered if reading is less than 0.5 or
"greater than 5.0."
Closecmd_4_Mill.Enabled = False
blnClose = False
Stack_No_18_Action.SetFocus
End If
If ((Stack_No_16_Air < 0.5) Or (Stack_No_16_Air > 5#)) And
(Stack_No_16_Action = "") Then
MsgBox "Corrective Action must be entered if reading is less than 0.5 or
greater than 5.0."
Closecmd_4_Mill.Enabled = False
blnClose = False
Stack_No_16_Action.SetFocus
End If
If ((Stack_No_17_Air < 0.5) Or (Stack_No_17_Air > 5#)) And
(Stack_No_17_Action = "") Then
MsgBox "Corrective Action must be entered if reading is less than 0.5 or
greater than 5.0."
Closecmd_4_Mill.Enabled = False
blnClose = False
Stack_No_17_Action.SetFocus
End If
if blnClose then
DoCmd.Close
End If

Exit_Closecmd_4_Mill_Click:
Exit Sub

Err_Closecmd_4_Mill_Click:
MsgBox Err.Description
Resume Exit_Closecmd_4_Mill_Click
End Sub
 
G

Guest

msgboxes are DIALOG bopxes, and close the moment they're clicked on. You
cannot control that action. You can, however, make up a form of your own that
LOOKS like a msgbox, and then you can do whatever you want with it...
 
G

Guest

I've copied this code in and replaced the original code. Now, the msgbox is
not working and the form is still closing.
 
G

Guest

Hi Sherrik,

Try just including the new code, do not copy and paste mine. While posting,
there have been some formatting losses that make the Message in the MsgBox
fail. Sorry about that I didn't realize it went back a bit differently then
what I copied out.

Basically, you are looking to mark something if the validation fails, and if
that mark exists, do NOT run the .Close command. I chose a boolean.

Try going back to your code, and only insert

Dim blnClose As Boolean

The blnClose = False statements.

And

if blnClose then
DoCmd.Close
End If
 
G

Guest

I've tried both suggestions. Even when I typed in the code as presented,
there was no change in the behavior. When I click OK on the MsgBox, the form
itself still closes.

I've also tried calling a form to replace the message box, but I'm not
getting the code right. Now, when I click on the command button on the form,
it simply sits there and does not close nor does it call the form I created
to replace the MsgBox. This represents the last change, where I've only
placed the open form code in the first if statement so far:

Private Sub Closecmd_4_Mill_Click()
On Error GoTo Err_Closecmd_4_Mill_Click
Dim blnClose As Boolean

blnClose = True

If ((Stack_No_18_Air < 0.5) Or (Stack_No_18_Air > 5#)) And
Len(Stack_No_18_Action <= 4) Then
DoCmd.OpenForm FormName:="CorrectiveActionRequired", view:=acNormal,
windowmode:=acDialog
'Closecmd_4_Mill.Enabled = False
'blnClose = False
Stack_No_18_Action.SetFocus
End If
Exit Sub
If ((Stack_No_16_Air < 0.5) Or (Stack_No_16_Air > 5#)) And
(Stack_No_16_Action = "") Then
Load CorrectiveActionRequired
Closecmd_4_Mill.Enabled = False
blnClose = False
Stack_No_16_Action.SetFocus
End If
If ((Stack_No_17_Air < 0.5) Or (Stack_No_17_Air > 5#)) And
(Stack_No_17_Action = "") Then
Load CorrectiveActionRequired
Closecmd_4_Mill.Enabled = False
blnClose = False
Stack_No_17_Action.SetFocus
End If
If blnClose = True Then
DoCmd.Close
End If

Exit_Closecmd_4_Mill_Click:
Exit Sub

Err_Closecmd_4_Mill_Click:
MsgBox Err.Description
Resume Exit_Closecmd_4_Mill_Click
End Sub
 
Top