473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with Excel RoundDown Function

133 100+
I have spinning cycles on attempting to Round Down within VBA. The code is as follows. I would appreciate any assistance. Line 102 is where it is generating a compiler error of "Method or data member not found."


Expand|Select|Wrap|Line Numbers
  1. Private Sub Create_tbl_SOData_Table()
  2.  
  3.    On Error GoTo Err_Hndlr
  4.  
  5.  
  6. strFirstRec = "Yes"
  7.  
  8. ' **** initialize temporary fields, date is set to dummy initial date, it has no meaning
  9.  
  10. strTempSalesOrderId = 0
  11. strTempSalesOrderNo = " "
  12. strTempPO = " "
  13. strTempPOID = 0
  14. strTempUnitPrice = 0
  15. strTempExtendedPrice = 0
  16. strTempShipmentPrice = " "
  17. strTempAccountCode = " "
  18. strTempInd = "D"
  19. strTempFund = " "
  20. strTempIndexOrg = " "
  21. strTempRule = "J099"
  22. strTempPOLineNo = 0
  23. strTempDate = "03/19/2007"
  24. strTempBuyer = " "
  25. strTempProductDescription = " "
  26. strTempQuantity = 0
  27. strTempSKUCatalog = " "
  28.  
  29. Dim dbs As DAO.Database
  30. Dim rstTemp As DAO.Recordset
  31. Dim strSQL As String
  32.  
  33. Set dbs = CurrentDb()
  34.  
  35. '**** create output table
  36. 'Delete temporary tabl
  37. DoCmd.RunSQL "DROP TABLE tbl_SOData;"
  38.  
  39.  strSQL = "SELECT SO_Data_Formatted.[Sales Order Id], " & _
  40.                 "SO_Data_Formatted.[Sales Order No], " & _
  41.                 "SO_Data_Formatted.[PO #], " & _
  42.                 "SO_Data_Formatted.[PO ID], " & _
  43.                 "SO_Data_Formatted.[Unit Price], " & _
  44.                 "SO_Data_Formatted.[Extended Price], " & _
  45.                 "SO_CFSummary.[Shipment Price], " & _
  46.                 "SO_CFSummary.Account AS [Account Code], 'D ' AS Ind, " & _
  47.                 "SO_CFSummary.Fund, " & _
  48.                 "SO_CFSummary.Index AS IndexOrg, " & _
  49.                 "'J099 ' AS RuleCode, " & _
  50.                 "SO_Data_Formatted.[PO Line #], " & _
  51.                 "SO_Data_Formatted.SODate, " & _
  52.                 "SO_Data_Formatted.[Buyer: First Name], " & _
  53.                 "SO_Data_Formatted.[Product Description], " & _
  54.                 "SO_Data_Formatted.Quantity, " & _
  55.                 "SO_Data_Formatted.[SKU/Catalog #], " & _
  56.                 "SO_Data_Formatted.[Sales Order No] " & _
  57.         "FROM SO_CFSummary " & _
  58.         "INNER JOIN SO_Data_Formatted " & _
  59.         "ON (SO_CFSummary.[Sales Order Line Number] = SO_Data_Formatted.[PO Line #]) " & _
  60.         "AND (SO_CFSummary.[Sales Order Id] = SO_Data_Formatted.[Sales Order Id]) " & _
  61.         "WHERE (((SO_Data_Formatted.[SODate]) Between #" & [Forms]![F_Import_DATA]![txb_date_start] & "# And #" & [Forms]![F_Import_DATA]![txb_date_end] & "#)) " & _
  62.         "ORDER BY SO_Data_Formatted.[Sales Order No];"
  63.  
  64. 'Create temporary table
  65. CurrentDb.Execute ("CREATE TABLE tbl_SOData( [Sales Order Id] Integer, " & _
  66.                                         "[Sales Order No#] Varchar (10), " & _
  67.                                         "[PO #] Varchar(8), " & _
  68.                                         "[PO ID] Integer, " & _
  69.                                         "[Unit Price] money, " & _
  70.                                         "[Extended Price] money, " & _
  71.                                         "[Shipment Price] money, " & _
  72.                                         "AccountCode Varchar(6), " & _
  73.                                         "Ind Varchar(1), " & _
  74.                                         "Fund Varchar(6), " & _
  75.                                         "IndexOrg Varchar(6), " & _
  76.                                         "RuleCode VARCHAR(4),  " & _
  77.                                         "[PO Line No#] Integer, " & _
  78.                                         "S0Date DATETIME, " & _
  79.                                         "Buyer VARCHAR(50), " & _
  80.                                         "[Product Description] VARCHAR(150),  " & _
  81.                                         "Quantity Integer, " & _
  82.                                         "[SKU/Catalog #] VARCHAR(50))")
  83.  
  84. Set rstTemp = dbs.OpenRecordset(strSQL)
  85. Set rstSummary = CurrentDb.OpenRecordset("tbl_SOData")
  86.  
  87. rstTemp.MoveFirst
  88.  
  89. Do While rstTemp.EOF = False
  90.  
  91.     If strFirstRec = "Yes" Then
  92.         strFirstRec = "No"
  93.     End If
  94.  
  95.     strTempSalesOrderId = rstTemp![Sales Order Id]
  96.     strTempSalesOrderNo = rstTemp![Sales Order No]
  97.     strTempPO = rstTemp![PO #]
  98.     strTempPOID = rstTemp![PO ID]
  99.  
  100.     If Not IsNull(rstTemp![Unit Price]) Then
  101.         strTempUnitPrice = rstTemp![Unit Price]
  102.         strTempUnitPrice = Application.WorksheetFunction.RoundDown(strTempUnitPrice, 2)
  103.     Else
  104.          strTempUnitPrice = 0
  105.     End If
  106.     If Not IsNull(rstTemp![Extended Price]) Then
  107.         strTempExtendedPrice = Application.WorksheetFunction.RoundDown((rstTemp![Extended Price]), 2)
  108.     Else
  109.          strTempExtendedPrice = 0
  110.     End If
  111.     If Not IsNull(rstTemp![Shipment Price]) Then
  112.         strTempShipmentPrice = Application.WorksheetFunction.RoundDown((rstTemp![Shipment Price]), 2)
  113.     Else
  114.         strTempShipmentPrice = 0
  115.     End If
  116.     strTempAccountCode = rstTemp![Account Code]
  117.  
  118.      strTempFund = rstTemp!Fund
  119.      strTempIndexOrg = rstTemp!IndexOrg
  120.     strTempPOLineNo = rstTemp![PO Line #]
  121.     strTempDate = rstTemp![SODate]
  122.     strTempBuyer = rstTemp![Buyer: First Name]
  123.     strTempProductDescription = rstTemp![Product Description]
  124.     strTempQuantity = rstTemp![Quantity]
  125.     strTempSKUCatalog = rstTemp![SKU/Catalog #]
  126.  
  127.  
  128.         rstSummary.AddNew
  129.  
  130.             rstSummary![Sales Order Id] = strTempSalesOrderId
  131.             rstSummary![Sales Order No#] = strTempSalesOrderNo
  132.             rstSummary![PO #] = strTempPO
  133.             rstSummary![PO ID] = strTempPOID
  134.             rstSummary![Unit Price] = strTempUnitPrice
  135.             rstSummary![Extended Price] = strTempExtendedPrice
  136.             rstSummary![Shipment Price] = strTempShipmentPrice
  137.             rstSummary!AccountCode = strTempAccountCode
  138.             rstSummary!Ind = strTempInd
  139.             rstSummary!Fund = strTempFund
  140.             rstSummary!IndexOrg = strTempIndexOrg
  141.             rstSummary!RuleCode = strTempRule
  142.             rstSummary![PO Line No#] = strTempPOLineNo
  143.             rstSummary!S0Date = strTempDate
  144.             rstSummary!Buyer = strTempBuyer
  145.             rstSummary![Product Description] = strTempProductDescription
  146.             rstSummary!Quantity = strTempQuantity
  147.             rstSummary![SKU/Catalog #] = strTempSKUCatalog
  148.  
  149.  
  150.         rstSummary.Update
  151.  
  152.  
  153.         strTempSalesOrderId = 0
  154.         strTempSalesOrderNo = " "
  155.         strTempPO = " "
  156.         strTempPOID = 0
  157.         strTempUnitPrice = 0
  158.         strTempExtendedPrice = 0
  159.         strTempShipmentPrice = " "
  160.         strTempAccountCode = " "
  161.  
  162.         strTempFund = " "
  163.         strTempIndexOrg = " "
  164.  
  165.         strTempPOLineNo = 0
  166.  
  167.         strTempBuyer = " "
  168.         strTempProductDescription = " "
  169.         strTempQuantity = 0
  170.         strTempSKUCatalog = " "
  171.  
  172.     rstTemp.MoveNext
  173. Loop
  174.  
  175. rstTemp.Close
  176. rstSummary.Close
  177.  
  178.  
  179. Create_tbl_SOData_Exit:
  180.   Exit Sub
  181.  
May 16 '11 #1

✓ answered by NeoPa

A VBA specific approach might be to use the Fix() function. This returns a rounded down integral value, but would need to be used with multiplying and dividing to handle numbers of decimal places :
Expand|Select|Wrap|Line Numbers
  1. strTempUnitPrice = CDbl(Fix(Val(strTempUnitPrice) * 100)) / 100
I assume you know the limitations of storing your numeric values in string variables.

8 3390
ADezii
8,834 Expert 8TB
I'm a little confused, dowlingm815. Are you attempting to utilize Excel's RoundDown() Worksheet Function from within Access in Line #102? If so, that approach will never work.
May 16 '11 #2
dowlingm815
133 100+
Hey Adezii,

Nice to hear from you. Yes, I need to round down, if the price is 2.667, it needs to drop the values pass the second digit, 2.66. I've tired cast, decimal, I'm running out of ideals.

Mary
May 17 '11 #3
NeoPa
32,556 Expert Mod 16PB
My best guess is that the OP is trying to do just that ADezii. It's hard to be sure because there is so little information in the question other than hundreds of lines of code. I can't say I'm overly amused.

I should make clear that the WorksheetFunction class is a member of the Excel Application object only. This is not general VBA, and needs to be made available to an Access Project (Tools / References in the VBA IDE) before use. It would then be referenced as :
Expand|Select|Wrap|Line Numbers
  1. Excel.WorksheetFunction.RoundDown()
F2 from the VBA IDE provides a way (The Object Browser) to negotiate the class structures associated with each application or library. This can be extremely helpful for a developer.
May 17 '11 #4
NeoPa
32,556 Expert Mod 16PB
A VBA specific approach might be to use the Fix() function. This returns a rounded down integral value, but would need to be used with multiplying and dividing to handle numbers of decimal places :
Expand|Select|Wrap|Line Numbers
  1. strTempUnitPrice = CDbl(Fix(Val(strTempUnitPrice) * 100)) / 100
I assume you know the limitations of storing your numeric values in string variables.
May 17 '11 #5
ADezii
8,834 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. 'NOTE: You must 1st set a Reference to the Microsoft Excel XX.X Object Library
  2. Dim appExcel As Excel.Application      'Declare in a Broader Scope
  3.  
  4. Set appExcel = New Excel.Application
  5.  
  6. Debug.Print appExcel.WorksheetFunction.RoundDown("32.519123", 2)
  7.  
  8. 'Execute after processing Recordset
  9. appExcel.Quit
  10. Set appExcel = Nothing
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. 32.51
May 17 '11 #6
NeoPa
32,556 Expert Mod 16PB
Application Automation (as you have illustrated) is actually not necessary ADezii. As long as the Microsoft Excel XX.XX Object Library is referenced, the method can be called (See in post #4 how it is referenced in code).
May 17 '11 #7
ADezii
8,834 Expert 8TB
Good point, NeoPa, and an oversight on my part.
May 17 '11 #8
dowlingm815
133 100+
Gentlmen,

Thank you for your assistance. I opted for using for using CDbl(Fix(Val(field))) approach to be consistent with the code. I apppreciate all of your input.

Mary
May 18 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Aaron | last post by:
I am trying to write a Python script file which analyses data in an iterative manner. During each iteration, I wish to utilise the Solver function in Excel to perform some constrained, non-linear...
5
by: Jim | last post by:
Need help with aggregate function...for each unique product, I need the provider with the cheapest cost factor Here't the table (Table1) ID product provider factor 1 123456 abc...
3
by: dibblm | last post by:
Below is current code used. I can only list one directory then move to next. I want to search one more directory further and can't seem to find how to get one deeper. What I want to accomplish is...
4
by: Vasilis X | last post by:
Hello. I want to use the excel function Year( ) (or Day, Month, Minute etc...) in a Visual Basic .net application. How can i do this? I tried Excel.WorksheetFunction but this class doesn't...
1
by: doug9149 | last post by:
Hello all, I've got some code I wrote using VBA for Excel that I'm trying to recreate using C#.NET. The code autofilters 1 entire column from 1 Worksheet from a Workbook and then appends this...
1
by: Carlos | last post by:
I'm getting RPC error when I try to register analys32.xll in the VB program. ErrorCode=-2147417851 Message="El servidor lanzó una excepción. (Exception from HRESULT: 0x80010105...
7
by: steveyjg | last post by:
This is my first post so I'm not sure if this should be in the .NET forum or the VB forum. Anyway I'm using VB .NET and trying to call the Percentile function. The function needs an Object as a...
5
by: lucky33 | last post by:
I am using the AVG function in a report to show the average number of cases picked per line item over a given period of time. For instance the user might want the info from 07/16/07 to 07/20/07. ...
3
by: keirnus | last post by:
Hello once again... I made a function in Excel. The function does some error checking within the Excel file. To be easy for me, I want my code in MS Access to simply call the function in Excel....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.