Solved

Access VBA query definition

Posted on 2012-04-02
4
468 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

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
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…

622 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