Solved

Variables in Select portion of SQL statement within VBA

Posted on 2014-04-21
2
696 Views
Last Modified: 2014-04-21
Good Afternoon Professionals,

 I need to customize the fields that are being sought after in the below select statement.

I am not to sure of the syntax for that.  The code is below:

' dim array variables
Dim tableone As String
Dim tabletwo As String
Dim thismonth As Variant
 
'Set Variable values
tableone = Me.ListT1.Value
tabletwo = Me.ListT2.Value
thismonth = Me.ListMonths

MsgBox thismonth

'Run query based on changing variables from parent form to be displayed in subform.

Forms![CompareMonthsForm]![CompareMonthsSelQForm].Form.RecordSource =

"SELECT T1.MOR_GROUP ,T1.CHARTDB_BIN as T1CHARTDB_BIN, T2.CHARTDB_BIN as T2CHARTDB_BIN, T1." & thismonth & " as T1 & " & thismonth & ", T2." & thismonth & " as T2" & thismonth & " " & _
"FROM " & tableone & " as T1 " & _
"Left JOIN " & tabletwo & "  as T2 " & _
"ON T1.MOR_GROUP = T2.MOR_GROUP " & _
"WHERE NZ(T1.CHARTDB_BIN, """") = NZ(T2.CHARTDB_BIN, """") " & _
" AND NZ(T1." & thismonth & ", """") <> NZ(T2." & thismonth & ", """");"
'" AND NZ(T1.Feb, """") <> NZ(T2.Feb, """");"

'Refresh the subform post query
Forms![CompareMonthsForm]![CompareMonthsSelQForm].Form.Requery

Question:  Is it possible to concatenate a field name in a select statement in vba?
for example,

Select T1.Jan as T1Jan, T2.Jan as T2Jan

To something like this?

Select T1." & thismonth & " as T1 & " & thismonth & ", T2." & thismonth & " as T1 & " & thismonth & "
0
Comment
Question by:UserName935
2 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 40012964
Well, with a normalized database, you wouldn't really have a field called T1.JAN or T1.Feb, you would have T1.MonthNum and T1.MonthValue,  which would allow you to have as many months as you want, all easily queryable.

But yes, you should be able to do that, although I would probably write it as:
strSQL = "SELECT T1.MOR_GROUP ,T1.CHARTDB_BIN as T1CHARTDB_BIN, " _
              & "T2.CHARTDB_BIN as T2CHARTDB_BIN, " _
              & "T1.[" & thismonth & "] as [T1 & " & thismonth & "], " _
              & "T2.[" & thismonth & "] as [T2" & thismonth & "] " _
       & "FROM [" & tableone & "] as T1 " _
       & "Left JOIN [" & tabletwo & "] as T2 " _
       & "ON T1.MOR_GROUP = T2.MOR_GROUP " _
       & "WHERE NZ(T1.CHARTDB_BIN, """") = NZ(T2.CHARTDB_BIN, """") " _
       & "  AND NZ(T1.[" & thismonth & "], """") <> NZ(T2.[" & thismonth & "], """")" _
       & "  AND NZ(T1.Feb, """") <> NZ(T2.Feb, """");"

Open in new window

0
 

Author Comment

by:UserName935
ID: 40013042
Yes, I agree, we are just beginning.  Prior to seeing the above comment, I was able to get this function with the code below:

' dim array variables
Dim tableone As String
Dim tabletwo As String
Dim thismonth As Variant
 
'Set Variable values
tableone = Me.ListT1.Value
tabletwo = Me.ListT2.Value
thismonth = Me.ListMonths

'Run query based on changing variables from parent form to be displayed in subform.
Forms![CompareMonthsForm]![CompareMonthsSelQForm].Form.RecordSource = "SELECT T1.MOR_GROUP ,T1.CHARTDB_BIN as T1CHARTDB_BIN, T2.CHARTDB_BIN as T2CHARTDB_BIN, T1." & thismonth & " as T1Feb ,T2." & thismonth & " as T2Feb " & _
"FROM " & tableone & " as T1 " & _
"Left JOIN " & tabletwo & "  as T2 " & _
"ON T1.MOR_GROUP = T2.MOR_GROUP " & _
"WHERE NZ(T1.CHARTDB_BIN, """") = NZ(T2.CHARTDB_BIN, """") " & _
" AND NZ(T1." & thismonth & ", """") <> NZ(T2." & thismonth & ", """");"



'Refresh the subform post query
Forms![CompareMonthsForm]![CompareMonthsSelQForm].Form.Requery

MsgBox "Done "

There is still some clean up to do and the field name referenced in the Select portion of the statement will be updated to also use the month variable.
I will try and use the suggested syntax.
without going out of the boundaries to much.  I wonder, would it be possible to create an additional piece of functionality to this by adding a feature in which a user can decide what percentage of variance a person wishes to display?
For example, the above query simply shows a comparative difference between like fields of two tables with the exact same structure.  would it be possible to add a text box on the main form in which a user would put in a number, such as, 25, which would signify 25% so that the query would only show those differing records that have a variance of 25% or more?
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Viewers will learn how the fundamental information of how to create a table.

757 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