Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Multiple If Then


Reply
 
Thread Tools Display Modes
  #1  
Old 01-23-2008, 05:54 PM
Jonathan_B Jonathan_B is offline
Regular
 
Join Date: Jan 2004
Location: Winter Park, Florida
Posts: 96
Default Multiple If Then


Not sure if this is the place to post this, but here goes. It is related to a database I am working with.

I have 3 text boxes, 2 standard and 1 multiline text boxes. I am attempting to write some code that will prevent the program from attempting to save if one of more of the textboxes are left blank. I am able to get one of them to work, but when I attemp to add more code for the other boxes, nothing happens.

Here is the code I have for one of the boxes..ideas on the other 2, please.

Private Sub cmdJEntrySave_Click()
If frmJournalEntry.txtEntrDate = " " Then
MsgBox "Entry Sate field can not be empty", vbCritical, "Journal Entry"
frmJournalEntry.txtEntrDate.SetFocus
Else

'Opens database to update/add new records
Dim boolAdding As Boolean

boolAdding = (datJournalSave.Recordset.EditMode = adEditAdd)

datJournalSave.Recordset.Update
datJournalSave.Recordset.AddNew

End If
End Sub

The other two textboxes are named, frmJournalEntry.txtentrytime and frmJournalEntry.textJournal
Reply With Quote
  #2  
Old 01-23-2008, 07:10 PM
JPB JPB is offline
Contributor
 
Join Date: Oct 2004
Posts: 629
Default

You can use the ElseIf statement to perform multiple checks:

Code:
Private Sub cmdJEntrySave_Click()
    If Trim$(frmJournalEntry.txtEntryDate.Text) = "" Then
        MsgBox "Entry Date field can not be empty", vbCritical, "Journal Entry"
        frmJournalEntry.txtEntryDate.SetFocus
    ElseIf Trim$(frmJournalEntry.txtEntryTime.Text) = "" Then
        MsgBox "Entry Time field can not be empty", vbCritical, "Journal Entry"
        frmJournalEntry.txtEntryTime.SetFocus
    ElseIf Trim$(frmJournalEntry.txtJournalEntry.Text) = "" Then
        MsgBox "Journal Entry field can not be empty", vbCritical, "Journal Entry"
        frmJournalEntry.txtJournalEntry.SetFocus
    Else
        'Opens database to update/add new records
        Dim boolAdding As Boolean

        boolAdding = (datJournalSave.Recordset.EditMode = adEditAdd)

        datJournalSave.Recordset.Update
        datJournalSave.Recordset.AddNew

    End If
End Sub
I also made some changes to make the testing code more robust:

1) Elimated Space characters from between your quotes to test for empty strings instead of strings with a space
2) Used the Trim$() function to remove leading and trailing spaces from the test so that just putting spaces in the textboxes won't bypass the test

I also recommend you do some more robust validation (i.e. make sure that date is actually a date, and the time is actually a time), but I'll leave that as an exercise.
Reply With Quote
  #3  
Old 01-23-2008, 07:51 PM
Jonathan_B Jonathan_B is offline
Regular
 
Join Date: Jan 2004
Location: Winter Park, Florida
Posts: 96
Default

Thanks for the help JPB. I compared your code to what I orinally had down and I see where I was making the mistake.

In regards to your suggestion on making sure the date is a date and so forth, I have that in place. I just didn't copy and paste that, since that was working for me.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->