Solved

VBA - Change the field name alias in a query

Posted on 2008-10-22
5
1,635 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
[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
  • 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

726 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