?
Solved

VBA - Change the field name alias in a query

Posted on 2008-10-22
5
Medium Priority
?
1,647 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 2000 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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Suggested Courses

800 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