Link to home
Start Free TrialLog in
Avatar of seanlhall
seanlhallFlag for United States of America

asked on

How can I use a If else correctly in a form?

I have an event the fires when a command button is clicked. If the clientid and employeeid are null then a msgbox pops up. You can cancel it and continue or return to the form and correct the problem. That works fine. The problem is when I the clientid and employeeid are no longer null it does not do the ELSE which is to close the form and open another.  
Private Sub Command249_Click()
On Error GoTo Err_Command249_Click
Dim stDocName As String
Dim stLinkCriteria As String
Dim stDocName1 As String
Dim stLinkCriteria1 As String
stDocName1 = Me.sendingfrm
stLinkCriteria1 = Me.statuscriteria
If Me.clientid = "" Or IsNull(Me.clientid) Or Me.employeeid = "" Or IsNull(Me.employeeid) Then
If MsgBox("Return to form, Assign Main Client/Lead Investigator?", vbQuestion + vbOKCancel, "Required data") = vbOK Then
Me.Command1157.SetFocus
Else
DoCmd.OpenForm stDocName1, , , "[casestatus] = " & Me![statuscriteria], , , OpenArgs:=Me.sendingfrm & ";" & Me.statuscriteria
DoCmd.Close acForm, "frmsubjectedit"
Exit_Command249_Click:
   Exit Sub

Err_Command249_Click:
   MsgBox Err.description
   Resume Exit_Command249_Click
    End If
    End If
End Sub

Open in new window

Avatar of crysallus
crysallus
Flag of Australia image

It looks like you've put the end if lines in the wrong place. Try:

If Me.clientid = "" Or IsNull(Me.clientid) Or Me.employeeid = "" Or IsNull(Me.employeeid) Then
    If MsgBox("Return to form, Assign Main Client/Lead Investigator?", vbQuestion + vbOKCancel, "Required data") = vbOK Then
        Me.Command1157.SetFocus
    End If
Else
    DoCmd.OpenForm stDocName1, , , "[casestatus] = " & Me![statuscriteria], , , OpenArgs:=Me.sendingfrm & ";" & Me.statuscriteria
    DoCmd.Close acForm, "frmsubjectedit"
End If

Open in new window

And remove the end if's from the bottom.
Avatar of Scott McDaniel (EE MVE )
Your If - Else - End If blocks are scattered throughout Labels and such, which is NOT a good thing to do.

If Me.clientid = "" Or IsNull(Me.clientid) Or Me.employeeid = "" Or IsNull(Me.employeeid) Then
  If MsgBox("Return to form, Assign Main Client/Lead Investigator?", vbQuestion + vbOKCancel, "Required data") = vbOK Then
    Me.Command1157.SetFocus
  End If
Else
  DoCmd.OpenForm stDocName1, , , "[casestatus] = " & Me![statuscriteria], , , OpenArgs:=Me.sendingfrm & ";" & Me.statuscriteria
  DoCmd.Close acForm, "frmsubjectedit"
End If

And do away with the two "End If" statements at the end of the code block.
Avatar of seanlhall

ASKER

Ok that fix most of the problem. The only problem is if the employeeid and or clientid are null, I get the message box. If I click ok, it set focus to  Me.Command1157.SetFocus. If I click cancel it just closes the msgbox, it is not doing the else.
ASKER CERTIFIED SOLUTION
Avatar of crysallus
crysallus
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, the cancel works now, but if you click ok to return to form, it set focus then closes the form.
As far as I can tell, that code shouldn't close the form if you select ok in the message box. Is it closing the form and opening the next form the same as when you click cancel, or does it just close the form and do nothing else?

If it's just closing the form, and not opening the new form as in the other scenarios, then something else is closing your form, not this code.
Your code did work, I had a mistake on my end. Thanks.