Results 1 to 5 of 5
  1. #1
    ser01 is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Feb 2010
    Posts
    64

    Changin report's fileds with VBA

    I have a report, that is based on a query. I want all the fields in the query to appear on the report. The query uses 3 tables for its data, and one of tables in constantly being deleted/recreated from vba and primary key is being assigned to one of the fields. Each time the table is recreated, 2 of the fields that are in the query, have their names and data changed. My problem is that when that happens, the query works fine since its SQL is being changed in VBA to reflect the new changes, but the report shows an error in the fields that have been changed in the table. Is there any way for me to dynamically change the report's fields, to the once used in the query.
    I know that this all sounds wrong, but it is the only way to do what I need done. Any suggestions would be much appreciated.
    Last edited by ser01; 04-14-2011 at 12:55 PM.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    I see a couple of solutions. One would be in your VBA that changes the query. You can use aliases to keep the field names in the report constant. Instead of

    SELECT NewFieldName FROM ...

    you'd have

    SELECT NewFieldName AS OldFieldName FROM ...

    and the report would see the old field names. Second, you can have code in the report's open event that opens a recordset on the report's source, and loop through the Fields collection to get the field names, with which you can set textbox control sources.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ser01 is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Feb 2010
    Posts
    64
    Quote Originally Posted by pbaldy View Post
    I see a couple of solutions. One would be in your VBA that changes the query. You can use aliases to keep the field names in the report constant. Instead of

    SELECT NewFieldName FROM ...

    you'd have

    SELECT NewFieldName AS OldFieldName FROM ...

    and the report would see the old field names. Second, you can have code in the report's open event that opens a recordset on the report's source, and loop through the Fields collection to get the field names, with which you can set textbox control sources.
    I'll give that a try and report back, Thank you very much for your speedy reply, much appreciated.

  4. #4
    ser01 is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Feb 2010
    Posts
    64
    And to clarify a bit, would I make this changes (use aliases) in the query's sql or on the report side?

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Yes, the aliases would be done in the query's SQL. Doing that, the report shouldn't require any changes or code. It will always see the same field names.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to two same fileds on the same form
    By plavookins in forum Forms
    Replies: 1
    Last Post: 04-11-2011, 03:17 AM
  2. Replies: 4
    Last Post: 12-13-2010, 05:33 PM
  3. On format, fileds that can be used
    By markjkubicki in forum Reports
    Replies: 1
    Last Post: 10-22-2010, 10:56 AM
  4. Replies: 1
    Last Post: 10-10-2010, 05:30 AM
  5. Replies: 2
    Last Post: 08-25-2010, 01:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums