Results 1 to 4 of 4
  1. #1
    adacpt is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    12

    Inserting a variable into a SQL statement

    Hello,


    I'm calling a macro that passes a SQL statement in as a parameter. Within that statement, I'd like to use the variable EmployeeName as the argument for the Where clause. The code I'm using is below:

    Sub CreateActualHoursQuery()


    Dim EmployeeName As String


    EmployeeName = "Test Employee"


    CreateQry "Actual Hours Test", "SELECT Employees.[Employee Name], Projects.[Project Name], [Project Costs].[Month Number], [Project Resources].[Actual Hours] FROM Projects INNER JOIN ([Project Costs] INNER JOIN (Employees INNER JOIN [Project Resources] ON Employees.ID = [Project Resources].[Employee Name]) ON [Project Costs].ID = [Project Resources].[Monthly Date]) ON Projects.ID = [Project Costs].[Project Name] WHERE (((Employees.[Employee Name])= EmployeeName));"


    End Sub

    Everything works fine, but the query that's created doesn't use the value for EmployeeName that I've declared. Instead, it creates a query parameter that prompts me to enter the parameter whenever I execute the query. How do I write this module, so that it just uses the value for EmployeeName I've specified in code?

    I appreciate any assistance.

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    You have to concatenate the variable into the string, and because it's a text value it needs delimiters. At its simplest:

    Code:
    "..WHERE Employees.[Employee Name]= '" & EmployeeName & "'"
    If the names could contain apostrophes, it would get slightly more complicated. There's a decent tutorial here:

    http://www.baldyweb.com/BuildSQL.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    adacpt is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    12
    Thank you. That's what I was missing!

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    No problemo!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 10-25-2012, 02:53 AM
  2. Replies: 0
    Last Post: 08-10-2011, 11:59 AM
  3. select statement with variable table name
    By dv89k in forum Queries
    Replies: 1
    Last Post: 05-26-2011, 10:54 PM
  4. Help Using Variable in DLookup Statement
    By bcmarshall in forum Access
    Replies: 9
    Last Post: 12-02-2010, 12:44 AM
  5. Replies: 5
    Last Post: 04-05-2010, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums