473,320 Members | 2,071 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,320 software developers and data experts.

Populating a listbox from a formatted array.

1
I am trying to populate a 2 col access listbox with some computed vals. I want to display the vals as Currency in the lstbox. The way I am attempting this is by putting the computed vals into an array. I format the vals as Curr in the array but when I use the lstbox.Additem to add these formnatted vals to the lstbox, the vals are not formatted? Is there any way to have the formatted vals from the array displayed as Curr in the lstbox?

Here is my code:
Expand|Select|Wrap|Line Numbers
  1.  
  2.    Dim sngMulti(10) As Currency
  3.     ' Fill array with values.
  4.         For intJ = 1 To 10
  5.             Calc = FormatCurrency((Calc * txtExponent), 2)
  6.             sngMulti(intJ) = Calc
  7.             Debug.Print FormatCurrency(sngMulti(intJ), 2)
  8.                lstCalc_Results.AddItem intJ & ";" & sngMulti(intJ)
  9.  
  10.         Next intJ
  11.  
  12.  
The Debug.Print shows the formatted vals.
I use the ReDim because eventually the lstbox items will be dynamic.
Oct 18 '11 #1
4 4593
ADezii
8,834 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. Dim sngMulti(10) As Currency
  2.  
  3. ' Fill Array with values.
  4. For intJ = 1 To 10
  5.   Calc = FormatCurrency((Calc * txtExponent), 2)
  6.     sngMulti(intJ) = Calc
  7.       lstCalc_Results.AddItem intJ & ";" & Format$(sngMulti(intJ), "Currency")
  8. Next intJ
Oct 18 '11 #2
NeoPa
32,556 Expert Mod 16PB
PCGuru:
Expand|Select|Wrap|Line Numbers
  1. Dim sngMulti(10) As Currency
That is a practice to be avoided at all costs. It's like indenting code randomly. It gives the impression the code is properly conformant to standards when it's not (which is worse than being obviously non-conformant). If a variable has an "sng" prefix it should only be a variable of type Single.

Currency (as you've used it there) is a type, not a format. You would need to format the value as a string into your ListBox. ADezii has provided an example of how to do that with the Format$() function. The Format() function, without the $, is recommended for general use, although Format$() is still supported for backwards compatibility.
Oct 18 '11 #3
ADezii
8,834 Expert 8TB
@NeoPa:
Not sure that I agree with you on this one.
The Format() function, without the $, is recommended for general use, although Format$() is still supported for backwards compatibility.
If you know that the Return Value of a Function will always be a String, you should never use the Variant Form because of the Explicit Data Conversion required by VBA (Variant ==> String). Case in point:
Expand|Select|Wrap|Line Numbers
  1. Dim lngStart As Long
  2. Dim lngEnd As Long
  3. Dim lngCtr As Long
  4. Dim curRetVal As Currency
  5.  
  6. DoCmd.Hourglass True
  7.  
  8. lngStart = timeGetTime
  9.   For lngCtr = 1 To 20000000
  10.     curRetVal = Format$(lngCtr, "Currency")
  11.   Next
  12. lngEnd = timeGetTime
  13.  
  14. Debug.Print "Time to Execute the String Version of Format$(): " & FormatNumber((lngEnd - lngStart) / 1000, 2) & " seconds"
  15.  
  16.  
  17.  
  18. lngStart = timeGetTime
  19.   For lngCtr = 1 To 20000000
  20.     curRetVal = Format(lngCtr, "Currency")
  21.   Next
  22. lngEnd = timeGetTime
  23.  
  24. Debug.Print "Time to Execute the Variant Version of Format(): " & FormatNumber((lngEnd - lngStart) / 1000, 2) & " seconds"
  25.  
  26. DoCmd.Hourglass False
RESULTS (2 Trial Runs):
Expand|Select|Wrap|Line Numbers
  1. Time to Execute the String Version of Format$(): 56.26 seconds
  2. Time to Execute the Variant Version of Format(): 61.57 seconds
  3.  
  4. Time to Execute the String Version of Format$(): 56.36 seconds
  5. Time to Execute the Variant Version of Format(): 67.48 seconds
Oct 19 '11 #4
NeoPa
32,556 Expert Mod 16PB
ADezii:
Not sure that I agree with you on this one.
I'm sorry my friend. I can't help that. I have no argument with your logic, but it doesn't alter the state that neither of us can control. IE. MS recommend that the $ versions of functions don't get used in new code (I can't give chapter and verse I'm afraid, but I do remember reading it somewhen - probably in my Office VBA course from many years back). The $ versions don't even have Help pages associated any more. I tried to find one when I posted earlier and was unable to even via the See also link.

Does this mean that your logic is flawed? Are MS always (even often) right to lead things in the directions they decide to? I would say no to both frankly. Nevertheless, it's not a tide I've chosen to swim against and, while I respect your decision to, I feel it's only fair to let newcomers understand there is a tide there to be dealt with.

Best regards as always :-)
Oct 19 '11 #5

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

Similar topics

6
by: Deano | last post by:
I needed to have a listbox populated by locations which are stored in tblLocations. However I wanted an "All locations" entry to be at the top of the listbox. This is not in the tblLocations. The...
4
by: Sangeetha | last post by:
I came across this array initialisation which I can't follow ---------------------------------------------- public static string AllOperators { get {
2
by: collie | last post by:
Hi, I have 2 listboxes. The first gets populated from the db as soon as the page loads. The second listbox get populated based on the user's selection from the first listbox. However,...
6
by: P K | last post by:
I have a listbox which I am populating on the client (it contains a list of dates selected from calender). The listbox is a server control. When I get to the server after postback by selecting an...
2
by: Doug Glancy | last post by:
Dim temp(Me.TimeIncrementListBox.Items.Count) As String Dim i As Int32 .... For i = 0 To UBound(temp) - 1 temp(i) = CType(Me.TimeIncrementListBox.Items(i), String) Next...
19
by: hexagram | last post by:
Hi guys good day, can anybody help for my problem The Scenario is A Listbox (ID) - Multiple Select - everytime i choose in the list box the following outbound textbutton and subform will...
15
by: NasirMunir | last post by:
I am trying to populate a listbox from another listbox on a access form. My first listbox has names of tables on a linked odbc databse. I am trying to display the tuples of the table in the second...
1
by: gazsharpe101 | last post by:
Hi, I have a report that is based on a query and I wish to populate a list box on the report with a value from the query based on the values of 2 other query results, i.e. if =5 and = 3 then add to...
11
by: goldstar | last post by:
Hello All, I currently have an order form, when a record is selected and saved this should appear within the listbox below. i could get this to add but at the same time the order that i have...
1
by: jonathanD | last post by:
Excel Gurus, I am suffering with a problem with the following Code Me.ListBox1.List = Range("a1:f10").Cells.SpecialCells_(xlCellTypeVisible).VALUE The listbox will popoulate...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.