Results 1 to 10 of 10

Thread: [RESOLVED] Another SQL Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Resolved [RESOLVED] Another SQL Question

    I am trying to get a percentage of how many products have been counted by using SQL statements. I have one that returns how many have been counted (checked) and another for how many have not been counted (needToCheck). I get a datatypoe mismatch when I try to equate the long variables to the connection execution. I was wondering if any knew how I could get the numbers I want easily rather than looping through the table twice. I would like to just be able to divide checked by needToCheck and multiply by 100 and output it to a label to let people know how far along they have come. Any suggestions? Thank you.

    VB Code:
    1. Private Sub completed()
    2.     Dim checked As Long
    3.     Dim needToCheck As Long
    4.     Dim sql As String
    5.    
    6.     Set connection = New ADODB.connection
    7.     connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    8.         "Data Source= G:\Finance\Inventory Control\CycleCount\DB\cycleCount.mdb"
    9.     connection.Open
    10.    
    11.     sql = "SELECT Count(PROD) AS Expr1 " & _
    12.               "From TOGO " & _
    13.               "WHERE (((TOGO.CHECKED)=TRUE));"
    14.    
    15.     checked = connection.Execute(sql)
    16.    
    17.     sql = "SELECT Count(PROD) AS Expr1 " & _
    18.               "From TOGO " & _
    19.               "WHERE (((TOGO.CHECKED)=FALSE));"
    20.  
    21.     needstoCheck = connection.Execute(sql)
    22. End Sub

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  2. #2
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    Philippines
    Posts
    468

    Re: Another SQL Question

    create a query on your database joining the two queriess and then
    you can use select (expr1/expr2)*100 as product_percentage from query_name

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Another SQL Question

    I'm not sure that I follow you. Is there no way I can get the number that the select statements return in the recordset?

  4. #4
    Lively Member Venus's Avatar
    Join Date
    Aug 2005
    Posts
    78

    Re: Another SQL Question

    You can group multiple select statements.

    SELECT (SELECT Count(PROD) From TOGO WHERE (((TOGO.CHECKED)=TRUE))) AS Checked, (SELECT Count(PROD) AS Expr1 From TOGO WHERE (((TOGO.CHECKED)=FALSE))) AS Unchecked

    That command does only 1 trip to the server and it's a matter now of taking the value of (Checked/Unchecked) * 100

    You can even do it in one command in SQL including the math, I hope my parenthesis match up.

    SELECT ((SELECT Count(PROD) From TOGO WHERE (((TOGO.CHECKED)=TRUE))) / (SELECT Count(PROD) AS Expr1 From TOGO WHERE (((TOGO.CHECKED)=FALSE)))) * 100 AS RESULT

    Now, you get one field returned and I think you'd reference it by Recordset!Result, you'd have to convert it to a string. That works with SQL Server, I'm assuming it will work with Access.

    For a tip, your tables should begin with a tb. As in, call it tbToGo because if you move off of Access into a SQL database, you now have stored procedures, views, and tables. It's easier to see tbToGo is a table, spToGo is a stored procedure, and vToGo is a view. I found it to be very useful.
    Last edited by Venus; Mar 9th, 2006 at 02:11 PM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Another SQL Question

    Thank you very much Venus; however, I would also like to know how to retrieve the information from the recordset. Whenever I execute the SQL statement the information will be stored in a recordset, and I need to get that information so I can output the percentage to a label.

  6. #6
    Lively Member Venus's Avatar
    Join Date
    Aug 2005
    Posts
    78

    Re: Another SQL Question

    Hmm... if I think I understand you'd do the following
    VB Code:
    1. Private Sub completed()
    2.     Dim checked As Long
    3.     Dim needToCheck As Long
    4.     Dim sql As String
    5.     Dim rs as new ADODB.Recordset ' Here
    6.    
    7.     Set connection = New ADODB.connection
    8.     connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    9.         "Data Source= G:\Finance\Inventory Control\CycleCount\DB\cycleCount.mdb"
    10.     connection.Open
    11.    
    12.     sql = "SELECT (SELECT Count(PROD) From TOGO WHERE (((TOGO.CHECKED)=TRUE))) AS Checked" & _
    13.  ", (SELECT Count(PROD) From TOGO WHERE (((TOGO.CHECKED)=FALSE))) AS Unchecked"
    14.  
    15.     Set rs = connection.Execute(sql)
    16.  
    17.     if RS.EOF Then
    18.         ' We have no records.  Hmm....
    19.         Msgbox("No records found")
    20.         Exit Sub
    21.     End if
    22.  
    23.     if rs!Unchecked = 0 Then ' Need to test if unchecked is 0
    24.           LabelToGo.Text = "0" ' Else, in formula below get a divide by zero error
    25.           Exit Sub
    26.     End If
    27.  
    28.     ' At this point, rs!Checked holds the number of Checked and rs!Unchecked holds the Unchecked
    29.  
    30.     LabelToGo.Text = cStr((rs!Checked / rs!Unchecked) * 100)
    31. End Sub

    That gives you a start I hope, and I hope my parenthesis all match up.
    Last edited by Venus; Mar 9th, 2006 at 04:15 PM.

  7. #7
    Lively Member Venus's Avatar
    Join Date
    Aug 2005
    Posts
    78

    Re: Another SQL Question

    By the way, I edited my original code above and trimmed the fat off it.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: Another SQL Question

    That will work, thank you very much Venus.

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  9. #9
    Lively Member Venus's Avatar
    Join Date
    Aug 2005
    Posts
    78

    Re: [RESOLVED] Another SQL Question

    Sigh, I hope you were able to figure out I was incorrect on what the fields were called. We were doing two "AS" Statements... like
    Select (Select formula1 as Expr1 from tb1) As Checked

    I went on the Expr1 but it was superseded by the second As and renamed to Checked. It's rs!Checked and rs!Unchecked. I modified my code to correct a second time. *Sigh*. Hate when that happens but anyway good luck!

  10. #10
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: [RESOLVED] Another SQL Question

    I think the original problem may have been expecting the SELECT statement to return a value, rather than a recordset, as Venus alluded to. I'd also second her/his practice of naming objects with a prefix - tblFoo, qryFoo, etc - to make it easier to see what you're dealing with in code.
    I've had plenty of AAAGGGHHH!!! stories about dealing with legacy work where tables, fields, macros, queries, reports, etc, were all named the same thing, often with reserved words (Date being a common one). Don't do it.
    Tengo mas preguntas que contestas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width