Solved

VBA - Change the field name alias in a query

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

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…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

724 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