+ Reply to Thread
Results 1 to 9 of 9

Link text Box to Combobox in Userform and autoupdate database

Hybrid View

abhiD Link text Box to Combobox in... 12-27-2011, 03:10 AM
abhiD Re: Link text Box to Combobox... 12-29-2011, 03:24 AM
john55 Re: Link text Box to Combobox... 12-29-2011, 06:49 AM
ASDFG Re: Link text Box to Combobox... 01-12-2012, 11:50 AM
john55 Re: Link text Box to Combobox... 01-20-2012, 04:54 AM
abhiD Re: Link text Box to Combobox... 01-20-2012, 05:29 AM
john55 Re: Link text Box to Combobox... 01-20-2012, 05:46 AM
abhiD Re: Link text Box to Combobox... 01-20-2012, 06:47 AM
john55 Re: Link text Box to Combobox... 01-20-2012, 06:58 AM
  1. #1
    Registered User
    Join Date
    12-26-2011
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2007
    Posts
    26

    Lightbulb Link text Box to Combobox in Userform and autoupdate database

    Hi,

    I am pretty new to VBA programming. I am new to programming of any kind all together. However, I am required to prepare an excel userform in which a user can select a value from the drop down combobox and the text box is linked to the corresponding column. The combobox gets its value from a database in the same sheet. Further if the value is not present, user can type the value in both the boxes. I have tried to do it myself, seeking help from various forums but m not able to do it.

    Please Help me with this.

    P.S. would it be possible to add the value in the data base automatically, if it is typed by the user.



    Private Sub cmdclose_Click()
    Unload Me
    End Sub
    
            Private Sub cmdAdd_Click()
            Dim lRow As Long
            Dim lmeter As Long
            Dim ws As Worksheet
            Set ws = Worksheets("sheet1")
            lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            lmeter = Me.cmboMeter.ListIndex
            
            If Trim(Me.cmboMeter.Value) = "" Then
            Me.cmboMeter.SetFocus
            MsgBox "please eneter a value"
            End If
            Exit Sub
            
            If Trim(Me.cmboMeter.Value) = "other.." Then
            Me.txtRef_1.SetFocus
            MsgBox "please enter a reference number"
            Exit Sub
            End If
            
             With ws
             .Cells(lRow, 1).Value = Me.cmboMeter.Value
             .Cells(lRow, 2).Value = Me.txtRef_1.Value
             .Cells(lRow, 3).Value = Me.units.Value
             End With
             Me.cmboMeter.Value = ""
             Me.txtRef_1.Value = ""
             Me.units.Value = ""
             Me.cmboMeter.SetFocus
            End Sub
    
    
    
    
    
    Private Sub UserForm_Initialize()
    Dim cmeter As Range
    Dim ws As Worksheet
    Set ws = Worksheets("sheet4")
    Me.MultiPage1.Value = 0
    
    
    For Each cmeter In ws.Range("meter1")
    With Me.cmboMeter
    .AddItem cmeter.Value
    .List(.ListCount - 1, 1) = cmeter.Offset(0, 1).Value
    End With
    Next cmeter
    
    
    End Sub
    
    Private Sub txtRef_1_AfterUpdate()
    
    Me![txtRef_1] = Me!cmboMeter.Column(1)
    End Sub
    Thanks.
    Attached Files Attached Files
    Last edited by pike; 01-21-2012 at 03:59 AM. Reason: request by john55 for solved prefix

  2. #2
    Registered User
    Join Date
    12-26-2011
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Link text Box to Combobox in Userform and autoupdate database

    Bump no response

  3. #3
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,060

    Re: Link text Box to Combobox in Userform and autoupdate database

    hi abhiD,
    as an option try this and see if it helps you!
    Private Sub cmboMeter_Change()
    Dim myName As String, myRange As Range
       
       myName = Me.cmboMeter.Text
       Set myRange = ThisWorkbook.Sheets("sheet4").Range("a:a")
       Set found = myRange.Find(myName, LookIn:=xlValues)
       If Not found Is Nothing Then
          Me.txtRef_1 = found.Offset(, 1)
          Me.units = found.Offset(, 2)
       Else
          Me.txtRef_1 = ""
          Me.units = ""
       End If
    End Sub
    Private Sub cmdAdd_Click()
    Application.ScreenUpdating = False
         
    'switch off screen updating to speed up code and prevent screen flickering
      
        
        Dim NextRw As Long
        With Sheets("Sheet4")
            'find next empty row using Column A
            NextRw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
    Dim Wks As Worksheet
        
            Set Wks = ActiveSheet
    
            'input the textbox entries
            .Cells(NextRw, 1).Value = Me.cmboMeter.Text
            .Cells(NextRw, 2).Value = Me.txtRef_1.Value
            .Cells(NextRw, 3).Value = Me.units.Value
            End With
            
             Me.cmboMeter.Value = ""
             Me.txtRef_1.Value = ""
             Me.units.Value = ""
             Me.cmboMeter.SetFocus
             Application.ScreenUpdating = True
    
            End Sub
    Private Sub UserForm_Initialize()
    Dim rList As Range
            With Sheets("Sheet4")
            Set rList = .Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp))
        End With
         Me.cmboMeter.List = rList.Value
       
    End Sub
    Attached Files Attached Files
    Regards, John55
    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.

    ...enjoy -funny parrots-

  4. #4
    Registered User
    Join Date
    01-03-2012
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Link text Box to Combobox in Userform and autoupdate database

    What if I just wanted to update a selection from the combo box instead of adding a new selection?

  5. #5
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,060

    Re: Link text Box to Combobox in Userform and autoupdate database

    hi abhiD,
    thank you for the rep! hope it helped you!
    If your problem is solved, please say so clearly, and mark your thread as Solved:
    Click the Edit button on your first post in the thread,
    Click Go Advanced, select [SOLVED] from the Prefix dropdown, then
    click Save Changes.

  6. #6
    Registered User
    Join Date
    12-26-2011
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Link text Box to Combobox in Userform and autoupdate database

    @john55

    thanks for the advise. The first part of the problem is indeed solved, but the second part, which is that if the part is not listed in the list a user can enter for the same in the boxes and then add that part to the master data base. in a sense the form has to work both the ways to pull values from the database and add the value there as well. I tried giving it a shot using check box, in which user will check the box in case the value is not listed in the combo box, and on clicking add button the value from the combo box and refrence no will be added to specifed columns in the master data base.

    But again, i have hit a road block..

  7. #7
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,060

    Re: Link text Box to Combobox in Userform and autoupdate database

    abhid, did you try to add something (what you did not find in yr combobox) and then click ok button?!
    I tested it with "123", which is not listed, and clicked ok, "123" and the "aaa", "bbb" (for both textboxes) are sent to yr sheet!
    or...did I miss something?

  8. #8
    Registered User
    Join Date
    12-26-2011
    Location
    Bangalore,India
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Link text Box to Combobox in Userform and autoupdate database

    @john

    Ya.. i checked it again, its working, i was trying to be overtly smart by using a check box..!:P
    thanks for the help man..!

    and i am not able to edit the first post hence cannot make it solved. I dunno why is that happening. Will mail the forum regulators regarding this..!
    thanks newes.
    Last edited by abhiD; 01-20-2012 at 06:52 AM. Reason: cannot change status to solved

  9. #9
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,060

    Re: Link text Box to Combobox in Userform and autoupdate database

    you're welcome!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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