ElseIf Statement Problem

  • Thread starter Thread starter Rob H
  • Start date Start date
R

Rob H

Hi I am using the following code to try and validate my
form before quitting the application.

I require cbocycle, cbometer, cbosite & txtreading to all
be filled in if the db is quit so the record is only
saved on exit if there is a full record.

There is one exception to this where if all of the above
fields are blank I would like to exit the db as the
record is blank. I have tried to use the elseif
statement to do this but I cant get them to work
together. Is there an empty state I can refer to or does
anyone know how to correct what I have written below?

Thanks

Rob

If Len(Me.[cbocycle] & vbNullString) = 0 Or Len(Me.
[cbometer] & vbNullString) = 0 Or Len(Me.[cbosite] &
vbNullString) = 0 Or Len(Me.[txtreading] & vbNullString)
= 0 Then
MsgBox ("To Quit the application you must clear the form
first or add a complete record to be saved upon exit"),
vbInformation, "Yorkshire Water"
Exit Sub
ElseIf Len(Me.[cbocycle] & vbNullString) = 0 And Len(Me.
[cbometer] & vbNullString) = 0 And Len(Me.[cbosite] &
vbNullString) = 0 And Len(Me.[txtreading] & vbNullString)
= 0 Then
DoCmd.Quit
Else
DoCmd.Quit
End If
 
With If-ElseIf, processing stops as soon as the first True statement is
found.

If all four controls are blank, it'll have been caught by the first If in
the structure.

Try

If Len(Me.[cbocycle] & vbNullString) = 0 And Len(Me.
[cbometer] & vbNullString) = 0 And Len(Me.[cbosite] &
vbNullString) = 0 And Len(Me.[txtreading] & vbNullString) = 0
Then
MsgBox ("To Quit the application you must clear the form
first or add a complete record to be saved upon exit"),
vbInformation, "Yorkshire Water"
Exit Sub
ElseIf Len(Me.[cbocycle] & vbNullString) = 0 Or Len(Me.
[cbometer] & vbNullString) = 0 Or Len(Me.[cbosite] &
vbNullString) = 0 Or Len(Me.[txtreading] & vbNullString) = 0 Then
DoCmd.Quit
Else
DoCmd.Quit
End If

You could also rewrite that first check as

(Len(Me.[cbocycle] & vbNullString) + Len(Me.
[cbometer] & vbNullString) + Len(Me.[cbosite] &
vbNullString) + Len(Me.[txtreading] & vbNullString)) = 0
 
If I understand what you want to do correctly, the problem with your code is
that you will never reach the ElseIf part if any or all of the four controls
are blank/null.

I suggest you check each control for blank null and increment a counter that
tallys how many controls are in that state. Then, if none are blank, save
the record. If all four are blank, close without saving. But if at least
one is not blank, do your message box warning.

TomU
 
Back
Top