Results 1 to 5 of 5
  1. #1
    jet0l19 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    2

    #Error on form. Type conversion? Please help!

    Hi there.



    I've been working in a form all morning trying to do a bit of manipulation. This is the general idea of what I'm trying to do:

    I have one form where there is an input that you type in specifying what field you want to be used for "Amount." You can type anything in this box for example Field1, Field2, Field3, etc.

    I have another form that calculates average and I want it to calculate the average of the field name that is typed in the initial box. If I type Field1 initially, I want it to average all of Field1. If I typed Field2, I'd want it to average Field2 in the same box.

    Now I've gotten to the point where I can make a text box appear that says the field that I typed in by linking it to the first form in the Control Source. Now I just need to be able to change the expression to take the average of that actual field instead of just displaying the field name. I tried several things as the Control Source.

    I'm basically trying to make the Control Source see Avg([Field1]), which does calculate if I type it in manually.

    When I have in the Control Source

    "[" & CStr([Forms]![Form Select Amount]![Amount]) & "]"

    I see [Field1]

    But when I try something like

    Avg("[" & CStr([Forms]![Form Select Amount]![Amount]) & "]")

    or

    Avg("[" & CVar([Forms]![Form Select Amount]![Amount]) & "]")


    I just get an #error message.


    Any help would be greatly appreciated. I'm new to access and the forum so I apologize if I broke any forum etiquette.

    Thank you!

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,915
    I think aggregate functions must use an actual field name that is in the RecordSource, not a variable. Reference to the control is a variable.

    Also, aggregate functions should be used in header/footer sections, not Detail.

    Why do you need to have user select a field? Why not just calculate all of them?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    jet0l19 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    2
    Quote Originally Posted by June7 View Post
    I think aggregate functions must use an actual field name that is in the RecordSource, not a variable. Reference to the control is a variable.

    Also, aggregate functions should be used in header/footer sections, not Detail.

    Why do you need to have user select a field? Why not just calculate all of them?
    It is a pretty complex database. The user needs to select a field because the information that is important is stored in different fields for different clients. However, this number needs to be displayed on the same report for standardization purposes.

    It just seems strange to me I can't just somehow convert the output which does display Field1 for me to be interpreted in a way that can be used in an expression referencing the actual Field1.

  4. #4
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Also, with the line


    Avg("[" & CStr([Forms]![Form Select Amount]![Amount]) & "]")

    you're converting Amount to a String and then you try to average it! I suspect that you can only get the average of a group of Numeric Fields, not a group of Text Fields.

    The statement

    Quote Originally Posted by jet0l19 View Post

    ...information that is important is stored in different fields for different clients...
    makes me think that you may have some design flaws in your data setup.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,915
    I agree with Linq. Not a normalized database. That's the crux of the issue and will continue to cause issues.

    Good catch on the CStr, I didn't even see it. However, removing it won't fix the problem. I tested and my remarks still apply. Using an expression in textbox ControlSource to construct the field name with a concatenated variable won't translate to the field but it can be done in VBA. Code can build the field name and set the textbox ControlSource property. I tested and it works. Example:
    Private Sub Combo101_AfterUpdate()
    Me.Text48.ControlSource = "=Avg([" & Me.Combo101 & "])"
    End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Type Conversion Failure
    By jessgold in forum Access
    Replies: 5
    Last Post: 11-12-2012, 10:39 PM
  2. Replies: 8
    Last Post: 08-09-2012, 07:04 PM
  3. Type conversion error
    By corymw in forum Access
    Replies: 1
    Last Post: 07-25-2012, 11:55 AM
  4. Data Type Conversion Error
    By graviz in forum Forms
    Replies: 7
    Last Post: 06-04-2012, 11:34 AM
  5. Type Conversion Failure
    By fpmsi in forum Access
    Replies: 7
    Last Post: 09-22-2011, 11:25 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