Stop code running if fields blank

Psiren17

Registered User.
Local time
Yesterday, 18:18
Joined
Nov 18, 2005
Messages
31
Ok - really need some help here. I have searched and searched but i think my problem is slightly different to the ones i am finding.

Basically i have a form and i want to force users to fill in one out of 4 tick boxes. If none of them are ticked when they press the close button i want the form to stay open and a message box come up.

I can get the message box part to work but the form still closes down.

The reason for this is that when the form closes it goes to another form that is linked to it by a unique ID number. This works by the following code.

Private Sub BTN_INVIS_Click()

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frm closure"

stLinkCriteria = "[issue ID]=" & Me![Issue ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_BTN_INVIS_Click:
Exit Sub

Err_BTN_INVIS_Click:
MsgBox Err.Description
Resume Exit_BTN_INVIS_Click

End Sub

What i have tried to do is insert a piece of code into the above to generate the message box so it now looks as follows:

Private Sub BTN_INVIS_Click()

If [Transport_Delivery_issue_] = False And [Process_issue_] = False And [Design_issue_] = False And [Supplier_issue_] = False Then

MsgBox "You must complete the liability box before you can close this issue"

Cancel = True

[Design_issue_].SetFocus

End If

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frm closure"

stLinkCriteria = "[issue ID]=" & Me![Issue ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_BTN_INVIS_Click:
Exit Sub

Err_BTN_INVIS_Click:
MsgBox Err.Description
Resume Exit_BTN_INVIS_Click

End Sub

This code checks the boxes and generates the message box if none are ticked - that works great. However it doesnt stop the second part of the code (the move to the next form) from executing if the boxes arent ticked.

Would really appreciate it if someone could help me - i'm fairly new to coding so the whole database is a bit cobbled together (mostly with help from this forum!).
 
in the form events there is an unload event

it looks like

Private Sub Form_Unload(Cancel As Integer)

if youdonotwanttheformtoclose then
cancel = vbcancel
exit sub
end if

End Sub


you need the above code in there to stop the form closing - put whatever tests you need in there, and change the condition to whatever you need, but set cancel to vbcancel to stop the form close.
 
Nearly!

Gemma,

Thanks for speedy reply - your suggestion seems to work partly but has thrown up a different problem.

The way the forms work is that when the first form is closed a second one linked to it opens. When the second one opens it has a macro in its "on load" property which closes down the first one. This was the only way i found to open the second form and close down the first because if i use the standard docmd.close the link to the second form doesnt work. Its probably a really bad way to do it but as i said - its a bit cobbled together!

When i add in your code and press the close button i get an "action failed" message box and my only option is to halt the macro. This then allows the form to close. It seems that i cant use your code and still use the macro to close the form.

Any further advice?
 
i can see that this might halt a macro, but i don't use macros at all. I am not sure how to test and loop within macros.

sorry
 

Users who are viewing this thread

Back
Top Bottom