Solved

VBA - Change the field name alias in a query

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

785 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