Solved

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

Posted on 2011-02-24
7
281 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
  • 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
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.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

820 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