Solved

Access VBA query definition

Posted on 2012-04-02
4
462 Views
Last Modified: 2012-05-25
I am dynamically creating field names for a query definition that is used to export data to an excel sheet. The field names are the last three months names. Everything works fine apart from the month names which are created in quotes. How should I write the query to exclude the quotes?

The query writes the three dynamically created month names as:
‘Jan Use’       ‘Feb Use’      ‘Mar Use’

I want:
Jan Use       Feb Use            Mar Use


Dim Monthname1 As String
Monthname1 = Format(DateAdd("m", -1, Date), "MMM") & " Use"
Dim Monthname2 As String
Monthname2 = Format(DateAdd("m", -2, Date), "MMM") & " Use"
Dim Monthname3 As String
Monthname3 = Format(DateAdd("m", -3, Date), "MMM") & " Use"
Dim qd As DAO.QueryDef
Dim Querytxt As String
CurrentDb.QueryDefs("qryCurrentRecords").SQL = "SELECT tblCurrentStock.[size], tblCurrentStock.ply, tblCurrentStock.[LST W/K], tblCurrentStock.[MTH/2DA] , tblCurrentStock.[1Mth] As '" & Monthname1 & "'  , tblCurrentStock.[2Mth] As '" & Monthname2 & "' , tblCurrentStock.[3Mth] As '" & Monthname3 & "' , Round(([1Mth]+[2Mth]+[3Mth])/3) AS [Avg], Round([stock]/12) AS [Mths lft], tblCurrentStock.Stock FROM tblCurrentStock"
0
Comment
Question by:JoeBo747
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 37796044
Did you remove the single quotes from your query?

Dim Monthname1 As String
Monthname1 = Format(DateAdd("m", -1, Date), "MMM") & " Use"
Dim Monthname2 As String
Monthname2 = Format(DateAdd("m", -2, Date), "MMM") & " Use"
Dim Monthname3 As String
Monthname3 = Format(DateAdd("m", -3, Date), "MMM") & " Use"
Dim qd As DAO.QueryDef
Dim Querytxt As String
CurrentDb.QueryDefs("qryCurrentRecords").SQL = "SELECT tblCurrentStock.[size], tblCurrentStock.ply, tblCurrentStock.[LST W/K], tblCurrentStock.[MTH/2DA] , tblCurrentStock.[1Mth] As " & Monthname1 & "  , tblCurrentStock.[2Mth] As " & Monthname2 & " , tblCurrentStock.[3Mth] As " & Monthname3 & " , Round(([1Mth]+[2Mth]+[3Mth])/3) AS [Avg], Round([stock]/12) AS [Mths lft], tblCurrentStock.Stock FROM tblCurrentStock"
0
 
LVL 77

Expert Comment

by:peter57r
ID: 37796053
Yu have explicitly put the quotes into your query so the obvious answer is to remove them.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37796068
test this


Dim Monthname1 As String
Monthname1 = Format(DateAdd("m", -1, Date), "MMM") & " Use"
Dim Monthname2 As String
Monthname2 = Format(DateAdd("m", -2, Date), "MMM") & " Use"
Dim Monthname3 As String
Monthname3 = Format(DateAdd("m", -3, Date), "MMM") & " Use"
Dim qd As DAO.QueryDef
Dim Querytxt As String
CurrentDb.QueryDefs("qryCurrentRecords").SQL = "SELECT tblCurrentStock.[size], tblCurrentStock.ply, tblCurrentStock.[LST W/K], tblCurrentStock.[MTH/2DA] , tblCurrentStock.[1Mth] As [" & Monthname1 & "]  , tblCurrentStock.[2Mth] As [" & Monthname2 & "] , tblCurrentStock.[3Mth] As [" & Monthname3 & "] , Round(([1Mth]+[2Mth]+[3Mth])/3) AS [Avg], Round([stock]/12) AS [Mths lft], tblCurrentStock.Stock FROM tblCurrentStock"



you need the [] brackets since your aliases have spaces
0
 
LVL 10

Expert Comment

by:plummet
ID: 37796069
Hi,

It's because you are adding single quotes in the SQL definition. However if you left them out you'd get an error because there's a space in the name, so you need to enclose them in square brackets:

CurrentDb.QueryDefs("qryCurrentRecords").SQL = "SELECT tblCurrentStock.[size], tblCurrentStock.ply, tblCurrentStock.[LST W/K], 
tblCurrentStock.[MTH/2DA] , tblCurrentStock.[1Mth] As [" & Monthname1 & "]  , tblCurrentStock.[2Mth] As [" & Monthname2 & "] , 
tblCurrentStock.[3Mth] As [" & Monthname3 & "] , Round(([1Mth]+[2Mth]+[3Mth])/3) AS [Avg], Round([stock]/12) AS [Mths lft], 
tblCurrentStock.Stock FROM tblCurrentStock"

Open in new window


I hope that helps
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of utilizing SQL Server views 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 Access…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

751 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