VB and Excel.

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2006
Posts: 11
Reputation: vishalsomani is an unknown quantity at this point 
Solved Threads: 0
vishalsomani vishalsomani is offline Offline
Newbie Poster

VB and Excel.

 
0
  #1
Feb 16th, 2006
Hello,
I have attached a database which will be in use for the question i am about to ask.

The question is ... Write queries to retrieve the following sets of information.
a) An annual report showing the number of orders and the value of the orders placed by each supplier, as a bar chart.
b) A list of orders exceeding a value specified by the user.

How do i start and what code shud i put in ? Please if anyone can help.
If you can create a VB form and send me that on vishalsomani@yahoo.com , it will be much appreciable.
Thanks.

So far whatever I have done, I have attached it but it doesn't seem to work.
Attached Files
File Type: zip Ok1.zip (41.5 KB, 39 views)
File Type: zip frmSupplier.zip (5.8 KB, 32 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: VB and Excel.

 
0
  #2
Feb 17th, 2006
vishalsomani,

Since this is an assignment I will not give you a full answer to your question, that is against the rules of this forum. However, I will give you the SQL queries to get the data you want since you could easily get those through Access anyway.

Here goes:

For the first problem of yearly sales per supplier you can use the following query to get your data:
SELECT Orders.SupplierID,
Suppliers.CompanyName,
Sum([Quantity]*[UnitPrice]) AS TotalAmt,
Count(Orders.OrderID) AS CountOfOrderID
FROM Orders LEFT JOIN Suppliers ON Orders.SupplierID = Suppliers.SupplierID
WHERE Year([OrderDate])=2003
GROUP BY Orders.SupplierID, Suppliers.CompanyName;

What you need to do here is to replace the year, i.e. 2003, with the year you are interested in when you build the query in your code, something like:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim SQLString As String
  2.  
  3. SQLString = "SELECT Orders.SupplierID, Suppliers.CompanyName, Sum([Quantity]*[UnitPrice]) AS TotalAmt, Count(Orders.OrderID) AS CountOfOrderID "
  4. SQLString = SQLString & "FROM Orders LEFT JOIN Suppliers ON Orders.SupplierID = Suppliers.SupplierID "
  5. SQLString = SQLString & "WHERE Year([OrderDate])= " & Inputbox("Please enter the year for the annual report:")
  6. SQLString = SQLString & " GROUP BY Orders.SupplierID, Suppliers.CompanyName"

For the second problem, of getting orders above a certain amount you should use the following query:
SELECT Orders.OrderID,
Orders.Product,
Orders.UnitPrice,
Orders.Quantity,
[Quantity]*[UnitPrice] AS TotalAmt,
Orders.OrderDate,
Orders.Received,
Orders.SupplierID,
Suppliers.CompanyName
FROM Orders LEFT JOIN Suppliers ON Orders.SupplierID = Suppliers.SupplierID
WHERE [Quantity]*[UnitPrice] >= 1000;

Here the trick is to get the total amount above which the users are interested you can use the same technique as above and then the code would look something like this:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim SQLString As String
  2.  
  3. SQLString = "SELECT Orders.OrderID, Orders.Product, Orders.UnitPrice, Orders.Quantity, [Quantity]*[UnitPrice] AS TotalAmt, Orders.OrderDate, Orders.Received, Orders.SupplierID, Suppliers.CompanyName "
  4. SQLString = SQLString & "FROM Orders LEFT JOIN Suppliers ON Orders.SupplierID = Suppliers.SupplierID "
  5. SQLString = SQLString & "WHERE [Quantity]*[UnitPrice] >= " & InputBox("Please enter the order total to report on:")

If you want to use other ways of getting the variable input into your queries just replace the InputBox() statements with your variables and everything should work OK.

Hope this helps

Happy coding

Yomet
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 11
Reputation: vishalsomani is an unknown quantity at this point 
Solved Threads: 0
vishalsomani vishalsomani is offline Offline
Newbie Poster

Re: VB and Excel.

 
0
  #3
Feb 18th, 2006
I thank you very much for your reply but this question is of Excel.. and i dont know if i have to make an excel chart in Excel first or just make it. However the queries which you have given me, i dont really understand where to put them in code because we have to show this as a bar chart and not about database if you understand me.
I have attached the Excel form i have tried, but it doesn't work. Please help. Thanks. Can you explain me my mistakes i have done in the excel form and help me correct them and explain me where this queries have to come please. Thanks once again. Vishal.



Originally Posted by vishalsomani
Hello,
I have attached a database which will be in use for the question i am about to ask.

The question is ... Write queries to retrieve the following sets of information.
a) An annual report showing the number of orders and the value of the orders placed by each supplier, as a bar chart.
b) A list of orders exceeding a value specified by the user.

How do i start and what code shud i put in ? Please if anyone can help.
If you can create a VB form and send me that on vishalsomani@yahoo.com , it will be much appreciable.
Thanks.

So far whatever I have done, I have attached it but it doesn't seem to work.
Attached Files
File Type: zip Ok.zip (47.3 KB, 17 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 11
Reputation: vishalsomani is an unknown quantity at this point 
Solved Threads: 0
vishalsomani vishalsomani is offline Offline
Newbie Poster

Re: VB and Excel.

 
0
  #4
Feb 18th, 2006
Then there is this other question.
1Write queries to retrieve the following sets of information.
a) A monthly order report for a specific supplier.

2) Embed each of the above queries in procedural code attached to a form. The queries are to be executed in response to events occuring to one or more controls within the form. An excel chart is to be used for query 1b) which is An annual report showing the number of orders and the value of the orders placed by each supplier, as a bar chart.

Hope you understand this.
Cheers, Vishal.

Originally Posted by vishalsomani
I thank you very much for your reply but this question is of Excel.. and i dont know if i have to make an excel chart in Excel first or just make it. However the queries which you have given me, i dont really understand where to put them in code because we have to show this as a bar chart and not about database if you understand me.
I have attached the Excel form i have tried, but it doesn't work. Please help. Thanks. Can you explain me my mistakes i have done in the excel form and help me correct them and explain me where this queries have to come please. Thanks once again. Vishal.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: VB and Excel.

 
0
  #5
Feb 24th, 2006
vishalsomani,

First of all I can tell you that are on the right track.

Now, the queries I gave you will (should) be used to populate the data area for your Chart. So what you do is simply open a connection to the database and set the recordset to be the query I gave you, i.e. for the Annual Report chart we would have something like:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command1_Click()
  2. Dim Prod As Connection
  3. Dim rs As Recordset
  4. Dim i As Integer
  5.  
  6. Set Prod = New ADODB.Connection
  7. Prod.Provider = "Microsoft.Jet.OLEDB.4.0;"
  8. 'NEVER Hard-Code a path unless you have a VERY good reason for doing this.
  9. 'Rather use the app.path to see where the application currently is
  10. ' Prod.ConnectionString = "C:\VB Assignment Uni\Assignment\OK.mdb"
  11. Prod.ConnectionString = App.Path & "\OK.mdb"
  12. Prod.Open
  13. Set rs = New ADODB.Recordset
  14.  
  15. rs.Open "SELECT Orders.SupplierID,Suppliers.CompanyName,Sum([Quantity]*[UnitPrice]) AS TotalAmt,Count(Orders.OrderID) As CountOfOrderID " & _
  16. "FROM Orders LEFT JOIN Suppliers ON Orders.SupplierID = Suppliers.SupplierID " & _
  17. "Where Year([OrderDate]) = 2003 " & _
  18. "GROUP BY Orders.SupplierID, Suppliers.CompanyName", Prod, adOpenDynamic, adLockOptimistic
  19.  
  20. Set xlApp = New Excel.Application
  21. 'Remove the following line to hide Excel, I want to see it for debugging
  22. xlApp.Visible = True
  23. Set xlWkb = xlApp.Workbooks.Add
  24. Set xlsht = xlWkb.Worksheets(1)

From this point you scroll through the recordset populating the A, B and C columns of your spreadsheet with the data returned by the query. This is now the data range for your chart (so make sure you keep track of the range you are populating, i.e. rows and columns).

The next code I am giving you is straight from the best way of learning how to code a new MS application - the macro recorder. Try this next time, and I really want you to try it

What I did was to:
- Open a new Excel spreadsheet
- Paste the results of my first query into cell A1
- Delete the header line
- Start recording the macro while I added the new bar chart as per the specs.

This is the code that Excel gave me
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Sub Macro1()
  2. '
  3. ' Macro1 Macro
  4. ' Macro recorded 02 24 2006 by Yomet
  5. '
  6.  
  7. '
  8. Charts.Add
  9. ActiveChart.ChartType = xlBarClustered
  10. ActiveChart.SeriesCollection(1).Name = "=""Sales (* 1000$)"""
  11. ActiveChart.SeriesCollection(2).Name = "=""Orders"""
  12. ActiveChart.Location Where:=xlLocationAsNewSheet
  13. With ActiveChart
  14. .HasTitle = True
  15. .ChartTitle.Characters.Text = "Annual Report"
  16. .Axes(xlCategory, xlPrimary).HasTitle = True
  17. .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Company"
  18. .Axes(xlValue, xlPrimary).HasTitle = True
  19. .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Sales / Orders"
  20. End With
  21. End Sub
From this I learn exactly how to use a lot of the functionality of Excel charts.
Now I realized that I could not see the number of orders in the chart, that was my own fault, the sales are in thousands of dollars (Euros, pounds, yen - whatever) while the number of sales are in the singles, so I divided the sales by 1000 and "Presto!" I now have orders and sales on the same chart.

Now you are able to deal with data into Excel and from there to a chart so building the list you asked about in the same (first) posting should be kids play.

For 1a) I would load the list box you already have with all the suppliers and then let the user select one of them before clicking the button to launch the monthly report.

Since you will then be using all these items in a form and the form's buttons produce different results depending on user input you have already answered 2).

I have included the modified project in a zip file with all my modified code included. Please read my comments, I put them there for a reason, and I am sure you will figure out the rest.

Hope this helps.

Happy coding

Yomet
Attached Files
File Type: zip OK-Modified.zip (47.7 KB, 37 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: VB and Excel.

 
0
  #6
Feb 25th, 2006
Thumbs up to Yomet on this one. Nice code, and good way to redirect focus back into the forum instead of to his e-mail. Well Done.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: VB and Excel.

 
0
  #7
Feb 26th, 2006
Thanks Comatose
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 11
Reputation: vishalsomani is an unknown quantity at this point 
Solved Threads: 0
vishalsomani vishalsomani is offline Offline
Newbie Poster

Re: VB and Excel.

 
0
  #8
Feb 27th, 2006
Hello,
Thanks for your mail.
I have now created the VB chart.
Lastly, I needed yur help in this writing a letter.
I have attached the copy of the letter, which shows thats how the letter should be like.

I only have to show one letter for any supplier.., with a command button which is done on form5.

The letter format is also attached.
I don't know how to call the variables if you can help me please.
Attached Files
File Type: doc Letter.doc (19.5 KB, 17 views)
File Type: zip New.zip (73.4 KB, 18 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: VB and Excel.

 
0
  #9
Mar 1st, 2006
Hi again Vishal,

Happy to hear that things are working well and nice to see some good coding on your part.

Now for your latest problem, I am not sure that trying to use the Word's MailMerge functionality is the best way to go. All you will do is to create a template that you can then use with any specific supplier from within Word, not your application.

I am assuming that your assignment needs to have you make a letter, a full letter not a MailMerge template, from within your program.

Here's what I would do:

Add two combo boxes to your form, one will contain all your suppliers and the other will then be dynamically populated with the orders for the supplier selected in the first combo box. In order to do this create a Sub that will populate the second combo box when the user selects a supplier in the first one.

Once the user has selected both a supplier and an order (hint: use the OnClick of the second combo to determine if the user has selected both) and clicks the button you open a recordset in your program that contains all the supplier information. You use this recordset to create your letter header, inserting the actual values into the Word document instead of inserting MailMerge fields. It should actually be easier since you can use the same technique as with the fixed MIS company address. All you need to change are fixed strings for recordset fields.

Next comes the order details, you would then open another recordset, or reuse the first one with a different SQL string in your .Open statement, just remember to close the recordset first...
Once you have opened this recordset you can easily loop through it to create your detail section, using a variable for the total amount of the order.

Seeing how far you came with your code from my last post I think this is about all you need in order to complete this assignment.

If you have more questions - go ahead and ask, I'll help as much as I can.

Happy coding

Yomet
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 11
Reputation: vishalsomani is an unknown quantity at this point 
Solved Threads: 0
vishalsomani vishalsomani is offline Offline
Newbie Poster

Re: VB and Excel.

 
0
  #10
Mar 2nd, 2006
Hi Danny,
Thanks for your immediate reply.
I have got to hand in my Vb work on March 3rd so please reply as quick as possible.

I really don't know how to populate dynamically values from one combo box to another.

Can you tell me what code do i need to write and on which methods of which combo box please?
Once i get that,
I can start making my letter.
Please reply as soon as you can.

I need two combo boxes, the first one to be supplierID and secondone to be orderID.

I have populated supplierID combo box, the only problem is how to get related orderID's from supplierID.
Thanks Vish.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC