Solved

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

Posted on 2011-02-24
7
279 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 8

Accepted Solution

by:
crysallus earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Your code did work, I had a mistake on my end. Thanks.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now