Results 1 to 6 of 6
  1. #1
    akneppers is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    3

    Need HELP Graphing Query from SOL Visual Basic

    Been Trying to Figure out how to fix my code but im trying to graph data from my query "STBDFWDQuery" but it will only graph if i it links to the STBDFWD table and not the Query. I will reference my code below:

    Option Compare Database
    Option Explicit


    Private Sub Date_Range_Click()


    On Error GoTo Date_Range_Click_Err
    DoCmd.OpenQuery "STBDFWDQuery", acViewNormal, acEdit
    DoCmd.OpenQuery "STBDFWDQuery", acViewNormal, acReadOnly


    Date_Range_Click_Exit:
    Exit Sub


    Date_Range_Click_Err:
    MsgBox Error$
    Resume Date_Range_Click_Exit


    End Sub

    Private Sub Check43_AfterUpdate()
    RunRpt
    End Sub


    Private Sub Check44_AfterUpdate()
    RunRpt
    End Sub


    Private Sub Check45_AfterUpdate()
    RunRpt
    End Sub


    Private Sub Form_Open(Cancel As Integer)
    Me.Move 0, 300
    End Sub




    Sub RunRpt()
    ' On Error GoTo Err_RunRpt
    Dim stDocName As String
    Dim strInclude As String
    Dim strInclude1 As String
    Dim strInclude2 As String
    Dim strInclude3 As String
    Dim strSQL As String
    Dim strSQL1 As String
    Dim strSQL2 As String
    Dim strSQL3 As String

    ' Check to see if we include the value for each checkbox
    If Forms!DateQueryForm.Check43 Then
    strInclude = "STBDFWD.CableCount,"
    End If

    If Forms!DateQueryForm.Check44 Then
    strInclude1 = "STBDFWD.Dyno1Tension,"
    End If

    If Forms!DateQueryForm.Check45 Then
    strInclude2 = "STBDFWD.Dyno2Tension,"
    End If

    If Forms!DateQueryForm.Check46 Then
    strInclude3 = "STBDFWD.CableSpeed,"
    End If

    If strInclude <> "" Then
    ' cuts off the last comma
    strInclude = Left(strInclude, Len(strInclude) - 1)
    ' adds syntax we need
    strInclude = "In(" & strInclude & ")"


    ' creates the new SQL string for the chart's row source with strInclude concatenated in
    '


    strSQL = "TRANSFORM Avg(STBDFWD.CableCount) AS CountOfOrders " & _
    "SELECT (Format([Opto22Time],'H:NNAMPM')) AS Expr1 " & _
    "FROM STBDFWD WHERE STBDFWD.CableCount " & strInclude & _
    "GROUP BY (Int([Opto22Time]*1440)),(Format([Opto22Time],'H:NNAMPM'))" & _
    "PIVOT 'Cable Count';"

    End If
    ' Report Name
    stDocName = "OrdersWithChart"
    ' keep changes from being visible
    Application.Echo False


    ' we have to open the report in design view in order to set the new row source
    ' of the chart. We do it hidden so nobody sees it happening.
    DoCmd.OpenReport "OrdersWithChart", acViewDesign, , , acHidden
    ' sets the row source of the chart to be the one we are substituting
    Reports!OrdersWithChart.MyGraph.RowSource = strSQL
    ' closes and saves the report
    DoCmd.Close acReport, stDocName, acSaveYes
    ' reopens it, this time so the user can see it.
    DoCmd.OpenReport stDocName, acViewPreview
    Application.Echo True






    Exit_RunRpt:
    Application.Echo True
    Exit Sub


    Err_RunRpt:
    MsgBox Err.Description
    Resume Exit_RunRpt
    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    build a chart,
    apply your query,
    edit the 'data' in the chart, and paste real data from your query.
    format the chart as needed.
    save the chart.

    now the chart should change along with the query.

  3. #3
    akneppers is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    3
    Well is there a way to do it through a form. Currently the user clicks a box and then it outputs to a report with the graph. Only want certain date and times selected.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Apparently, CROSSTAB query with filter criteria requires PARAMETERS clause. Review http://allenbrowne.com/ser-67.html#Param

    I have built many graphs for my DBs and none require CROSSTAB - lucky me. Think they are all XYScatter type. I have helped with a pie chart, can't remember if that needed CROSSTAB.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    akneppers is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    3
    Im a little confused about the parameters, would either of the FROM statements work or do i have it wrong?

    strSQL = "TRANSFORM Avg([CableCount]) AS CountOfOrders " & _
    "SELECT (Format([Opto22Time],'H:NNAMPM')) AS Expr1 " & _
    "FROM STBDFWD WHERE (((STBDFWD.Opto22Date)>=[Forms]![DateQueryForm]![Start_Date] AND (STBDFWD.Opto22Date)<=[Forms]![DateQueryForm]![End_Date]) AND ((STBDFWD.Opto22Time)>=[Forms]![DateQueryForm]![Start_Time] And (STBDFWD.Opto22Time)<=[Forms]![DateQueryForm]![End_Time]))" & strInclude & _
    "GROUP BY (Int([Opto22Time]*1440)),(Format([Opto22Time],'H:NNAMPM'))" & _
    "PIVOT 'Cable Count';"
    ' "FROM STBDFWD WHERE ((STBDFWD.Opto22Date Between #[Forms]![DateQueryForm]![Start_Date]# AND #[Forms]![DateQueryForm]![End_Date]#) AND (STBDFWD.Opto22Date Between #[Forms]![DateQueryForm]![Start_Time]# And #[Forms]![DateQueryForm]![End_Time]#))" & _

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    What happens when you use either version?

    What exactly do you not understand about the use of PARAMETERS? The article seems clear enough. Build CROSSTAB query object with the query designer to get the correct syntax to use in VBA. Switch to SQL View to see the SQL and how it is constructed with PARAMETERS clause.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. select query in visual basic
    By suman ghalley in forum Programming
    Replies: 1
    Last Post: 05-18-2017, 02:42 AM
  2. Visual Basic
    By Darth21 in forum Access
    Replies: 4
    Last Post: 08-11-2014, 06:18 AM
  3. Replies: 2
    Last Post: 03-17-2014, 12:51 PM
  4. Visual Basic
    By kwooten in forum Queries
    Replies: 2
    Last Post: 01-25-2012, 08:22 AM
  5. Query in visual basic
    By Lucas83 in forum Programming
    Replies: 1
    Last Post: 06-10-2010, 11:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums