Multiple If Then

Jonathan_B
01-23-2008, 06:54 PM
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

JPB
01-23-2008, 08:10 PM
You can use the ElseIf statement to perform multiple checks:


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.

Jonathan_B
01-23-2008, 08:51 PM
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.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum