Syntax Error in Simple SQL Statement?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jbodford
    New Member
    • Jun 2012
    • 1

    Syntax Error in Simple SQL Statement?

    Hi there!

    I'm trying to group employees into age groups to see percentages per age group. This is my code:

    Code:
    SELECT [CSH Employees].DOB, [CSH Employees].status
    FROM [CSH Employees]
    WHERE (([CSH Employees].status)<>"former")
    
    Public Function AgeGroup(DOB As Date) As String
    
        Dim intAge As Integer
    
        'Age Calculation
        intAge = DateDiff("yyyy", [DOB], Now()) + _
                 Int(Format(Now(), "mmdd") < Format([DOB], "mmdd"))
    
        Select Case intAge
    
            'For each Age range, write out Age Group (used in qry)
        CaseIs < 30
            AgeGroup = "<30"
        Case 30 to 44
            AgeGroup = "30-44"
        Case 45 to 59
            AgeGroup = "45-59"
        Case Is > 59
            AgeGroup = "60+"
         End Select
    
    End Function
    I'm getting this error:

    Syntax error (missing operator) in query expression '(([CSH Employees].status)<>"form er")
    Public Function AgeGroup(DOB As Date) As String
    Dim intAge As Integer
    'Age Calculation
    intAge = DateDiff("yyyy" , [DOB], Now()) + _
    Int(Format(Now( ), "mmdd") < Format([DOB], "mmdd"))

    What am I missing or doing wrong?
    Last edited by Rabbit; Jun 21 '12, 04:07 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    Did you try to use VBA code in SQL? You can't do that. the VBA code needs to go in a module and then you call it from a SQL query. Also, no where in your SQL do you call the function you define.

    Comment

    Working...