That's not quite it Cyd, and I'll explain as even minor confusions in this area (such an important area within database work) can cause you so many problems.
The point is that everything between the quotes (") in VBA is actually a string. The ampersand (&) sticks two strings together, and when used with numbers this causes the number to be converted to a string automatically so
that string can be added to the other to create the bigger (longer generally) string.
Consider the difference between :
and
- "[lngEmpID] = " & LoginID
The former is a single string which results in
[lngEmpID] = LoginID. The latter, if we assume for the moment an example value of the Me.LoginID control of your form to be 32 for instance, results in
[lngEmpID] = &
32 or
[lngEmpID] = 32.
When we look at these two scenarios at the point where these strings are passed over to SQL (via the
DLookup() call) we see they both start with
[lngEmpID] = . This is fine as the fieldname [lngEmpID] is recognised by SQL. All good so far.
Now we get to the difference between the two versions of the SQL command string. The first compares it to a value
LoginID. SQL has no way of understanding this reference as it is actually a control on a form, but not fully specified. SQL wets its pants and stops working. In the second version though, it sees a simple numeric value - 32 - and is happy to continue processing.
The second version works because we have VBA referencing the value of Me.LoginID before adding the result to the other part of the string.