Solved

VBA - Change the field name alias in a query

Posted on 2008-10-22
5
1,617 Views
Last Modified: 2013-11-28
How do you change the alias name in a query object via VBA? You might ask why; I have one form that I want to display the results from multiple query. Each query has different fields name and I want the one form to be generic enough to handle all the option. So, I have a generic form with 30 fields. Each labaled text1, text2, text3, text4....

The user selects the report type on the main form. In VBA, the process does the following...
1.  looks at the report type to determine which table to use...
2. Filters out the the records need from the results by creating a strSQL
3. Assigns the strSQL to the form.recordsource.

Example
Dim ReportType As String
ReportType = Me.cmb_reporttype
If ReportType = "Positions" Then
strSQL = "select * from WSOReportingAllpositions"
End If
Me!WSOReportingMainResults.Form.RecordSource = strSQL

Since each query has different field values; I want to parse through each and set the alias name automatically... like this...

    Counter = 1
    Set qdf = db.QueryDefs(QueryName)
    For Each fld In qdf.Fields
        FieldValue = fld.Name
        SourceField = fld.SourceField
        Source = fld.SourceTable
       
        If fld.Name Like "text*" Then
        Else
        fld.Name = "text" & Counter & ":" & SourceField
        End If
        Counter = Counter + 1
   Next fld

I am having trouble with this statement: fld.Name = "text" & Counter & ":" & SourceField. It will not allow me to change the field name in the query. For example:

Column 1 has "Asset_Name". I want text1: Asset_Name OR another way "SELECT Asset_Name AS text1, ...

How can I accomplish this? Thanks for the assistance.







0
Comment
Question by:daviddurham
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:EruditeMonkey
ID: 22775978
How about just to changing the recordsource property of each control on the form?
0
 

Author Comment

by:daviddurham
ID: 22776008
I will need to look at this. It may work. Let me try. thanks  
0
 
LVL 2

Expert Comment

by:EruditeMonkey
ID: 22776021
Sorry, that should be ControlSource.
0
 

Author Comment

by:daviddurham
ID: 22776768
I have a question about the solution...

On the result form... the text boxes are named is 'txt_text1, txt_text2, etc.

I have to loop though and look at each text box and set the control source. Here is what I tried...
    Counter = 1
    Set qdf = db.QueryDefs(QueryName)
    For Each fld In qdf.Fields
        FieldValue = fld.Name
        SourceField = fld.SourceField
        Source = fld.SourceTable
       ControlSourceRecord = "Me!WSOReportingMainResults.Form.txt_text" & Counter & ".ControlSource"
       ControlSourceRecord = fld.SourceField
       Counter = Counter + 1
     Next fld

The loop works but is not setting the fld.SourceField. Is there a syntax error

Me. is the main form
WSOReportingMainResults is the subform

thanks










0
 
LVL 2

Accepted Solution

by:
EruditeMonkey earned 500 total points
ID: 22777071
Hmm, I'm not sure about the two lines with ControlSourceRecord. I don't often use subforms so I'm not sure for the syntax, but if the textboxes were on the main form it would be something like this:
Counter = 1
    Set qdf = db.QueryDefs(QueryName)
    For Each fld In qdf.Fields
        FieldValue = fld.Name
        SourceField = fld.SourceField
        Source = fld.SourceTable
       Controls("txt_text" & counter).ControlSource= fld.SourceField  
       Counter = Counter + 1
     Next fld
 
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

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…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
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…

758 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

21 Experts available now in Live!

Get 1:1 Help Now