If-Then-Elseif 
Author Message
 If-Then-Elseif

How can I get VB to check that everything is OK before continue to the
next process ???

Private Sub Command1_Click()

If txtName = "" Then
MsgBox "You have to write a name", vbExclamation
txtName.SetFocus
ElseIf CmbTime = "" Then MsgBox "You have to write the time",
vbExclamation
CmbTime.SetFocus
ElseIf txtperson = "" Then MsgBox "What kind of person ?", vbExclamation
txtRefperson.SetFocus

End If

    Data1.Recordset.AddNew
    Data1.Recordset.Fields("Name") = txtName.Text
    Data1.Recordset.Fields("Time") = Text5.Text

What I want, is that before VB goto the Data-recordset, the
"If-Then-Elseif" have to be right
Who can I make sure that this happends ??

--
----------------------

----------------------



Mon, 13 Dec 1999 03:00:00 GMT  
 If-Then-Elseif

What you are trying to do is data validation. Data Validation is not
straightforward and can be very involved, depending on the level of validation
you need.

As for your example, your code will work fine to ensure all the fields are not
blank/empty. But, of course, that does not guarantee that the values in the
controls are valid. But if you only want to ensure that the controls are
non-blank, your code is fine. you might try indenting, though, for readability
(and put each subordinate line underneath the ELseIf clause) as shown:

Private Sub Command1_Click()
   If txtName = "" Then
      MsgBox "You have to write a name", vbExclamation
      txtName.SetFocus
   ElseIf CmbTime = "" Then
      MsgBox "You have to write the time", vbExclamation
      CmbTime.SetFocus
   ElseIf txtperson = "" Then
      MsgBox "What kind of person ?", vbExclamation
      txtRefperson.SetFocus
   End If
End Sub

HTH,
Greg DiGiorgio

Quote:

>How can I get VB to check that everything is OK before continue to the
>next process ???

>Private Sub Command1_Click()

>If txtName = "" Then
>MsgBox "You have to write a name", vbExclamation
>txtName.SetFocus
>ElseIf CmbTime = "" Then MsgBox "You have to write the time", vbExclamation
>CmbTime.SetFocus
>ElseIf txtperson = "" Then MsgBox "What kind of person ?", vbExclamation
>txtRefperson.SetFocus

>End If

>    Data1.Recordset.AddNew
>    Data1.Recordset.Fields("Name") = txtName.Text
>    Data1.Recordset.Fields("Time") = Text5.Text

>What I want, is that before VB goto the Data-recordset, the
>"If-Then-Elseif" have to be right
>Who can I make sure that this happends ??



Mon, 13 Dec 1999 03:00:00 GMT  
 If-Then-Elseif

Quote:

> How can I get VB to check that everything is OK before continue to the
> next process ???

Try this:

Private Sub Command1_Click()

If txtName = "" Then
   MsgBox "You have to write a name", vbExclamation
   txtName.SetFocus
   Exit Sub
End If

If CmbTime = "" Then
    MsgBox "You have to write the time", vbExclamation
    CmbTime.SetFocus
    Exit Sub
End If

If txtperson = "" Then
    MsgBox "What kind of person ?", vbExclamation
    txtRefperson.SetFocus
    Exit Sub
End If

    Data1.Recordset.AddNew
    Data1.Recordset.Fields("Name") = txtName.Text
    Data1.Recordset.Fields("Time") = Text5.Text

End Sub



Mon, 13 Dec 1999 03:00:00 GMT  
 If-Then-Elseif

See "Exit" in the Help.

Quote:

> How can I get VB to check that everything is OK before continue to the
> next process ???

> Private Sub Command1_Click()

> If txtName = "" Then
> MsgBox "You have to write a name", vbExclamation
> txtName.SetFocus
> ElseIf CmbTime = "" Then MsgBox "You have to write the time",
> vbExclamation
> CmbTime.SetFocus
> ElseIf txtperson = "" Then MsgBox "What kind of person ?", vbExclamation
> txtRefperson.SetFocus

> End If

>     Data1.Recordset.AddNew
>     Data1.Recordset.Fields("Name") = txtName.Text
>     Data1.Recordset.Fields("Time") = Text5.Text

> What I want, is that before VB goto the Data-recordset, the
> "If-Then-Elseif" have to be right
> Who can I make sure that this happends ??

> --
> ----------------------

> ----------------------

--
The views expressed herein do not represent the views of my employer.

To reach me via e-mail, try (without the asterixes)

Pursuant to US Code, Title 47, Chapter 5, Subchapter II, Sec. 227,
any and all nonsolicited commercial E-mail sent to this address is
subject to a download and archival fee in the amount of $500 US.
E-mailing denotes acceptance of these terms.



Mon, 13 Dec 1999 03:00:00 GMT  
 If-Then-Elseif

Quote:

>How can I get VB to check that everything is OK before continue to the
>next process ???

>Private Sub Command1_Click()

>If txtName = "" Then
>MsgBox "You have to write a name", vbExclamation
>txtName.SetFocus
>ElseIf CmbTime = "" Then MsgBox "You have to write the time",
>vbExclamation
>CmbTime.SetFocus
>ElseIf txtperson = "" Then MsgBox "What kind of person ?", vbExclamation
>txtRefperson.SetFocus

>End If

>    Data1.Recordset.AddNew
>    Data1.Recordset.Fields("Name") = txtName.Text
>    Data1.Recordset.Fields("Time") = Text5.Text

>What I want, is that before VB goto the Data-recordset, the
>"If-Then-Elseif" have to be right
>Who can I make sure that this happends ??

>--
>----------------------

>----------------------

All you need to do is put "Exit Sub" statements after each of your .SetFocus
statements.  That will cause your Data1... statements not to be executed until
all of your data validation succeeds.


Tue, 14 Dec 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. nested if/elseif statements

2. if then elseif problem

3. If Then ElseIf Statements

4. Select case/if then elseif

5. Need confirmation about ElseIf statement

6. Select Case Vs If - ElseIf

7. If Then ElseIf the solution to this bug?

8. If..Elseif vs. Select case

9. If..ElseIf..Else problem

10. Problems with a simple if-elseif

11. IF ELSEIF statements in VBScript?

12. Select case ... OR If...Elseif... ??

 

 
Powered by phpBB® Forum Software