Results 1 to 9 of 9

Thread: [RESOLVED] I don't know whats wrong.. sorting by dcombo.. datagrid.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    18

    Resolved [RESOLVED] I don't know whats wrong.. sorting by dcombo.. datagrid.

    i'm trying to sort datagrid by branch name.



    HTML Code:
    Private Sub dcmbSortBranch_Click(Area As Integer)
    Dim BranchSearh As String
    
    BranchSearch = UCase(dcmbSortBranch.Text)
    
    If BranchSearch = "" Then
    Adodc1.RecordSource = "SELECT e.end_of_day_report_id, e.report_date, b.branch_name, e.total_sales, e.total_expenses, e.details_expenses, e.total_unsold, e.remarks FROM end_of_day_reports e, branch_tbl b WHERE e.branch_id=b.branch_id"
    Else
        Adodc1.Recordset.MoveFirst
        Do While Adodc1.Recordset.EOF = False
            If BranchSearch = Adodc1.Recordset.Fields("branch_name") Then
                Adodc1.RecordSource = "SELECT e.end_of_day_report_id, e.report_date, b.branch_name, e.total_sales, e.total_expenses, e.details_expenses, e.total_unsold, e.remarks FROM end_of_day_reports e, branch_tbl b WHERE e.branch_id=b.branch_id AND b.branch_name =' & BranchSearch & '"
            End If
            Adodc1.Recordset.MoveNext
        Loop
        
      
    End If
    End Sub

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    18

    Re: I don't know whats wrong.. sorting by dcombo.. datagrid.

    sorry.. it's not sorting.. i want to retrieve record with the branch name when i select in the dcombo...

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: I don't know whats wrong.. sorting by dcombo.. datagrid.

    Thread moved to 'Database Development' forum (the 'VB6' forum is only meant for questions which don't fit in more specific forums)

    I don't understand why you put in the loop etc... the only line you should have between the Else and the matching End If is the Adodc1.RecordSource one.


    By the way, if you haven't seen it already I recommend reading the article Why is using bound controls a bad thing? from our Database Development FAQs/Tutorials (at the top of this forum)

    That article contains a link to the recommended alternative (ADO code), which has an example that replicates the main features of the data control. There are many other articles in the "Classic VB - ADO" section of the FAQs thread (particularly "the further steps" article), which help you to extend it in many ways - like adding search functionality, working with other controls, etc.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    18

    Re: I don't know whats wrong.. sorting by dcombo.. datagrid.

    i changed the code:
    Code:
    Dim BranchSearh As String
    
    BranchSearch = UCase(dcmbSortBranch.Text)
    
    If BranchSearch = "" Then
    Adodc1.RecordSource = "SELECT e.end_of_day_report_id, e.report_date, b.branch_name, e.total_sales, e.total_expenses, e.details_expenses, e.total_unsold, e.remarks FROM end_of_day_reports e, branch_tbl b WHERE e.branch_id=b.branch_id"
    ElseIf BranchSearch = Adodc1.Recordset.Fields("branch_name") Then
    Adodc1.RecordSource = "SELECT e.end_of_day_report_id, e.report_date, b.branch_name, e.total_sales, e.total_expenses, e.details_expenses, e.total_unsold, e.remarks FROM end_of_day_reports e, branch_tbl b WHERE e.branch_id=b.branch_id AND b.branch_name =' & BranchSearch & '"
    End If
    nothing happens

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: I don't know whats wrong.. sorting by dcombo.. datagrid.

    Well this part is a problem:
    Code:
    AND b.branch_name =' & BranchSearch & '"
    ...because it will search the field b.branch_name for the text " & BranchSearch & ", rather than doing anything with your BranchSearch variable.

    Take a look at the FAQ article How can I put the value of a variable/control into my SQL statement?

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    18

    Re: I don't know whats wrong.. sorting by dcombo.. datagrid.

    i change it to this..

    Code:
    b.branch_name ='" & BranchSearch & "'"
    still nothing is happening.. i'm just confused now..

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: I don't know whats wrong.. sorting by dcombo.. datagrid.

    Well that's as far as I can help - because like most people I don't use bound-controls.

    All I can suggest is that you read the other article in the "Classic VB - Data-bound controls" section of the DB FAQs, which is likely to help.

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: I don't know whats wrong.. sorting by dcombo.. datagrid.

    What is the purpose of the ElseIf statement? If that statement returns False, nothing will happen. You just need an Else statement.

    Call ADODC1.Refresh after the If statement.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    18

    Re: I don't know whats wrong.. sorting by dcombo.. datagrid.

    i have fixed it.. tnx

    Code:
    If dcmbSort.Text = "" Then
    Adodc1.Refresh
    Else
    Adodc1.RecordSource = "SELECT e.end_of_day_report_id, e.report_date, b.branch_name, e.total_sales, e.total_expenses, e.details_expenses, e.total_unsold, e.remarks FROM end_of_day_reports e, branch_tbl b WHERE e.branch_id=b.branch_id AND b.branch_name='" & dcmbSort.Text & " '"
    Adodc1.Refresh
    End If
    End Sub

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