To All . . .
I was requested as cook for the holidays and had to get busy. I've been monitoring the thread although meal time preparation & cooking became full time. Now that its over I can comitt my full attention.
I'm going to explain why things are the way they are, but I'd first like to say the code I posted was simply quick & dirty . . . as it proved to be. Normally giving more full thought, I would've posted as [blue]PHV[/blue] suggested. A check of threads where I've posted the same but in more proper logical format proves this out ([blue]missinglinq[/blue] I'm sure if you triple check you'll find this to be so!).
In any case, the reason . . .
Code:
[blue]If ctl.Tag = "?" And Trim(ctl & "") = "" Then[/blue]
. . . fails is because of the following:
[blue][tt]
Quote Microsoft: When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence.
When expressions contain operators from more than one category, arithmetic operators are evaluated first, comparison operators are evaluated next, and logical operators are evaluated last. Comparison operators all have equal precedence; that is, they are evaluated in the left-to-right order in which they appear. Arithmetic and logical operators are evaluated in the following order of precedence:
Priority1 Priority2 Priority3
Arithmetic Comparison Logical
********************************** ***************************** *********
Exponentiation (^) Equality (=) Not
Negation (-) Inequality (<>) And
Multiplication and division (*, /) Less than (<) Or
Integer division (\) Greater than (>) Xor
Modulus arithmetic (Mod) Less than or equal to (<=) Eqv
Addition and subtraction (+, -) Greater than or equal to (>=) Imp
String concatenation ([red]
&[/red]) Like
Is[/tt][/blue]
If we take a good look at that line of code and the [blue]Priority1[/blue] column (in order of precedence from top to bottom), we come up with ampersand [red]
&[/red] [blue]as the first priority[/blue].
This causes the 2nd logical expression [blue]Trim(ctl & "") = ""[/blue] to be evaluated 1st! . . . and herein lies the problem. Not only can this be any control without the tag property set, but it could be any other object on the form! . . . A command button will surely fail here!
Best I can do is offer my apologies for the confusion and correct the code.
So . . . [blue]
mrkshpntf[/blue] here ya go:
Code:
[blue]Dim Msg As String, Style As Integer, Title As String
Dim DL As String, ctl As Control
DL = vbNewLine & vbNewLine
For Each ctl In Me.Controls
[purple][b]If ctl.Tag = "?" Then
If Trim(ctl & "") = "" Then[/b][/purple]
Msg = "'" & ctl.Name & "' is Required!" & DL & _
"Please enter a value or hit Esc to abort the record . . ."
Style = vbInformation + vbOKOnly
Title = "Required Data Missing! . . ."
MsgBox Msg, Style, Title
ctl.Name.SetFocus
Cancel = True
Exit For
End If
End If
Next[/blue]
[blue]Cheers All! . . . and a happy ThanksGiving! . . .[/blue]
See Ya! . . . . . .
Be sure to see thread181-473997