Results 1 to 20 of 20

Thread: [RESOLVED] Add code to existing: Populate combobox from database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2013
    Posts
    100

    Resolved [RESOLVED] Add code to existing: Populate combobox from database

    Good afternoon,
    I know I have alot to learn so please bare with me Thank you in advanced for any help.

    I have the following code which pulls values into a listview control. This works perfectly.

    However I would also like to pull in values from another table in the database into certain combo boxes. I am not sure where to place this code within the current code.

    Below is my current code
    Code:
    Private Sub Form_Load()
    
    Dim myConn As ADODB.Connection
    
    Set myConn = New ADODB.Connection
    myConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\Business Intelligence\VB6\Database9.mdb;"
    myConn.Open
    
    Dim strSQL As String 'our query string
    Dim oRS As ADODB.Recordset 'our recordset object
    Dim lvwItem As ListItem 'this is necessary to directly reference the ListView control
    Set oRS = New ADODB.Recordset
     
    'change this SQL as appropriate for your needs
    strSQL = "SELECT Date_Entered, Loan_Number, Investor, State, Doc_type, Description, Number_Pages,Tracking_Number,Address, Processor, Reviewer1, executor1, Reviewer2, Executor2, Notes, Attorney, PLX, Clarifire_Completion, Attorney_Confirmation  FROM MailTeamTracker "
    'change oConn to reflect the Connection object you are using in your program
    oRS.Open strSQL, myConn
     
    'load the listview
    Do While Not oRS.EOF
       Set lvwItem = ListView1.ListItems.Add(, , Format(oRS.Fields.Item("Date_Entered").Value, "mm/dd/yyyy"))
       
       lvwItem.SubItems(1) = oRS.Fields.Item("Loan_Number").Value & ""
       lvwItem.SubItems(2) = oRS.Fields.Item("Investor").Value & ""
       lvwItem.SubItems(3) = oRS.Fields.Item("State").Value & ""
       lvwItem.SubItems(4) = oRS.Fields.Item("Doc_Type").Value & ""
       lvwItem.SubItems(5) = oRS.Fields.Item("Description").Value & ""
       lvwItem.SubItems(6) = oRS.Fields.Item("Number_Pages").Value & ""
       lvwItem.SubItems(7) = oRS.Fields.Item("Tracking_Number").Value & ""
       lvwItem.SubItems(8) = oRS.Fields.Item("Address").Value & ""
       lvwItem.SubItems(9) = oRS.Fields.Item("Processor").Value & ""
       lvwItem.SubItems(10) = oRS.Fields.Item("Reviewer1").Value & ""
       lvwItem.SubItems(11) = oRS.Fields.Item("Executor1").Value & ""
       lvwItem.SubItems(12) = oRS.Fields.Item("Reviewer2").Value & ""
       lvwItem.SubItems(13) = oRS.Fields.Item("Executor2").Value & ""
       lvwItem.SubItems(14) = oRS.Fields.Item("Notes").Value & ""
       lvwItem.SubItems(15) = oRS.Fields.Item("Attorney").Value & ""
       lvwItem.SubItems(16) = oRS.Fields.Item("PLX").Value & ""
       lvwItem.SubItems(17) = oRS.Fields.Item("Clarifire_Completion").Value & ""
       lvwItem.SubItems(18) = oRS.Fields.Item("Attorney_Confirmation").Value & ""
       
       
       oRS.MoveNext
    Loop
      
    oRS.Close
    Set oRS = Nothing
    
      End Sub
    and below is the code i would like to integrate (just a sample of one combobox)
    Code:
    Dim strSQL as String    'Declare the variables we need 
    Dim oRS as ADODB.Recordset
      Set oRS = New ADODB.Recordset
    
      strSQL = "SELECT Processor FROM Processor"
    
      oRS.Open strSQL, myConn, adOpenForwardOnly, adLockReadOnly, adCmdText
    
      With cboProcessor
        .Clear
        Do While Not oRS.EOF
          .AddItem oRS.fields("Processor").value
          oRS.MoveNext
        Loop
      End With
    
                               'Tidy up
      oRS.Close
      Set oRS = Nothing

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Add code to existing: Populate combobox from database

    Add this part right after your loop.

    Code:
    strSQL = "SELECT Processor FROM Processor"
      oRS.Open strSQL, myConn, adOpenForwardOnly, adLockReadOnly, adCmdText
      With cboProcessor
        .Clear
        Do While Not oRS.EOF
          .AddItem oRS.fields("Processor").value
          oRS.MoveNext
        Loop
      End With
    EDIT: PS--I have NOT checked any of the code for correctness... but once your connection is established, you can query any/all tables separately by using a new (or reusing an existing) recordset.
    Last edited by SamOscarBrown; Jun 18th, 2013 at 11:32 AM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2013
    Posts
    100

    Re: Add code to existing: Populate combobox from database

    Thanks Sam.

    I attempted to add the code after the loop prior to my initial post however i receive an error: "Operation not allowed when the object is open" and it highlights the

    Code:
      oRS.Open strSQL, myConn, adOpenForwardOnly, adLockReadOnly, adCmdText
    Thoughts?

    Code:
    Private Sub Form_Load()
    
    Dim myConn As ADODB.Connection
    
    Set myConn = New ADODB.Connection
    myConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\Business Intelligence\VB6\Database9.mdb;"
    myConn.Open
    
    Dim strSQL As String 'our query string
    Dim oRS As ADODB.Recordset 'our recordset object
    Dim lvwItem As ListItem 'this is necessary to directly reference the ListView control
    Set oRS = New ADODB.Recordset
     
    'change this SQL as appropriate for your needs
    strSQL = "SELECT Date_Entered, Loan_Number, Investor, State, Doc_type, Description, Number_Pages,Tracking_Number,Address, Processor, Reviewer1, executor1, Reviewer2, Executor2, Notes, Attorney, PLX, Clarifire_Completion, Attorney_Confirmation  FROM MailTeamTracker "
    'change oConn to reflect the Connection object you are using in your program
    oRS.Open strSQL, myConn
     
    'load the listview
    Do While Not oRS.EOF
       Set lvwItem = ListView1.ListItems.Add(, , Format(oRS.Fields.Item("Date_Entered").Value, "mm/dd/yyyy"))
       
       lvwItem.SubItems(1) = oRS.Fields.Item("Loan_Number").Value & ""
       lvwItem.SubItems(2) = oRS.Fields.Item("Investor").Value & ""
       lvwItem.SubItems(3) = oRS.Fields.Item("State").Value & ""
       lvwItem.SubItems(4) = oRS.Fields.Item("Doc_Type").Value & ""
       lvwItem.SubItems(5) = oRS.Fields.Item("Description").Value & ""
       lvwItem.SubItems(6) = oRS.Fields.Item("Number_Pages").Value & ""
       lvwItem.SubItems(7) = oRS.Fields.Item("Tracking_Number").Value & ""
       lvwItem.SubItems(8) = oRS.Fields.Item("Address").Value & ""
       lvwItem.SubItems(9) = oRS.Fields.Item("Processor").Value & ""
       lvwItem.SubItems(10) = oRS.Fields.Item("Reviewer1").Value & ""
       lvwItem.SubItems(11) = oRS.Fields.Item("Executor1").Value & ""
       lvwItem.SubItems(12) = oRS.Fields.Item("Reviewer2").Value & ""
       lvwItem.SubItems(13) = oRS.Fields.Item("Executor2").Value & ""
       lvwItem.SubItems(14) = oRS.Fields.Item("Notes").Value & ""
       lvwItem.SubItems(15) = oRS.Fields.Item("Attorney").Value & ""
       lvwItem.SubItems(16) = oRS.Fields.Item("PLX").Value & ""
       lvwItem.SubItems(17) = oRS.Fields.Item("Clarifire_Completion").Value & ""
       lvwItem.SubItems(18) = oRS.Fields.Item("Attorney_Confirmation").Value & ""
       
       
       oRS.MoveNext
    Loop
    
    strSQL = "SELECT Processor FROM Processor"
      oRS.Open strSQL, myConn, adOpenForwardOnly, adLockReadOnly, adCmdText
      With cboProcessor
        .Clear
        Do While Not oRS.EOF
          .AddItem oRS.Fields("Processor").Value
          oRS.MoveNext
        Loop
      End With
    
    oRS.Close
    Set oRS = Nothing
    
      End Sub
    Last edited by MWhiteDesigns; Jun 18th, 2013 at 12:15 PM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2013
    Posts
    100

    Re: Add code to existing: Populate combobox from database

    I changed it to the below and i no longer receive an error when launching the form. I checked my combo and even though i see values in the database, there are no values in the drop down.

    So i stepped through the code using F8, when i get to
    Code:
    .AddItem oRS.Fields("Processor").Value
    i get an error message again stating "Invalid use of Null". But once again, there are values in this column in the database.

    What am I doing wrong?

    Code:
    Private Sub Form_Load()
    
    Dim myConn As ADODB.Connection
    
    Set myConn = New ADODB.Connection
    myConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\Business Intelligence\VB6\Database9.mdb;"
    myConn.Open
    
    Dim strSQL As String 'our query string
    Dim oRS As ADODB.Recordset 'our recordset object
    Dim pRS As ADODB.Recordset 'our recordset object
    Dim lvwItem As ListItem 'this is necessary to directly reference the ListView control
    Set oRS = New ADODB.Recordset
    Set pRS = New ADODB.Recordset
     
    'change this SQL as appropriate for your needs
    strSQL = "SELECT Date_Entered, Loan_Number, Investor, State, Doc_type, Description, Number_Pages,Tracking_Number,Address, Processor, Reviewer1, executor1, Reviewer2, Executor2, Notes, Attorney, PLX, Clarifire_Completion, Attorney_Confirmation  FROM MailTeamTracker "
    'change oConn to reflect the Connection object you are using in your program
    oRS.Open strSQL, myConn
     
    'load the listview
    Do While Not oRS.EOF
       Set lvwItem = ListView1.ListItems.Add(, , Format(oRS.Fields.Item("Date_Entered").Value, "mm/dd/yyyy"))
       
       lvwItem.SubItems(1) = oRS.Fields.Item("Loan_Number").Value & ""
       lvwItem.SubItems(2) = oRS.Fields.Item("Investor").Value & ""
       lvwItem.SubItems(3) = oRS.Fields.Item("State").Value & ""
       lvwItem.SubItems(4) = oRS.Fields.Item("Doc_Type").Value & ""
       lvwItem.SubItems(5) = oRS.Fields.Item("Description").Value & ""
       lvwItem.SubItems(6) = oRS.Fields.Item("Number_Pages").Value & ""
       lvwItem.SubItems(7) = oRS.Fields.Item("Tracking_Number").Value & ""
       lvwItem.SubItems(8) = oRS.Fields.Item("Address").Value & ""
       lvwItem.SubItems(9) = oRS.Fields.Item("Processor").Value & ""
       lvwItem.SubItems(10) = oRS.Fields.Item("Reviewer1").Value & ""
       lvwItem.SubItems(11) = oRS.Fields.Item("Executor1").Value & ""
       lvwItem.SubItems(12) = oRS.Fields.Item("Reviewer2").Value & ""
       lvwItem.SubItems(13) = oRS.Fields.Item("Executor2").Value & ""
       lvwItem.SubItems(14) = oRS.Fields.Item("Notes").Value & ""
       lvwItem.SubItems(15) = oRS.Fields.Item("Attorney").Value & ""
       lvwItem.SubItems(16) = oRS.Fields.Item("PLX").Value & ""
       lvwItem.SubItems(17) = oRS.Fields.Item("Clarifire_Completion").Value & ""
       lvwItem.SubItems(18) = oRS.Fields.Item("Attorney_Confirmation").Value & ""
       
       
       oRS.MoveNext
    Loop
    
    strSQL = "SELECT Processor FROM DropDowns"
    
      With cboProcessor
        .Clear
        Do While Not oRS.EOF
          .AddItem oRS.Fields("Processor").Value
          oRS.MoveNext
        Loop
      End With
    
        oRS.Close
        Set oRS = Nothing
    
      End Sub

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Add code to existing: Populate combobox from database

    Yes, sorry...close that recordset first (or declare and open a second one)...first option is the best.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2013
    Posts
    100

    Re: Add code to existing: Populate combobox from database

    Errr..

    I feel like I am losing brain cells!

    Thank you again Sam.

    So i placed the code after the oRS is closed; however i receive an error message: "object various or with block variable not set" and once again it highlights the below
    Code:
    oRS.Open strSQL, myConn, adOpenForwardOnly, adLockReadOnly, adCmdText
    below is the code now.

    Code:
    Private Sub Form_Load()
    
    Dim myConn As ADODB.Connection
    
    Set myConn = New ADODB.Connection
    myConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\Business Intelligence\VB6\Database9.mdb;"
    myConn.Open
    
    Dim strSQL As String 'our query string
    Dim oRS As ADODB.Recordset 'our recordset object
    
    Dim lvwItem As ListItem 'this is necessary to directly reference the ListView control
    Set oRS = New ADODB.Recordset
    
     
    'change this SQL as appropriate for your needs
    strSQL = "SELECT Date_Entered, Loan_Number, Investor, State, Doc_type, Description, Number_Pages,Tracking_Number,Address, Processor, Reviewer1, executor1, Reviewer2, Executor2, Notes, Attorney, PLX, Clarifire_Completion, Attorney_Confirmation  FROM MailTeamTracker "
    'change oConn to reflect the Connection object you are using in your program
    oRS.Open strSQL, myConn
     
    'load the listview
    Do While Not oRS.EOF
       Set lvwItem = ListView1.ListItems.Add(, , Format(oRS.Fields.Item("Date_Entered").Value, "mm/dd/yyyy"))
       
       lvwItem.SubItems(1) = oRS.Fields.Item("Loan_Number").Value & ""
       lvwItem.SubItems(2) = oRS.Fields.Item("Investor").Value & ""
       lvwItem.SubItems(3) = oRS.Fields.Item("State").Value & ""
       lvwItem.SubItems(4) = oRS.Fields.Item("Doc_Type").Value & ""
       lvwItem.SubItems(5) = oRS.Fields.Item("Description").Value & ""
       lvwItem.SubItems(6) = oRS.Fields.Item("Number_Pages").Value & ""
       lvwItem.SubItems(7) = oRS.Fields.Item("Tracking_Number").Value & ""
       lvwItem.SubItems(8) = oRS.Fields.Item("Address").Value & ""
       lvwItem.SubItems(9) = oRS.Fields.Item("Processor").Value & ""
       lvwItem.SubItems(10) = oRS.Fields.Item("Reviewer1").Value & ""
       lvwItem.SubItems(11) = oRS.Fields.Item("Executor1").Value & ""
       lvwItem.SubItems(12) = oRS.Fields.Item("Reviewer2").Value & ""
       lvwItem.SubItems(13) = oRS.Fields.Item("Executor2").Value & ""
       lvwItem.SubItems(14) = oRS.Fields.Item("Notes").Value & ""
       lvwItem.SubItems(15) = oRS.Fields.Item("Attorney").Value & ""
       lvwItem.SubItems(16) = oRS.Fields.Item("PLX").Value & ""
       lvwItem.SubItems(17) = oRS.Fields.Item("Clarifire_Completion").Value & ""
       lvwItem.SubItems(18) = oRS.Fields.Item("Attorney_Confirmation").Value & ""
        
       oRS.MoveNext
    Loop
    
        oRS.Close
        Set oRS = Nothing
        
    strSQL = "SELECT Processor FROM DropDowns"
      oRS.Open strSQL, myConn, adOpenForwardOnly, adLockReadOnly, adCmdText
      With cboProcessor
        .Clear
        Do While Not oRS.EOF
          .AddItem oRS.Fields("Processor").Value
          oRS.MoveNext
        Loop
      End With
    
        oRS.Close
        Set oRS = Nothing
    
      End Sub
    What am i doing wrong now???

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Add code to existing: Populate combobox from database

    First things first....in post #4 you are having the NULL issue
    do this :

    Code:
    If not IsNull(oRS.Fields("Processor").value then
       .AddItem oRS.Fields("Processor").Value
    End if
    I'll look at your other issue in a moment

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2013
    Posts
    100

    Re: Add code to existing: Populate combobox from database

    Thank you Sam.

    I am an extremely visual learner so seeing examples of what I did wrong or rather what I need to include helps alot.

    However I created another record set and it seems to work this way. But I would like to see how closing and opening works as well, as you stated it would a be better option.

    Code:
    Private Sub Form_Load()
    
    Dim myConn As ADODB.Connection
    
    Set myConn = New ADODB.Connection
    myConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\Business Intelligence\VB6\Database9.mdb;"
    myConn.Open
    
    Dim strSQL As String 'our query string
    Dim oRS As ADODB.Recordset 'our recordset object
    Dim ddRS As ADODB.Recordset 'our recordset object
    
    Dim lvwItem As ListItem 'this is necessary to directly reference the ListView control
    Set oRS = New ADODB.Recordset
    Set ddRS = New ADODB.Recordset
    
     
    'change this SQL as appropriate for your needs
    strSQL = "SELECT Date_Entered, Loan_Number, Investor, State, Doc_type, Description, Number_Pages,Tracking_Number,Address, Processor, Reviewer1, executor1, Reviewer2, Executor2, Notes, Attorney, PLX, Clarifire_Completion, Attorney_Confirmation  FROM MailTeamTracker "
    'change oConn to reflect the Connection object you are using in your program
    oRS.Open strSQL, myConn
     
    'load the listview
    Do While Not oRS.EOF
       Set lvwItem = ListView1.ListItems.Add(, , Format(oRS.Fields.Item("Date_Entered").Value, "mm/dd/yyyy"))
       
       lvwItem.SubItems(1) = oRS.Fields.Item("Loan_Number").Value & ""
       lvwItem.SubItems(2) = oRS.Fields.Item("Investor").Value & ""
       lvwItem.SubItems(3) = oRS.Fields.Item("State").Value & ""
       lvwItem.SubItems(4) = oRS.Fields.Item("Doc_Type").Value & ""
       lvwItem.SubItems(5) = oRS.Fields.Item("Description").Value & ""
       lvwItem.SubItems(6) = oRS.Fields.Item("Number_Pages").Value & ""
       lvwItem.SubItems(7) = oRS.Fields.Item("Tracking_Number").Value & ""
       lvwItem.SubItems(8) = oRS.Fields.Item("Address").Value & ""
       lvwItem.SubItems(9) = oRS.Fields.Item("Processor").Value & ""
       lvwItem.SubItems(10) = oRS.Fields.Item("Reviewer1").Value & ""
       lvwItem.SubItems(11) = oRS.Fields.Item("Executor1").Value & ""
       lvwItem.SubItems(12) = oRS.Fields.Item("Reviewer2").Value & ""
       lvwItem.SubItems(13) = oRS.Fields.Item("Executor2").Value & ""
       lvwItem.SubItems(14) = oRS.Fields.Item("Notes").Value & ""
       lvwItem.SubItems(15) = oRS.Fields.Item("Attorney").Value & ""
       lvwItem.SubItems(16) = oRS.Fields.Item("PLX").Value & ""
       lvwItem.SubItems(17) = oRS.Fields.Item("Clarifire_Completion").Value & ""
       lvwItem.SubItems(18) = oRS.Fields.Item("Attorney_Confirmation").Value & ""
        
       oRS.MoveNext
    Loop
    
        oRS.Close
        Set oRS = Nothing
        
    strSQL = "SELECT Processor FROM DropDowns"
      ddRS.Open strSQL, myConn, adOpenForwardOnly, adLockReadOnly, adCmdText
      With cboProcessor
        .Clear
        Do While Not ddRS.EOF
            If Not IsNull(ddRS.Fields("Processor").Value) Then
            .AddItem ddRS.Fields("Processor").Value
            End If
          ddRS.MoveNext
        Loop
      End With
    
        ddRS.Close
        Set ddRS = Nothing
    
      End Sub
    Last edited by MWhiteDesigns; Jun 18th, 2013 at 02:23 PM.

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Add code to existing: Populate combobox from database

    The other issue?
    Delete this line before reopening oRS:

    Set oRS = Nothing

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2013
    Posts
    100

    Re: Add code to existing: Populate combobox from database

    Quote Originally Posted by SamOscarBrown View Post
    The other issue?
    Delete this line before reopening oRS:

    Set oRS = Nothing
    Excellent!!! Works like a charm. Thank you.

    Lastly, what If i want to populate another cbo box? I added the below but it still only pulls in the first command for "Processor"

    Code:
    strSQL = "SELECT Processor, Investor FROM DropDowns"
      oRS.Open strSQL, myConn, adOpenForwardOnly, adLockReadOnly, adCmdText
      
    With cboProcessor
        .Clear
        Do While Not oRS.EOF
            If Not IsNull(oRS.Fields("Processor").Value) Then
            .AddItem oRS.Fields("Processor").Value
            End If
          oRS.MoveNext
        Loop
      End With
    
    With cboInvestor
        .Clear
        Do While Not oRS.EOF
            If Not IsNull(oRS.Fields("Investor").Value) Then
            .AddItem oRS.Fields("Investor").Value
            End If
          oRS.MoveNext
        Loop
      End With 
    
        oRS.Close
        Set ddRS = Nothing

  11. #11
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Add code to existing: Populate combobox from database

    That is because you have already moved through your recordset (oRS.Movenext) in your first loop (you have already reached the EOF).
    Simply put a "oRS.MoveFirst" line before "With cboInvestor"

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2013
    Posts
    100

    Re: Add code to existing: Populate combobox from database

    Ahh! Viola! Thank you!!

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2013
    Posts
    100

    Re: Add code to existing: Populate combobox from database

    Alrighty. Last question on this topic althought slightly different ( I hope).

    Form loads great. However I have a command button that transfers information from the listview into various fields (cbo boxes, text boxes etc).
    If i set the combo's to '0-drop down combo' it works, as it should.

    But i would technically like it to be '2- drop down list'
    After the change when it runs, if a field i load into is blank, I receive a read only error. Is there anyway around this using the below?

    Code:
    Private Sub cmdSelect_Click()
    cboInvestor.Text = ListView1.SelectedItem.SubItems(2)
    end sub
    Last edited by MWhiteDesigns; Jun 18th, 2013 at 03:48 PM.

  14. #14
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Add code to existing: Populate combobox from database

    Are you attempting to add the item selected from the ListView into the ComboBox, or is it there already and you want to select it ?
    This little Function will search the Combo for a given item and if not found, add it to the Items
    Code:
    Private Function FindOrAddItem(cmb As ComboBox, strToFind As String) As Long
    Dim lngI As Long
    FindOrAddItem = -1
    '
    ' Loop through the ComboBox items checking for
    ' the item were looking for
    ' if found, set the return value to the Item number
    '
    Do
        If UCase(cmb.List(lngI)) = UCase(strToFind) Then
            FindOrAddItem = lngI
        Else
            lngI = lngI + 1
        End If
    Loop Until FindOrAddItem >= 0 Or lngI > cmb.ListCount
    If FindOrAddItem < 0 Then
        '
        ' Didn't find the Item, add it to the ComboBox
        ' and return it's item number
        '
        cmb.AddItem strToFind
        FindOrAddItem = cmb.NewIndex
    End If
    End Function
    You could use it thus
    Code:
    Private Sub cmdSelect_Click()
    cboInvestor.ListIndex = FindOrAddItem(cboInvestor, ListView1.SelectedItem.SubItems(2))
    End Sub
    Which will set the .Text property of the ComboBox to the item selected from the ListView. Note that this code will cause the cboInvestor_Click event to be triggered so if you have code in that event you should take it into account.
    Last edited by Doogle; Jun 19th, 2013 at 01:30 AM.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jun 2013
    Posts
    100

    Re: Add code to existing: Populate combobox from database

    Good morning doogle,
    I am attempting to take the listview items that were loaded, like in these examples (there are more, just wanted to provide a smaller example):

    Code:
     lvwItem.SubItems(1) = oRS.Fields.Item("Loan_Number").Value & ""
       lvwItem.SubItems(2) = oRS.Fields.Item("Investor").Value & ""
       lvwItem.SubItems(3) = oRS.Fields.Item("State").Value & ""
       lvwItem.SubItems(4) = oRS.Fields.Item("Doc_Type").Value & ""
    And place them in textboxes or combo boxes. The user will select the row, and click a command button that runs the below.

    I am using the below
    Code:
    Private Sub cmdSelect_Click()
    txtLoanNumber.Text = ListView1.SelectedItem.SubItems(1)
    cboInvestor.Text = ListView1.SelectedItem.SubItems(2)
    cboState.Text = ListView1.SelectedItem.SubItems(3)
    cboDocType.Text = ListView1.SelectedItem.SubItems(4)
    end sub
    However if the combo boxes (cbo) are set to 2-Drop down list, AND the field is blank on the database, i receive the read only error. However If i change it to 0-drop down combo. I would like to keep it as 2-drop down list and create a workaround if blank. But if that's not possible i understand.

    I attempted the above and I still receive the same error.

    Thoughts?
    Last edited by MWhiteDesigns; Jun 19th, 2013 at 07:26 AM.

  16. #16
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Add code to existing: Populate combobox from database

    Quote Originally Posted by MWhiteDesigns View Post
    However if the combo boxes (cbo) are set to 2-Drop down list, AND the field is blank on the database, i receive the read only error. However If i change it to 0-drop down combo. I would like to keep it as 2-drop down list and create a workaround if blank. But if that's not possible i understand.
    What do you mean "the field is blank on the database"? Are you saying that when you 'loaded' your listview from the DB there are some blanks that occur in your listview? If so, when you do your query, simply don't return blank values.

    It appears your cmdSelect click event should work fine regardless if the cbo is 0 or 2. And it should work regardless if the item in the listview is a blank or not. Guess I need a bit more info/explanation.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jun 2013
    Posts
    100

    Re: Add code to existing: Populate combobox from database

    Hi Sam,

    By blank I mean, if you look at the database, there are no values in that particular column for that record.

    How would i go about not returning blanks?

  18. #18
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Add code to existing: Populate combobox from database

    Remember this code?
    Code:
    With cboProcessor
        .Clear
        Do While Not oRS.EOF
            If Not IsNull(oRS.Fields("Processor").Value) Then
            .AddItem oRS.Fields("Processor").Value
            End If
          oRS.MoveNext
        Loop
      End With
    You can rewrite as:
    Code:
    With cboProcessor
        .Clear
        Do While Not oRS.EOF
            If Not IsNull(oRS.Fields("Processor").Value) or len(trim(oRS.Fields("Processor").value)) <> 0 Then
            .AddItem oRS.Fields("Processor").Value
            End If
          oRS.MoveNext
        Loop
      End With
    or..:
    Code:
    With cboProcessor
        .Clear
        Do While Not oRS.EOF
            If Not IsNull(oRS.Fields("Processor").Value) then 
                 If Len(trim(oRS.Fields("Processor").value)) <> 0 Then
                     .AddItem oRS.Fields("Processor").Value
                 End If
            End If
          oRS.MoveNext
        Loop
      End With
    Do the same for the other cbos you have....IOW, you are going to populate your comboboxes only if the returned string value is not NULL (EMPTY) or if the length of that string is not zero.

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Jun 2013
    Posts
    100

    Re: Add code to existing: Populate combobox from database

    Thank you Sam for the help. Much appreciated.

  20. #20
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Add code to existing: Populate combobox from database

    No prob....you can rate my post(s) if desired. (Little star by my post)
    Also, if you are done with this specific topic, please mark this thread as RESOLVED (Under Thread Tools).

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