Results 1 to 11 of 11
  1. #1
    XenoZephyr is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    16

    Question basic: How do I simply get data from a field on a report using VBA?

    I apologize, I feel that I should be able to figure this out but I'm missing something. I've searched online and on this forum and couldn't find the answers though I think I'm not using the correct language because I think this should be a very simple answer.



    I have an Access 2010 database. We have a student records table, with fields like FirstName, LastName, and DatabaseID and also fields for Permanent Address and Current Address, and a true/false field of SameAsPerm to indicate if both addresses are the same.

    From a form, we have a button that opens up the report in order to print it
    strReportName = "rptStudent"
    strCriteria = "[DatabaseID]=" & Me![DatabaseID]
    DoCmd.OpenReport strReportName, acViewPreview, , strCriteria

    On the report I need to check if SameAsPerm is true, then use the Permanent Address else use CurrentAddress, but when I try to reference the fields that should be there, all I get is errors.

    Do I have to write code to create a recordset or a recordsetclone and then reference a field within that?? If the report has the data, I want to get to it directly. Thanks, I appreciate any help!




    When I try to reference a field within the Report_Load sub I get errors
    for example
    ID = Me![DatabaseID] gives error saying that it can't find that field referenced within your expression

    My report data source is set to SELECT Students.* FROM Students, and I'm only referencing fields within that Students table.

    Am I running into problems because it's on the Load section?

    What is the easiest straightforward way to read information from your table within a report? I have to think this is really straightforward and so I'm very confused as to why I can't get this to work at all.

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You need to include a report control for each field you want to reference. Have you?

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I would probably have textboxes with control sources like:

    =IIf(SameAsPerm = True, PermanentAddress, CurrentAddress)

    You could do something similar in the report's source query.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    XenoZephyr is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    16
    No, I am trying to use unbound fields and then populate them, for example. I have a txtbox called txtFullName and I put the code:
    txtFullName = me![FirstName] & " " & me![LastName] and it says that it can't find the fields FirstName and LastName...even though the recordsource of the report includes all of the fields from that table...

    So I need to have a txtfield on the report and then set it to visible = false if I don't want it to show up? That seems bizarre and inefficient...?

  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,521
    No offense, but it seems "bizarre and inefficient" to use unbound fields and then populate them with code. The report's record source includes the data, so why use unbound textboxes?

    It looks like Allan understood the question better than I did, and I didn't realize he had already answered (my apologies), so I'll get out of the way.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I think you are doing a marvelous job on this one Paul. Please continue.

  7. #7
    XenoZephyr is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    16
    Sorry I wasn't trying to insult anyone by saying bizarre and inefficient, it was not a comment on anyone's programming. I said it because I'm just very confused by the difference between a Form and a Report. On a form, I can say ID = Me![DatabaseID] and it works fine, I can get data from any field that I want. On a report though it doesn't work so I felt like I was doing something wrong and/or missing something.

    My reason for doing it this way is because we have 2 versions (Permanent vs Current) of 5 fields, Add1, Add2, City, State, and Zip. Plus we have Country for the Permanent address but not for Current because we know that's within the USA. So I wanted to check check once and make all the assignments in a batch instead of checking 5 times. The final output needs to be in the proper format of
    Add1
    Add2
    City, State, Zip
    Country

    But skip the space for Add2 if that's blank and skip the space for Country if it's not needed...I can do that easily with building it with VBA code, but if I do it by controls, then I'll need to figure out a way to have Add2 somehow not on the report so that there's not a weird space there. How can I check to see if Add2 is null if I have to include a txtbox including Add2 regardless? That seems like a catch-22 and leaves me lost and confused. I hope I don't sound argumentative, I appreciate all the feedback, I'm just frustrated because this seems like it should be a very basic step in Access and I'm very confused by it. Plus I'm fairly certain I had it working at one point, directly getting data from the fields and made changes and when I thought I undid everything I couldn't get it working again. But it's possible I'm thinking of something else.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I've got this in the format event of the section containing the control on a report:

    Code:
      Me.txtCustName = (Me.CustAttn + vbCrLf) & _
                       (Me.CustomerName + vbCrLf) & _
                       (Me.CustomerAddress1 + vbCrLf) & _
                       (Me.CustAddress2 + vbCrLf) & _
                       (Me.CustAddress3 + vbCrLf)
    But as Allan mentioned, the referenced fields have controls on the report that are hidden. It does seem goofy to need them, but I've had the same experience he has.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I should have added that the mix of "&" and "+" is a trick that handles the potentially Null fields.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    XenoZephyr is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    16
    Got it all working, thanks again for all the help! I don't know enough that this is a path I would have found on my own!

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem, we were happy to help!
    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. Replies: 2
    Last Post: 03-07-2013, 04:50 PM
  2. simply adding a field
    By Woodworker in forum Access
    Replies: 5
    Last Post: 11-09-2012, 09:23 AM
  3. The simply way to consolidate two bases...
    By jeanpri in forum Import/Export Data
    Replies: 4
    Last Post: 05-14-2012, 01:53 AM
  4. Simply Multiplication on SubForm field using VBA
    By DB2010MN26 in forum Programming
    Replies: 1
    Last Post: 12-10-2011, 01:00 AM
  5. Basic help linking data to a report?
    By SportyDog in forum Reports
    Replies: 3
    Last Post: 11-30-2009, 02:26 AM

Tags for this Thread

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