Solved

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

Posted on 2011-02-24
7
282 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:seanlhall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:crysallus
ID: 34976469
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.
0
 
LVL 84
ID: 34976475
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.
0
 

Author Comment

by:seanlhall
ID: 34976518
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Accepted Solution

by:
crysallus earned 500 total points
ID: 34976548
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"
    End If
Else
    DoCmd.OpenForm stDocName1, , , "[casestatus] = " & Me![statuscriteria], , , OpenArgs:=Me.sendingfrm & ";" & Me.statuscriteria
    DoCmd.Close acForm, "frmsubjectedit"
End If

Open in new window

0
 

Author Comment

by:seanlhall
ID: 34976669
Ok, the cancel works now, but if you click ok to return to form, it set focus then closes the form.
0
 
LVL 8

Expert Comment

by:crysallus
ID: 34976701
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.
0
 

Author Closing Comment

by:seanlhall
ID: 34980134
Your code did work, I had a mistake on my end. Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question