Google Answers Logo
View Question
 
Q: Display a Access chart inside VB6 application? ( No Answer,   5 Comments )
Question  
Subject: Display a Access chart inside VB6 application?
Category: Computers > Programming
Asked by: jstm-ga
List Price: $20.00
Posted: 12 Mar 2005 12:52 PST
Expires: 11 Apr 2005 13:52 PDT
Question ID: 493366
I have a VB6 application that I use a button to open a Access chart
(using macro) at runtime, and I would like the chart to display inside
my Parent MDI form?

How can I display an access chart inside my VB6 program?
Answer  
There is no answer at this time.

Comments  
Subject: Re: Display a Access chart inside VB6 application?
From: willcodeforfood-ga on 12 Mar 2005 21:11 PST
 
There is no way to embed an Access form inside your VB MDI
application.  To view your chart in VB, follow these instructions,
create your chart inside VB by following these instructions:

You need the professional edition of VB, otherwise you won?t have the
necessary charting control.  If you can get through step #2 in the
following instructions, you do have the professional edition of VB.

1. Right click on the toolbox in the VB designer and choose the
Components... command.  The toolbox is the section of the screen where
you choose which control you want to put on your form (usually along
the left side).
2. Scroll down and choose Microsoft Chart Control 6.0 (OLEDB)
3. You will see a new tool in the toolbox that looks like a chart.
4. Click on the chart tool and draw a chart object onto your form,
just like you?d draw a button or textbox.  VB will name the chart
MSChart1.
5. Right click on the new MSChart object, choose Properties, and set
as many display properties as you want to, such as background and line
colors, etc.

Now you need to write a query that will get your data:

Open SQL Query Analyzer and write a query that can return two fields. 
The first is the failure reason and the second is the number of
failures:

FailureReason	NumberFailures
=============	==============
failed test A	45
failed test Z   76
failed test Q   135

If you need help with this, you?ll need to tell me what fields you
have and in what tables, etc. and I can walk you through it.  An
example might be:

select FailureReason, count(*) as NumberFailures from FailuresTable
where ProductionArea = 12 and WorkCenter = 34 and FailureDate between
'1/1/2005' and '1/31/2005'

Later we'll work on substituting in the parameters from the VB form
into the query to get the appropriate production area, work center and
dates.  For now just hard-code the values into your query.  Make sure
your query runs in Query Analyzer before continuing.

Now, picking up where we left off working on the form at step 5 above:

6. Add a button to the form, call it cmdShowChart and double-click on
it to add a Click event to the button.
7. Add the following code to the button?s Click event:

====================================== begin code

Private Sub cmdShowChart_Click()
    With MSChart1
        .Title.Text = "Failure Reason Breakdown"
        .Title.VtFont.Name = "Arial"
        .Title.VtFont.Size = 12
        .Plot.AutoLayout = True
        .Title.Location.LocationType = VtChLocationTypeTop
        .ShowLegend = True
    End With

    Dim strQuery As String

    Dim cn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    cn.ConnectionString = "your connection string
    cn.Open
    strQuery = "the query that you got working before"
    rs.Open strQuery, cn, adOpenKeyset
    
    Dim cols As Integer
    cols = rs.RecordCount
    
    Dim X() As Variant
    
    If cols > 0 Then
        ReDim X(1 To cols, 1 To 2)
        Dim i As Integer
        i = 1
        While Not rs.EOF
            X(i, 1) = "R " & rs("TypeOfProductID")
            X(i, 2) = rs("Total")
            i = i + 1
            rs.MoveNext
        Wend
        MSChart1.ChartData = X
    End If
    
End Sub

====================================== end code

Add a reference in your project to the Microsoft ActiveX Data Objects
2.7 Library like I explained in the other questions.

Once you get this far, we can see what else you need and figure out
how to do the rest.
Subject: Re: Display a Access chart inside VB6 application?
From: willcodeforfood-ga on 12 Mar 2005 22:45 PST
 
Replace the VB that looks like this:

        While Not rs.EOF
            X(i, 1) = "R " & rs("TypeOfProductID")
            X(i, 2) = rs("Total")
            i = i + 1
            rs.MoveNext
        Wend

With this instead:

        While Not rs.EOF
            X(i, 1) = rs("FailureReason")
            X(i, 2) = rs("NumberFailures")
            i = i + 1
            rs.MoveNext
        Wend

I have to get my sample code running and then replace pieces before
posting here, since my setup isn't exactly same as yours.  Just forgot
to replace that part.
Subject: Re: Display a Access chart inside VB6 application?
From: jstm-ga on 13 Mar 2005 14:48 PST
 
Thanks! This works great. Is there a way I can make it ask me for the
Start Date and End date?
Subject: Re: Display a Access chart inside VB6 application?
From: willcodeforfood-ga on 13 Mar 2005 16:25 PST
 
Start by adding two textboxes to your form called txtDateBegin and txtDateEnd.

You've got a line in your current VB code that will look something like this:

    strQuery = "select FailureReason, count(*) as NumberFailures from " & _
               " FailuresTable where ProductionArea = 12 and " & _
               " WorkCenter = 34 and FailureDate between " & _
               "'1/1/2005' and '1/31/2005'"

Change this line to:

    If Not IsDate(txtDateBegin) Then  ' validate date to avoid runtime errors
        MsgBox "Enter a valid begin date."
        Exit Sub
    End If
    If Not IsDate(txtDateEnd) Then ' validate date to avoid runtime errors
        MsgBox "Enter a valid end date."
        Exit Sub
    End If
    ' build the query by substituting the user's values into the SQL statement
    strQuery = "select FailureReason, count(*) as NumberFailures from " & _
               " FailuresTable where ProductionArea = 12 and " & _
               " WorkCenter = 34 and FailureDate between " & _
               "'" & txtDateBegin & "' and '" & txtDateEnd & "'"

You can add as many parameters as you like this way.  You'll want to
use combobox and listbox controls to allow the user to select the
production area and work center parameters.  As we have just done for
the date parameter, simply modify the statement in VB that is building
the strQuery variable.

... where ProductionArea = 12 and WorkCenter = 34 and ...

becomes

... where ProductionArea = " & cmbProductionArea & " and WorkCenter =
" & cmbWorkCenter & " and ...

It also helps to set the dates to a useful default when the form loads like this:

Private Sub Form_Load()
    txtDateBegin = Month(Now) & "/1/" & Year(Now)
    txtDateEnd = DateAdd("d", -1, DateAdd("m", 1, txtDateBegin))
End Sub
Subject: Re: Display a Access chart inside VB6 application?
From: jstm-ga on 13 Mar 2005 17:11 PST
 
THANKS AGAIN! This works Great...

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy