Solved

Format Currency in List Box using RowSource

Posted on 2008-06-10
36
974 Views
Last Modified: 2008-06-11
Need to pass a SQL query into a list box when a record is selected from an adjacent list box.  On the trigger even, the below SQL query resuls are sent to a list box with the RowSource command.  I can't get the Sum figure outputed as Order Total to be formatted into Currency.
strSQL_OrderHistory = "SELECT dbo.tblorders.idorder, dbo.tblorders.chrponumber, dbo.tblorders.dtordered, SUM(dbo.qry_customer_followup_orderhistory.[Line Total]) AS [Order Total], " & _

                     "dbo.tblorders.dtrequestship , dbo.tblorders.idcustomer, dbo.tblorders.intcancelled, dbo.tblorders.inttranstype " & _

"FROM dbo.tblorders LEFT OUTER JOIN " & _

                      "dbo.qry_customer_followup_orderhistory ON dbo.tblorders.idorder = dbo.qry_customer_followup_orderhistory.idorder " & _

"GROUP BY dbo.tblorders.idorder, dbo.tblorders.idcustomer, dbo.tblorders.chrponumber, dbo.tblorders.dtordered, dbo.tblorders.dtrequestship, " & _

                      "dbo.tblorders.intcancelled , dbo.tblorders.inttranstype " & _

"HAVING (dbo.tblorders.idcustomer = '" & Me.lstcustomers & "') And (dbo.tblorders.intcancelled = 0) And (dbo.tblorders.inttranstype = 1)"
 

Me.lsthistory.RowSource = strSQL_OrderHistory

Open in new window

0
Comment
Question by:opus111
  • 16
  • 11
  • 3
  • +3
36 Comments
 
LVL 58

Expert Comment

by:harfang
ID: 21756389
A listbox understands only text. You need to format your field in your query, e.g.

Format(SUM(dbo.qry_customer_followup_orderhistory.[Line Total]), 'Currency') AS [Order Total]

(°v°)
0
 

Author Comment

by:opus111
ID: 21756408
I tried that, as soon as I put that in and run the form, the list box stays blank when I run the query.  The form is in MS Access, Using Visual Basic to call this event on the "on click" of a different list box.  Basically, click on the customer to the left, and the customers order history comes up on the list box on the right.  To be safe, I copied your suggestion into it and still didn't work.
0
 
LVL 75
ID: 21756462
You can avoid this hassle by using a Continuous subform ...

mx
0
 

Author Comment

by:opus111
ID: 21756475
I know, I've got several different list boxes on several different tabs that get updated when a customer is selected.  It seems to be much easier to deal with the list boxes than multiple continuous forms.  I was just hoping there would be an easy fix.  Thanks for any advise you can give....
0
 
LVL 75
ID: 21756485
Well, what Marcus posted should work ... you have to do it in a query.  Then after you get it to display Currency, you get to figure out how to make it Right justified :-)

mx
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 21756504
This should be in a way identical to the solution from Great (°v°):
Format(SUM(dbo.qry_customer_followup_orderhistory.[Line Total]), '#,#.00') AS [Order Total]
0
 

Author Comment

by:opus111
ID: 21756505
Here's the code I'm using.  It works, then I add the Format....'currency' and it doesn't work.  I'm confused.
Private Sub lstcustomers_Click()
 

Dim strSQL_OrderHistory As String, strSQL_ProductHistory As String, strSQL_FollowupHistory As String, strSQL_Locations As String
 

'Pass Thru Order History
 

strSQL_OrderHistory = "SELECT dbo.tblorders.idorder, dbo.tblorders.chrponumber, dbo.tblorders.dtordered, Format(SUM(dbo.qry_customer_followup_orderhistory.[Line Total]),'Currency') AS [Order Total], " & _

                     "dbo.tblorders.dtrequestship , dbo.tblorders.idcustomer, dbo.tblorders.intcancelled, dbo.tblorders.inttranstype " & _

                        "FROM dbo.tblorders LEFT OUTER JOIN " & _

                      "dbo.qry_customer_followup_orderhistory ON dbo.tblorders.idorder = dbo.qry_customer_followup_orderhistory.idorder " & _

                        "GROUP BY dbo.tblorders.idorder, dbo.tblorders.idcustomer, dbo.tblorders.chrponumber, dbo.tblorders.dtordered, dbo.tblorders.dtrequestship, " & _

                      "dbo.tblorders.intcancelled , dbo.tblorders.inttranstype " & _

                    "HAVING (dbo.tblorders.idcustomer = '" & Me.lstcustomers & "') And (dbo.tblorders.intcancelled = 0) And (dbo.tblorders.inttranstype = 1)"
 

Me.lsthistory.RowSource = strSQL_OrderHistory
 

End Sub

Open in new window

0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 21756507
w/ $

Format(SUM(dbo.qry_customer_followup_orderhistory.[Line Total]), '$#,#.00') AS [Order Total]
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 21756515
hoe about:

Format(SUM(Nz(dbo.qry_customer_followup_orderhistory.[Line Total],0)), '$#,0.00') AS [Order Total]
0
 

Author Comment

by:opus111
ID: 21756518
Last two still didn't work.  I remove the changes, and it works.
Private Sub lstcustomers_Click()
 

Dim strSQL_OrderHistory As String, strSQL_ProductHistory As String, strSQL_FollowupHistory As String, strSQL_Locations As String
 

'Pass Thru Order History
 

strSQL_OrderHistory = "SELECT dbo.tblorders.idorder, dbo.tblorders.chrponumber, dbo.tblorders.dtordered, Format(SUM(dbo.qry_customer_followup_orderhistory.[Line Total]), '$#,#.00') AS [Order Total], " & _

                     "dbo.tblorders.dtrequestship , dbo.tblorders.idcustomer, dbo.tblorders.intcancelled, dbo.tblorders.inttranstype " & _

                        "FROM dbo.tblorders LEFT OUTER JOIN " & _

                      "dbo.qry_customer_followup_orderhistory ON dbo.tblorders.idorder = dbo.qry_customer_followup_orderhistory.idorder " & _

                        "GROUP BY dbo.tblorders.idorder, dbo.tblorders.idcustomer, dbo.tblorders.chrponumber, dbo.tblorders.dtordered, dbo.tblorders.dtrequestship, " & _

                      "dbo.tblorders.intcancelled , dbo.tblorders.inttranstype " & _

                    "HAVING (dbo.tblorders.idcustomer = '" & Me.lstcustomers & "') And (dbo.tblorders.intcancelled = 0) And (dbo.tblorders.inttranstype = 1)"
 

Me.lsthistory.RowSource = strSQL_OrderHistory
 

End Sub

Open in new window

0
 
LVL 75
ID: 21756524
Try creating a saved query with that SQL , then set the Row Source to that query - instead of doing the SQL in code.

mx
0
 
LVL 8

Expert Comment

by:Emil_Gray
ID: 21756534
Use a DLOOKUP to format the result. As I have elsewhere noted Access is quite annoying.
0
 

Author Comment

by:opus111
ID: 21756537
I tried the Sum(Nz... it didn't work.  I believe the code would work if I set the Row Source.  However, I don't know how to feed the dynamic customer ID into the saved qry.  That's why I resorted to using an event trigger in Visual basic.
0
 

Author Comment

by:opus111
ID: 21756542
As for the DLookup, in my limited knowledge, I can only do a DLookup based on a saved qry, and I don't know how to pass the variable into the saved qry.  Is this now extending outside the scope of my original question?
0
 
LVL 75
ID: 21756553
How are you seeing DLookup playing into this mix ?
0
 

Author Comment

by:opus111
ID: 21756564
Can you do a Dlookup that references a save qry and pass a variable into it? (a variable into the saved queary?  I haven't been able to do that?  
0
 
LVL 75
ID: 21756574
Where is the 'variable' coming from?  A form?

Ok ... I see

 Me.lstcustomers

So ... that part of the WHERE clause would be Forms.YourFormName.lstCustomers

mx
0
 

Author Comment

by:opus111
ID: 21756601
Thanks, I'll mess around with a DLookup and get back to you.  Thanks!
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 75
ID: 21756612
DLookup ???  What for?  Use a saved query and reference the Form as I showed.  DLookup will be a performance hit.

mx
0
 

Author Comment

by:opus111
ID: 21756622
Oh I see...sorry.  I'll try it.
0
 

Author Comment

by:opus111
ID: 21756663
It won't let me reference a form for the criteria in a saved query....gives me an error.  HOWEVER -- I'm making this in Access with their stored procedures.  Am I supposed to create a stored query outside of Access using only SQL?  This is new to me...sorry.
0
 
LVL 75
ID: 21756692
" Am I supposed to create a stored query outside of Access using only SQL? "
No. Inside of Access, which doesn't have Stored Procedures, instead 'saved' queries.

Show me the SQL for the saved query.

mx
0
 

Author Comment

by:opus111
ID: 21756736
I'm sorry, I don't think I'm working in the right place: I put the reference to the form into the SQL and I get The column prefix Forms.frmcustomer_followup does not match with a table name or alias name used in the query."

I'm building these in the design view on access.



SELECT     dbo.tblorders.idorder, dbo.tblorders.dtordered, SUM(dbo.qry_customer_followup_orderhistory.[Line Total]) AS [Order Total], 

                      dbo.tblorders.dtrequestship, dbo.tblorders.idcustomer, dbo.tblorders.intcancelled, dbo.tblorders.inttranstype

FROM         dbo.tblorders LEFT OUTER JOIN

                      dbo.qry_customer_followup_orderhistory ON dbo.tblorders.idorder = dbo.qry_customer_followup_orderhistory.idorder

GROUP BY dbo.tblorders.idorder, dbo.tblorders.idcustomer, dbo.tblorders.chrponumber, dbo.tblorders.dtordered, dbo.tblorders.dtrequestship, 

                      dbo.tblorders.intcancelled, dbo.tblorders.inttranstype

HAVING      (dbo.tblorders.idcustomer = Forms.frmcustomer_followup.lstCustomers) AND (dbo.tblorders.intcancelled = 0) AND (dbo.tblorders.inttranstype = 1)

Open in new window

0
 
LVL 75
ID: 21756756
"The column prefix Forms.frmcustomer_followup does not match with a table name or alias name used in the query.""

I've never seen that error message.  Are you in the Access query designer ?

mx
0
 

Author Comment

by:opus111
ID: 21756762
yes.
0
 
LVL 75
ID: 21756770
Well, sorry. I don't work with SQL .. so, maybe  it's different. The SQL 'looks' ok ... that form reference and all.

Have you actually saved this query - so that you see it listed in the database window - in the queries tab?

mx
0
 

Author Comment

by:opus111
ID: 21756786
All the queries are saved on the SQL sever under the "Views" tab.  I'm so sorry for the trouble.  I've always wanted to be able to use the same save query, but pass a variable into it, but every time I've built one and referenced a form field for it's search criteria it barks at me.  Never been able to pull it off.
0
 
LVL 75
ID: 21756823
"All the queries are saved on the SQL sever under the "Views" tab."

OK ... well, that's something completely different.  Not likely that would work.  Not sure why the formatting Marchus (Harfang) showed you doesn't work either. Weird.

Marcus ????
0
 

Author Comment

by:opus111
ID: 21756842
I too, obviously am at a loss.  it doesn't make sense.  I sure appreciate your efforts. I've spent hours researching and have found the same solutions from other threads that you guys have offered me....but they don't work.  Somethings getting lost between assigning the SQL statement to a Rowsource on a list box via a "on click" event.
0
 
LVL 8

Expert Comment

by:Emil_Gray
ID: 21756914
It's too late for me to go into it but a DLOOKUP allows you to have a totally separate source for the data you want to obtain.

Tomorrow I'll check and see if you have found your answer.

After all, whatever works for you is all that matters.

I'm a little curious what answer works. This is a bit interesting.

Emil
0
 

Author Comment

by:opus111
ID: 21756926
Thanks...I'll look more into the DLookup option as well.
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 300 total points
ID: 21757645
> I can't get the Sum figure outputed as Order Total to be formatted into Currency.

This works fine here:

SELECT
  Format(Sum(tblTest.Amount),'currency') AS Total,
  tblTest.CustomerId
FROM
  tblTest
GROUP BY
  tblTest.CustomerId;

so Format is not the issue. However, your dbo.prefix of the table names indicate a pass-through query; thus you must use the syntax of SQL Server, like:

SELECT
  CAST(Sum(dbo.tblTest.Amount) AS money) AS Total,
  dbo.tblTest.CustomerId
FROM
  dbo.tblTest
GROUP BY
  dbo.tblTest.CustomerId;

/gustav
0
 

Author Comment

by:opus111
ID: 21757668
You are awesome!!! Thank you!!!

0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 21757699
You are welcome!

/gustav
0
 
LVL 8

Expert Comment

by:Emil_Gray
ID: 21757770
Great. I'm glad you got your answer.
0
 
LVL 75
ID: 21761361
Thanks Gustav.

mx
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now