Solved

Access VBA query definition

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

Expert Comment

by:Rey Obrero
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

947 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now