Solved

Access VBA query definition

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

803 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