?
Solved

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

Posted on 2011-02-24
7
Medium Priority
?
285 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 85
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 8

Accepted Solution

by:
crysallus earned 2000 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

771 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