Results 1 to 5 of 5
  1. #1
    ke4peo is offline Novice
    Windows 10 Office 365
    Join Date
    Jun 2022
    Posts
    6

    Trying to get percentage via VBA but won't show in field

    So, I'm trying to calculate a Transfer % for call statistics.

    Related Table Fields:
    totalcalls (Number, Long Integer) - Total number of calls
    totalxfers (Number, Long Integer) - Total number of transfers
    xferpercent (Number, Double) - Transfer % (Format is set to Percentage and fixed to 1 decimal point)

    There are also the same fields on the form.



    Related VBA Code:
    Me.totalcalls.Value = DCount("Type", "CallData Query", "[Date]= #" & entrydate & "#")
    Me.totalxfers.Value = DCount("Type", "CallData Query", "[Transfer]= 'Y' AND [Date]= #" & entrydate & "#")
    Me.xferpercent.Value = totalxfers / livecalls

    When I do that and click the 'Calculate Stats' button, I get values in [totalcalls] and [totalxfers] but in [xferpercent] I get nothing. Blank. And no errors.

    However, if I unbind the [totalxfers] text box and set it's ControlSource to =[totalxfers]/[livecalls] then VOILA! I get a percentage value in the box! Of course, it doesn't save a value to the record in the DailyStats table, which is what I need it to do.

    Any suggestions on why Me.xferpercent.Value isn't working, but setting it as ControlSource is? Am I missing something stupidly simple?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    What is livecalls? It is field that totalcalls is bound to? If you save totalcalls and totalxfers, there is no need to save xferpercent.

    What event is code in?
    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
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by ke4peo View Post
    Related VBA Code:
    Code:
    Me.totalcalls.Value = DCount("Type", "CallData Query", "[Date]= #" & entrydate & "#")
       Me.totalxfers.Value = DCount("Type", "CallData Query", "[Transfer]= 'Y' AND [Date]= #" & entrydate & "#")
       Me.xferpercent.Value = totalxfers / livecalls
    1. Date is a reserved word. You don't want to give tables, fields, form objects, etc the same name as a reserved word. Here is a list of reserved words for reference.
    2. In your last line what is livecalls ?
    3. These textboxes are bound? If so you don't want to set Me.textbox_name.Value in code, just use the control source property
    4. You do or do not intend to save this value to a dailystats table? Generally speaking a calculated value should not be saved in a table and always be calculated at run time
    5. Is there a specific reason you want to do this in vba? It seems to me that all you've shown here can be done without vba and within the countrol source properties of your textboxes.

  4. #4
    ke4peo is offline Novice
    Windows 10 Office 365
    Join Date
    Jun 2022
    Posts
    6
    TestDB.zipOk, I might need to clarify a bit here so I created dummy data in the database and going to attach it. Hopefully it helps.

    Tables:
    CallData - This is where all the call data is stored.
    DailyStats - This is where I want to store the daily stats (Total Calls, Total Transfers, Transfer %, etc....)

    Queries:
    CallData Query - Simple query, pulls all records from CallData table
    DailyStats Query - Simple query, pulls all records from DailyStats table

    Forms:
    Daily Call Stats - This is the form I'm having the trouble on.

    There is VBA code linked to the On_Click event of the "Calculate New Stats" button. This code is supposed to prompt for the date to process and then set the value of each field based on the code for that field. In TestDB:

    Transfer % is set by VBA code: Me.xferpercent.Value = totalxfers / livecalls

    Text21 is set by it's ControlSource: = [totalxfers] / [livecalls]

  5. #5
    ke4peo is offline Novice
    Windows 10 Office 365
    Join Date
    Jun 2022
    Posts
    6
    Figured it out!

    Instead of specifying the fields to divide by, I just used the same DCount's that count the total transfers and live calls instead. So basically:

    Me.xferpercent.Value = DCount("Type", "CallData Query", "[Transfer]= 'Y' AND [Date]= #" & entrydate & "#") / DCount("Type", "CallData Query", "[Live]= 'Y' AND [Date]= #" & entrydate & "#")

    That got it populating the proper %. (And yes, now that I got that solved I'm going through and renaming any "Date" and "Time" field names and then I'll sift through all the code to fix the references)

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

Similar Threads

  1. Crosstab Query to show percentage of row total
    By keith2511 in forum Queries
    Replies: 3
    Last Post: 03-20-2019, 12:14 PM
  2. Have a query show the percentage of an amount
    By TrulyVisceral in forum Queries
    Replies: 4
    Last Post: 05-17-2018, 05:52 PM
  3. Cannot change value of a percentage field
    By kdbailey in forum Access
    Replies: 1
    Last Post: 06-19-2014, 02:32 PM
  4. Replies: 1
    Last Post: 01-11-2013, 06:01 PM
  5. Replies: 10
    Last Post: 10-19-2012, 05:10 AM

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