+ Reply to Thread
Results 1 to 6 of 6

code for updating rows based on a userform combobox selection

Hybrid View

  1. #1
    Registered User
    Join Date
    08-19-2013
    Location
    romania
    MS-Off Ver
    Excel 2007
    Posts
    3

    code for updating rows based on a userform combobox selection

    Hi,
    First of all I appologise if I didn't post this problem correctly, but I'm new to this forum, as well as to excel programming...
    I've created two Excel VBA forms: the first one pops up when the Workbook Opens and gives the possibility to:
    (1) enter a new project, and in this case Page1 from a Multipage of the second form is shown
    and
    (2) update an existing project, wich shows Page2 from the Multipage of the second form.
    Page2 of the second form contains a ComboBox that is populated with the projects allready entered. When a project is selected, data is retreived into the comboboxes and textboxes of the second form (on both pages), permitting the user to update fields.
    My problem resides in the fact that I can't get the form to update an existing record based on the Combobox selection. It only inserts new records, no matter what I do...I've tried various solutions found on the internet, but this being my first vba project, I'm a little overwhelmed by the situation...

    I've attached my workbook, where I've changed the captions of the various objects of the forms.

    Thank you in advance for your time and help!
    Attached Files Attached Files
    Last edited by raluk_ro22; 08-27-2013 at 07:53 AM.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: code for updating rows based on a userform combobox selection

    Hi raluk_ro22

    Welcome to the Forum!

    Are the Project ID's Unique...will the SAME Project ID EVER appear more than once in Column B?
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    08-19-2013
    Location
    romania
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: code for updating rows based on a userform combobox selection

    Hi jaslake,
    Thank you!
    The Project ID's are NOT unique. A project id may appear more than once in column B, depending on the Department that works on the project.

    Maybe a glimpse of the code may help:
    '---------------------------
    ' CODE FOR THE Submit BUTTON
    '---------------------------
    Private Sub cmbValidare_Click()
        
    ' CODE FOR ENTERING NEW DATA (NEW PROJECT)
        If (Me.tbEndDate >= Me.tbStDate) And _
        (Me.cboFinalizat <> "" Or Me.cboFinalizat <> "Select") And _
        frmWelcome.obActualizare.Value = False Then
        
            Dim Ctrl As Control
            Dim r As Integer
            Dim t As Integer
            Dim nextline As Integer
            
            With Worksheets("CENTRALIZATOR")
                nextline = .Range("A65536").End(xlUp).Row + 1
                
                For Each Ctrl In frmCentralizator.Controls
                    r = Val(Ctrl.Tag)
                    If r > 0 Then
                        sCentralizator.Cells(nextline, r) = Ctrl
                    End If
                Next
            End With
            'CLEAR THE DATA
            Me.cboSubdep.Value = "Select"       ' Department
            Me.tbCodPr.Value = ""              ' Project id
            Me.tbNumePr.Value = ""             ' Project name
            Me.cboTarget.Value = "Select"       ' Target
            Me.cboMethod1.Value = "Select"      ' Method 1
            Me.cboMethod2.Value = "Select"      ' Method 2
            Me.cboLocation.Value = "Select"     ' Location
            Me.cboAngajat.Value = "Select"      ' Employee name
            Me.cboCercetator.Value = "Select"   ' Person name
            Me.cboCoordonator.Value = "Select"  ' Coordinator name
            Me.tbStDate.Value = ""             ' Start date
            Me.tbTiming.Value = ""             ' Timing
            
            Me.tbEndDate.Value = ""            ' End date
            Me.cboComplexitate.Value = "Select" ' Complexity
            Me.tbCosturiOre.Value = ""         ' Hours allocated
            Me.tbModuloAttDP.Value = ""        ' Hours allocated 2
            Me.tbOreLucrate.Value = ""         ' Hours worked
            Me.tbDiferenta.Value = ""          ' Difference
            Me.cboQuality.Value = "Select"      ' Quality
            Me.tbComentarii.Value = ""         ' Comments
            Me.cboSubdep2.Value = "Select"      ' Department 2
            Me.cboFinalizat.Value = "Select"    ' End project
            Me.cboFeedback.Value = "Select"     ' Received feedback
    
    ' CODE FOR UPDATING DATA
        ElseIf frmWelcome.obActualizare.Value = True Then
        '----------------------------------------
        ' THIS IS THE PART THAT I NEED HELP WITH
        '----------------------------------------
        End If
        
            Application.Visible = True
            frmCentralizator.Hide
            sCentralizator.Visible = True
            sCentralizator.Select  
        End
    ending:
    End Sub
    
    '-----------------------------------------------------------------------------------------
    ' CODE FOR THE cboSearchPr COMBOBOX (Search project for update) WHEN A PROJECT IS SELECTED
    '-----------------------------------------------------------------------------------------
    Private Sub cboSearchPr_Change()
    
        Dim c
        Dim cboSearchPr As String
        Dim Label15, Label16, Label17, Label18, Label19, Label20, Label21, Label22, Label23, Label24, Label25, Label26 As String
        Dim MyRange As Range
        Set MyRange = Worksheets("CENTRALIZATOR").Range("CentralizatorList")
    
        With MyRange
            Set c = .Find(Me.cboSearchPr.Value, LookIn:=xlValues)
            
            If Not c Is Nothing Then
            
                firstAddress = c.Address
                Me.Label15 = c.Offset(0, -1).Value
                Me.Label16 = c.Offset(0, 0).Value
                Me.Label17 = c.Offset(0, 1).Value
                Me.Label18 = c.Offset(0, 2).Value
                Me.Label19 = c.Offset(0, 3).Value
                Me.Label20 = c.Offset(0, 4).Value
                Me.Label21 = c.Offset(0, 5).Value
                Me.Label22 = c.Offset(0, 6).Value
                Me.Label23 = c.Offset(0, 7).Value
                Me.Label24 = c.Offset(0, 8).Value
                Me.Label25 = c.Offset(0, 9).Value
                Me.Label26 = c.Offset(0, 10).Value
    
                Me.cboSubdep.Value = c.Offset(0, -1).Value
                Me.tbCodPr.Value = c.Offset(0, 0).Value
                Me.tbNumePr.Value = c.Offset(0, 1).Value
                Me.cboTarget.Value = c.Offset(0, 2).Value
                Me.cboMethod1.Value = c.Offset(0, 3).Value
                Me.cboMethod2.Value = c.Offset(0, 4).Value
                Me.cboLocation.Value = c.Offset(0, 5).Value
                Me.cboAngajat.Value = c.Offset(0, 6).Value
                Me.cboCercetator.Value = c.Offset(0, 7).Value
                Me.cboCoordonator.Value = c.Offset(0, 8).Value
                Me.tbStDate.Value = c.Offset(0, 9).Value
                Me.tbTiming.Value = c.Offset(0, 10).Value
    
                Me.tbEndDate.Value = c.Offset(0, 11).Value
                Me.cboComplexitate.Value = c.Offset(0, 12).Value
                Me.tbCosturiOre.Value = c.Offset(0, 13).Value
                Me.tbModuloAttDP.Value = c.Offset(0, 14).Value
                Me.tbOreLucrate.Value = c.Offset(0, 15).Value
                'Me.tbDiferenta.Value = c.Offset(0, 16).Value
                Me.cboQuality.Value = c.Offset(0, 17).Value
                Me.tbComentarii.Value = c.Offset(0, 18).Value
                Me.cboSubdep2.Value = c.Offset(0, 19).Value
                Me.cboFinalizat.Value = c.Offset(0, 20).Value
                Me.cboFeedback.Value = c.Offset(0, 21).Value
                
            End If
        End With
    End Sub
    
    '-------------------------------------------------------------------------
    ' CODE FOR POPULATING THE cboSearchPr COMBOBOX (Search project for update)
    '-------------------------------------------------------------------------
    Private Sub UserForm_Initialize()
    
        Me.MultiPage1.Value = iPage
        Me.MultiPage1.Style = fmTabStyleNone
        
        If Me.MultiPage1.Value = 0 Then
            
            Me.lblSearchPr.Visible = False
            Me.cboSearchPr.Visible = False
            
            Me.Frame1.Visible = False
            Me.Label15.Visible = False
            Me.Label16.Visible = False
            Me.Label17.Visible = False
            Me.Label18.Visible = False
            Me.Label19.Visible = False
            Me.Label20.Visible = False
            Me.Label21.Visible = False
            Me.Label22.Visible = False
            Me.Label23.Visible = False
            Me.Label24.Visible = False
            Me.Label25.Visible = False
            Me.Label26.Visible = False
        Else
    
            Me.cboSearchPr.List = Worksheets("CENTRALIZATOR").Range("CentralizatorList").Value
            Me.cboSearchPr.RowSource = Worksheets("CENTRALIZATOR").Range("CentralizatorList").Address(external:=True)
    
            Me.lblSearchPr.Visible = True
            Me.cboSearchPr.Visible = True
            
            Me.Frame1.Visible = True
            Me.Label15.Visible = True
            Me.Label16.Visible = True
            Me.Label17.Visible = True
            Me.Label18.Visible = True
            Me.Label19.Visible = True
            Me.Label20.Visible = True
            Me.Label21.Visible = True
            Me.Label22.Visible = True
            Me.Label23.Visible = True
            Me.Label24.Visible = True
            Me.Label25.Visible = True
            Me.Label26.Visible = True
        End If
    End Sub
    Last edited by raluk_ro22; 08-26-2013 at 05:04 AM.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: code for updating rows based on a userform combobox selection

    Hi raluk_ro22

    The Code in the attached has been modified for this
    I can't get the form to update an existing record based on the Combobox selection
    Please note, I've done nothing with the Cell Colors as I have no idea what you're doing or why. Beside that...I'm color blind
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    08-19-2013
    Location
    romania
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: code for updating rows based on a userform combobox selection

    Hi jaslake,
    Sorry for responding so late... Thank you so much for your help! The code you provided was the answer to my problem. I would never have thought of that solution by myself.
    And don't worry for the Cell Colors I'll work on those later...

    Thank you again!!

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: code for updating rows based on a userform combobox selection

    You're welcome...glad I could help. Thanks for the Rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Change Userform Label based on ComboBox Selection
    By rbyrd023 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-02-2014, 09:25 AM
  2. [SOLVED] Pre populating a userform from excel based on combobox selection
    By michelle 1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2013, 12:09 PM
  3. [SOLVED] Change Labels in Userform based on selection in ComboBox
    By johnw993 in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 01-03-2013, 10:52 PM
  4. [SOLVED] How VB code can unhide/hide rows based on combobox selection?
    By jgomez in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-04-2012, 01:25 PM
  5. Populating a userform based on a combobox selection
    By blindzero678 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-22-2010, 03:37 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1