Results 1 to 5 of 5

Thread: Problem sorting by alphabetical order...

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    3

    Problem sorting by alphabetical order...

    Hello:

    I've got a lists of risk ids that display in one box "strID" and I've created another box "strID1"to display the risk ids as well as the descriptions. I can't figure out how to display them in alphabetical order. The code is below:

    Private Sub cmdProcess_Click()
    Dim x, y As Integer

    Dim cBox, cQuery, cQuery1 As String

    Dim strID, strID1, strThreshhold As String, rs As dao.Recordset, sql As String

    strID = ""

    If Me.cboQuarter.Value <> "" And Me.cboYear.Value <> "" And Me.cboSite.Value <> "" And Me.cbothreshold.Value <> "" Then

    If Trim(Me.cboSite.Value) = "All" Then

    Me.cboSite.Value = ""

    End If

    cQuery = "quarter = '" & Trim(Me.cboQuarter.Value) & "' And year = " & Trim(Str(Me.cboYear.Value)) & " And Risk_ID like '" & Trim(Me.cboSite.Value) & "*'"

    For x = 1 To 5
    For y = 1 To 5

    strID = ""

    sql = "select risk_ID, risk_description from risks where " & cQuery & strThreshhold & " And Int(residual_consequence_score) =" & Trim(Str(y)) & " And Int(residual_likelihood_score) = " & Trim(Str(x))

    Set rs = CurrentDb.OpenRecordset(sql)

    cBox = "Risk" & Trim(Str(x)) & Trim(Str(y))

    Me.Controls(cBox).Value = ""
    Do Until rs.EOF


    strID = strID & " " & rs("risk_id")
    strID1 = strID1 & " " & rs("risk_id") & " - " & rs("risk_description") & vbCrLf '****Can anyone get this to display alphabetically****?
    rs.MoveNext

    Me.Controls(cBox).Value = strID

    Loop

    Next y
    Next x

    Me.Controls!RiskDesc = strID1

    If Trim(Me.cboSite.Value) = "" Then

    Me.cboSite.Value = "All"

    End If

    Me.cmdPrint.Enabled = True

    Me.Refresh

    Else

    MsgBox ("Please select a value before clicking the Generate Chart button")

    End If

    Debug.Print sql

    End Sub

    Any help is appreciated. I'm a novice coder and using VB 6.5 and Access 2003.

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: Problem sorting by alphabetical order...

    Code:
    sql = "SELECT risk_ID, risk_description 
           FROM    risks  
           WHERE " & cQuery & strThreshhold & 
                     " And Int(residual_consequence_score) =" & 
                       Trim(Str(y)) & 
                     " And Int(residual_likelihood_score) = " & 
                       Trim(Str(x))
          ORDER BY risk_description 

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    3

    Re: Problem sorting by alphabetical order...

    Thanks for the suggestion... Unfortunately this is still not working. Is the new sql statement in the correct place? It needs to be after the loop so it just affects the display box w/ the descriptions.

    Code:
    Private Sub cmdProcess_Click()
    Dim x, y As Integer
    
    Dim cBox, cQuery, cQuery1 As String
    
    Dim strID, strID1, strThreshhold As String, rs As dao.Recordset, sql As String
    
    strID = ""
    
    
    If Me.cboQuarter.Value <> "" And Me.cboYear.Value <> "" And Me.cboSite.Value <> "" And Me.cbothreshold.Value <> "" Then
    
         If Trim(Me.cboSite.Value) = "All" Then
        
         Me.cboSite.Value = ""
             
         End If   
    
         cQuery = "quarter = '" & Trim(Me.cboQuarter.Value) & "' And year = " & Trim(Str(Me.cboYear.Value)) & " And Risk_ID like '" & Trim(Me.cboSite.Value) & "*'"
         
         For x = 1 To 5
    
         For y = 1 To 5
    
         strID = ""
    
         sql = "select risk_ID, risk_description from risks where " & cQuery & strThreshhold & " And Int(residual_consequence_score) =" & Trim(Str(y)) & " And Int(residual_likelihood_score)  = " & Trim(Str(x))
          
         Set rs = CurrentDb.OpenRecordset(sql)
    
         cBox = "Risk" & Trim(Str(x)) & Trim(Str(y))
    
         Me.Controls(cBox).Value = ""
         Do Until rs.EOF
                  
    
         strID = strID & " " & rs("risk_id")
         strID1 = strID1 & " " & rs("risk_id") & " - " & rs("risk_description") & vbCrLf
         rs.MoveNext
         Me.Controls(cBox).Value = strID
    
         Loop
    
         sql1 = "SELECT risk_ID, risk_description from risks where " & cQuery & strThreshhold & " And Int(residual_consequence_score) =" & Trim(Str(y)) & " And Int(residual_likelihood_score) = " & Trim(Str(x)) & " order by risk_ID"
    
         Next y
    
         Next x
        
             Me.Controls!RiskDesc = strID1
        
         If Trim(Me.cboSite.Value) = "" Then
        
         Me.cboSite.Value = "All"
         
                  
         End If
    
         Me.cmdPrint.Enabled = True
    
         Me.Refresh
    
    Else
    
        MsgBox ("Please select a value for Site before clicking the Generate Chart button")
    
    End If
    
      Debug.Print sql
    
    End Sub

  4. #4
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: Problem sorting by alphabetical order...

    Sorry, didn't checked out the rest of your code...
    It seems to me that you want to fill a Combo box with some data from a recordset
    Then you don't need the For-Next

    1.- Open your recordset with the desired filters (quarter, year, site, etc) and order it by description
    2.- Then process your recordset form bof to eof and add items to the combobox

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    3

    Re: Problem sorting by alphabetical order...

    Quote Originally Posted by jggtz
    Sorry, didn't checked out the rest of your code...
    It seems to me that you want to fill a Combo box with some data from a recordset
    Then you don't need the For-Next

    1.- Open your recordset with the desired filters (quarter, year, site, etc) and order it by description
    2.- Then process your recordset form bof to eof and add items to the combobox
    Thanks... Will try out.

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