Compile Error in query expression in Access 2010

Posted on 2012-08-28
Last Modified: 2012-08-28
I put an internal messaging system in my access database so the users can send me (Admin) a message if need something. When I click on my frm_ReceiveMail, it gives me the Compile Error in query expression '(((TBL_Message.To)=CurrentUser()) AND ((TBL_Message.DateReceived) Is Null)'

I have a query that has To and DateReceived. The critera for the To field is CurrentUser() and DateReceived criteria is Is Null.

Does anyone know what this error is with a solution?
Question by:lauriecking0623
    LVL 22

    Expert Comment

    by:Kelvin Sparks
    I suspect this is the CurrentUser() bit. This always used to related to the logged in user when Access had security. The concept of users has now gone - unless you still use mdb's and mdw's.

    You would need code to pickup the windows username and put that in its place.

    LVL 56

    Accepted Solution

    CurrentUser() is still a valid function call.  Are you building this SQL statement in code?  If not, then you most likely have broken references.  Open a module, bring up the debug window and type:

    ? CurrentUser()

    and hit return.  You should get admin back.  If you get an error, then it's references for sure.

     If you are building this select in code, then your syntax needs to be different.


    Author Comment


    How would I do this code to pick up the windows username? I am still trying to learn how to call these references in VBA?


    Author Closing Comment

    @JDettman, this worked. I do not get this error. I get a new error; however, I am going post this as a new question. Thank you for your assistance.
    LVL 56

    Expert Comment

    by:Jim Dettman (Microsoft MVP/ EE MVE)
    Here's the code for getting the user name (or station name).

    You'll need to add the PtrSafe for the 64 bit.


    Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long
    Private Declare Function GetUserNameA Lib "advapi32.dll" (ByVal lpBuffer As String, nSize As Long) As Long

    Public Function WhoAmI(bReturnUserName As Boolean) As String

            ' Function returns either user name or computer name

            Dim strName As String * 255

    10      If bReturnUserName = True Then
    20        GetUserNameA strName, Len(strName)
    30      Else
    40        GetComputerNameA strName, Len(strName)
    50      End If

    60      WhoAmI = left$(strName, InStr(strName, vbNullChar) - 1)

    End Function

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    In the previous article, Using a Critera Form to Filter Records (, the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
    Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now