Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with ElseIf statement 3

Status
Not open for further replies.

bjt52

Technical User
Sep 1, 2004
37
US
I am new at writing code coding and I hope someone can tell what I am doing wrong.
I am trying to write code to require 3 fields be entered before the form will close, but it will not step through the 3 steps and when you click the ok button it will close the form.
Thanks


If IsNull(Me!customer_name) Then
MsgBox ("Enter the customer name or a contact person!")
Me.common_litel_ckt.SetFocus
End Sub
ElseIf IsNull(Me!customer_acct_id) Then
MsgBox ("Enter a Customer Account.")
Me.customer_acct_id.SetFocus
Exit Sub
ElseIf IsNull(Me!cust_phone) Then
MsgBox ("Enter a Phone Number!.")
Me.project_name.SetFocus
Exit Sub
End If
Forms!frmMain.Requery
Forms!frmMain!subDSView.Form.Requery
End Sub
 
How about...
Code:
Private Sub cmdClose_Click()
If IsNull(Me!customer_name) [COLOR=red]OR If me!customer_name = "" [/color]Then
    MsgBox ("Enter the customer name or a contact person!")
    Me.common_litel_ckt.SetFocus
[s]End Sub[/s]
ElseIf IsNull(Me!customer_acct_id) [COLOR=red]OR If me!customer_acct_id = "" [/color]Then
    MsgBox ("Enter a Customer Account.")
    Me.customer_acct_id.SetFocus
[s]Exit Sub[/s]
ElseIf IsNull(Me!cust_phone) [COLOR=red] OR If me!cust_phone = "" [/color]Then
    MsgBox ("Enter a Phone Number!.")
    Me.project_name.SetFocus
[s]Exit Sub[/s]
End If
[s]Forms!frmMain.Requery
Forms!frmMain!subDSView.Form.Requery[/s]
End Sub

Randy
 
If Len(Trim(Me.customer_name.Text)) = 0 Then
MsgBox ("Enter the customer name or a contact person!")
Me.common_litel_ckt.SetFocus
Exit Sub

ElseIf Len(Trim(Me.customer_acct_id.Text)) = 0 Then
MsgBox ("Enter a Customer Account.")
Me.customer_acct_id.SetFocus
Exit Sub

ElseIf Len(Trim(Me.cust_phone.Text)) = 0 Then
MsgBox ("Enter a Phone Number!.")
Me.project_name.SetFocus
Exit Sub

End If

Forms!frmMain.Requery
Forms!frmMain!subDSView.Form.Requery

End Sub
 
No need for the ElseIfs here...You are checking three separate fields for validity.

Code:
If IsNull(Me!customer_name) Then
    MsgBox ("Enter the customer name or a contact person!")
    Me.common_litel_ckt.SetFocus
    Exit Sub
End If

If IsNull(Me!customer_acct_id) Then
    MsgBox ("Enter a Customer Account.")
    Me.customer_acct_id.SetFocus
    Exit Sub
End If

If IsNull(Me!cust_phone) Then
    MsgBox ("Enter a Phone Number!.")
    Me.project_name.SetFocus
    Exit Sub
End If

Forms!frmMain.Requery
Forms!frmMain!subDSView.Form.Requery

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
mstrmage...

Why continue checking for validity and changing the focus if the first one fails? Nesting the if's will stop the check the first time a required field is found to be missing.



Randy
 
Randy,

First, to be perfeclty honest, I have never been a fan of the elseif clause. I have almost always found it easier to read and logically define either using separate if statements or a select. If you have a good reason/conecpt/example of why and where esleif truly is appropriate, please fill me in.

Second, each of the above ifs I posted had an exit sub in them. This followed the same logic path the elseif originally posted did.

I know my logic is not always the greatest...always looking for input. Thanks.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Let me add to that last a bit, in that I understand where elseif is a bit more efficient than select. I also understand that elseif can evaluate diferrent values than the initial if.

But logically to me, if I am evaluating dissimilar values, why should they be coded together anyway?

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
mstrmage....
Just as you are not a fan of the elseif clause, I don't particularly care for the Exit Sub command. However, the only possible problem I see with your code, is that, since you included Exit Sub in each of the If clauses, the code that follows the last If will never execute unless all required fields are validated.

Also, it appears that we ALL missed an important item. Where and when does the form actually close, which is what bjt52 wants it to do?

bjt52....
Each of the suggestions offered here will work. It's up to you to decide which one you prefer. However, your original post states that you want to validate these entries before the form closes. Therefore, unless I'm missing something, you should include a close command (me.close or forms!formname.close) at the end of the subroutine.


Randy
 
randy and bjt52,

Point taken. Thanks. We all have our preferred/hated things...this was just a headbump over them. I am a fan of one entry/one exit, which I gather you may be as well. But I am also a fan of "as long as the code works, and works in a reasonable time, it is good (not necessarily best) code"

I assumed that bjt52 did not want to form to close or at least that the last two lines should not execute if any of the if checks failed. That is what assuming does...

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Thank you all for such quick responses.. It is working.

I do have another question related to this issue. I have added this code MsgBox("Enter the customer name or a contact person!", vbExclamation + vbOKCancel, "Exit New Entry") = vbOK Then
DoCmd.Close
to the first MsgBox but when I click Ok and the form closes i get this message "The expression you entered refers to an object that is closed or doesn't exist".
I think it is coming from the DoCmd.Close but I am not sure what else to use.
Thanks
 
Try removing the = vbOK from the msgbox code.
You might also change the buttons to simply vbOK. If I remember right, with a cancel button, you'll need additional code to tell your application what to do if the user selects the cancel button.


Randy
 
mstrmage,

You're right... don't know what I was thinking. The form should only close if all fields validate properly.


Randy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top