none
Recursive join & data-bound combo box RRS feed

  • Question

  • Namaste!

    This ought to be relatively simple...

    I have a Jet database that contains the table of interest, tblCategory.  tblCategory has three attributes of interest for this issue:  fldCategoryID, fldParentCategoryID, and fldName.  There is a self-join from fldCategoryID to fldParentCategoryID (one-to-many).

    On my form I wish to have a combobox display all the categories available (shows the fldName attribute of tblCategory) and assign the selected value to fldParentCategoryID.

    So, my form is based on a dataset that is attached to tblCategory in the normal way.  That works just fine.

    To facilitate the combobox, I created another dataset that simply contains fldCategoryID and fldName attributes and is non-editable - imaginatively named dsLUCategory (dataset lookup Category).

    I bound the combobox to dsLUCategory (it created all the relevant binding source, etc., controls).  The display member is fldName, the value member is fldCategoryID, and the selected value is bound to TblCategoryBindingSource.fldParentCategoryID.  TblCategoryBindingSource is the main data supplier for everything else on the form.

    That all *seems* to make sense.

    However, on test, when a record is created and saved, I then rebind/fill the binding source for the combobox and then the combobox "selects" the current category (e'en though I did NOT physically or by code select anything).  Consequently, there can be no subsequent adds due to a record issue.

    Now, I haven't touched VB in about 8 months, so I'm probably missing something really obvious.

    Anyone have any thoughts?

    Thanks!

    Peace, Love, and Light,

    /s/ Jon C. Munson II
    • Edited by Jon C. Munson II Wednesday, April 15, 2009 4:32 PM forgot to say "thanks"!!!
    Wednesday, April 15, 2009 4:30 PM

Answers

  • Well, this all seems a little silly to have to do, but, here's how I solved this particular issue (at least for now):
        Private Function check() As Boolean
            Dim bRet As Boolean
            Dim r As DataRow
            Dim rv As DataRowView
    
            bRet = False
    
            rv = CType(Me.BindingSource.Current, DataRowView)
            r = rv.Row
            If Len("" + r.Item("attribOfInterest").ToString()) > 0 Then
                bRet = True
            End If
    
            check = bRet
    
        End Function
    
        Private Sub setComboSelection()
            If Not check() Then Me.attribOfInterestComboBox.SelectedIndex = -1
        End Sub
    
        Private Sub BindingSource_PositionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingSource.PositionChanged
            setComboSelection()
        End Sub
    
        Private Sub BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorSaveItem.Click
            
            ' code here
            
            setComboSelection()
    
            ' more code here
     
        End Sub
    If there is a better way, I'd be happy to entertain it...

    Thanks!

    Peace, Love, and Light,

    /s/ Jon C. Munson II
    Thursday, April 16, 2009 3:54 PM

All replies

  • Are you saying you can add one record, but you cannot add another?

    What is the error that you are getting?
    Wednesday, April 15, 2009 6:16 PM
  • The error message isn't as relevant as the issue, which is why the combobox is selecting something to begin with.

    I've been working on the Mac for the last 8 months, so I'm a little focused on Cocoa yet (my application is available on both platforms, specifically written for each).  Prior to that I had done up my app in VB.  This version is a derivative, but new, version of the "product family."  In Cocoa, a popup is more like the combobox here.  The Cocoa popup displays a "blank" when nothing is chosen.

    Does the combobox here automatically choose the first item in the list?
    Wednesday, April 15, 2009 6:33 PM
  • Depends on the style of combo box you are using. Check out the DropDownStyle property to see if the DropDown or DropDownList best meets your needs.

    Otherwise, you can use cb.SelectedIndex = -1 to cause it to select no value.

    Hope this helps.
    Wednesday, April 15, 2009 6:40 PM
  • Neither one solves the issue.  For kicks and giggles I checked the selected index of the cbo and noted it was zero after the rebind which would correspond to the first item in the list.

    In thinking about the SelectedIndex, isn't there a better way?

    In order to set SelectedIndex properly, one will have to set it to -1 on every rebind, and on every record move, except when the parent is present (if it can be done accurately).

    Kind of a PITA, yes?

    Is there no other way?
    Wednesday, April 15, 2009 7:13 PM
  • Well, this all seems a little silly to have to do, but, here's how I solved this particular issue (at least for now):
        Private Function check() As Boolean
            Dim bRet As Boolean
            Dim r As DataRow
            Dim rv As DataRowView
    
            bRet = False
    
            rv = CType(Me.BindingSource.Current, DataRowView)
            r = rv.Row
            If Len("" + r.Item("attribOfInterest").ToString()) > 0 Then
                bRet = True
            End If
    
            check = bRet
    
        End Function
    
        Private Sub setComboSelection()
            If Not check() Then Me.attribOfInterestComboBox.SelectedIndex = -1
        End Sub
    
        Private Sub BindingSource_PositionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingSource.PositionChanged
            setComboSelection()
        End Sub
    
        Private Sub BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorSaveItem.Click
            
            ' code here
            
            setComboSelection()
    
            ' more code here
     
        End Sub
    If there is a better way, I'd be happy to entertain it...

    Thanks!

    Peace, Love, and Light,

    /s/ Jon C. Munson II
    Thursday, April 16, 2009 3:54 PM