Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-04-2016, 01:46 AM
highrise955 highrise955 is offline UserForm Dropdown List Not Populating Windows 10 UserForm Dropdown List Not Populating Office 2013
Advanced Beginner
UserForm Dropdown List Not Populating
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default UserForm Dropdown List Not Populating

I have a userform called "DrawingNumberEntryForm" with a ComboBox control called "DrawingNumberUf". I am trying to populate the control with a list from an Excel sheet.



Here is my code...

Code:
Private Sub DrawingNumberEntryForm_Initialize()

Dim varData As Variant
Dim lngIndex As Long
Dim oCC As ContentControl
  varData = LoadFromExcel_ADODB("c:\temp\ItemSheet.xlsx", "Item Sheet")
  Set oCC = .SelectContentControlsByTitle("DrawingNumberUf").Item(1)
  'Set oCC = ActiveDocument.SelectContentControlsByTag("Tag").Item(1)
  With oCC
    For lngIndex = .DropdownListEntries.Count To 1 Step -1
      .DropdownListEntries(lngIndex).Delete
    Next lngIndex
    For lngIndex = 0 To UBound(varData, 2)
      .DropdownListEntries.Add varData(0, lngIndex), varData(1, lngIndex)
    Next lngIndex
  End With
lbl_Exit:
  Exit Sub

End Sub


Function LoadFromExcel_ADODB(ByRef strSource As String, _
                              strRange As String, Optional bIsSheet As Boolean = True, _
                              Optional bSuppressHeadingRow As Boolean = True)
Dim oConn As Object
Dim oRecSet As Object
Dim strConnection As String
Dim lngCount As Long
  If bIsSheet Then
    strRange = strRange & "$]"
  Else
    strRange = strRange & "]"
  End If
  Set oConn = CreateObject("ADODB.Connection")
  If bSuppressHeadingRow Then
    'Suppress first row.
    strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                  "Data Source=" & strSource & ";" & _
                  "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
  Else
    'No suppression.
    strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                   "Data Source=" & strSource & ";" & _
                   "Extended Properties=""Excel 12.0 Xml;HDR=NO"";"
  End If
  oConn.Open ConnectionString:=strConnection
  Set oRecSet = CreateObject("ADODB.Recordset")
  'Read the data from the worksheet/range.
  oRecSet.Open "SELECT * FROM [" & strRange, oConn, 2, 1
  With oRecSet
    .MoveLast
    'Get count.
    lngCount = .RecordCount
    .MoveFirst
  End With
  LoadFromExcel_ADODB = oRecSet.GetRows(lngCount)
  'Cleanup
  If oRecSet.State = 1 Then oRecSet.Close
  Set oRecSet = Nothing
  If oConn.State = 1 Then oConn.Close
  Set oConn = Nothing
lbl_Exit:
  Exit Function
End Function
No matter what I do I can't get the ComboBox to populate. The same code (slightly altered and thanks to macropod and gmaxey! )) works perfectly fine in the main document. Can anyone spot anything obvious that I am doing incorrectly?
Reply With Quote
  #2  
Old 04-04-2016, 04:02 AM
gmayor's Avatar
gmayor gmayor is offline UserForm Dropdown List Not Populating Windows 10 UserForm Dropdown List Not Populating Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You are trying to use code intended for a content control with a userform combobox.
See http://www.gmayor.com/Userform_ComboBox.html which includes code to populate a list box or combobox from an Excel worksheet.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 04-04-2016, 04:10 AM
highrise955 highrise955 is offline UserForm Dropdown List Not Populating Windows 10 UserForm Dropdown List Not Populating Office 2013
Advanced Beginner
UserForm Dropdown List Not Populating
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
You are trying to use code intended for a content control with a userform combobox.
See http://www.gmayor.com/Userform_ComboBox.html which includes code to populate a list box or combobox from an Excel worksheet.

Damn it! I actually had that code from another thread you and macropod helped me with but I chose to try to just duplicate what I did in my main document.

Also feel pretty foolish about the "not a content control" thing as well.

Thanks gmayor, I will give it a shot.
Reply With Quote
  #4  
Old 04-05-2016, 01:45 AM
highrise955 highrise955 is offline UserForm Dropdown List Not Populating Windows 10 UserForm Dropdown List Not Populating Office 2013
Advanced Beginner
UserForm Dropdown List Not Populating
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
You are trying to use code intended for a content control with a userform combobox.
See http://www.gmayor.com/Userform_ComboBox.html which includes code to populate a list box or combobox from an Excel worksheet.
Quote:
Originally Posted by highrise955 View Post
Damn it! I actually had that code from another thread you and macropod helped me with but I chose to try to just duplicate what I did in my main document.

Also feel pretty foolish about the "not a content control" thing as well.

Thanks gmayor, I will give it a shot.
Hi Graham!

Your code works fine for me if I define the range as a worksheet. However, when I specify range as a "Named Range" I get the following error...



Here is my code...

Code:
Private Sub Userform_Initialize()

xlFillList ListOrComboBox:=Me.DrawingNumberUf, _
iColumn:=1, _
strWorkbook:="C:\Temp\ItemSheet.xlsx", _
strRange:="CollRange", _
RangeIsWorksheet:=False, _
RangeIncludesHeaderRow:=True

End Sub


Sub xlFillList(ListOrComboBox As Object, _
iColumn As Long, _
strWorkbook As String, _
strRange As String, _
RangeIsWorksheet As Boolean, _
RangeIncludesHeaderRow As Boolean)




If RangeIsWorksheet = True Then strRange = strRange & "$]"

Set CN = CreateObject("ADODB.Connection")


If RangeIncludesHeaderRow Then
CN.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & strWorkbook & ";" & _
"Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
Else
CN.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & strWorkbook & ";" & _
"Extended Properties=""Excel 12.0 Xml;HDR=NO"";"
End If


Set RS = CreateObject("ADODB.Recordset")
RS.CursorLocation = 3

RS.Open "SELECT * FROM [" & strRange, CN, 2, 1 'read the data from the worksheet

...
...
Any ideas on what I'm doing wrong?
Reply With Quote
  #5  
Old 04-05-2016, 04:47 AM
gmayor's Avatar
gmayor gmayor is offline UserForm Dropdown List Not Populating Windows 10 UserForm Dropdown List Not Populating Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Actually you are doing nothing wrong. The error was mine. The code has lost a line
I'll fix it on the web site, but in the meantime, I have corrected it below.

The userform primarily needs code to set a Tag of 0 for cancel and 1 for proceeding associated with the command buttons and in both cases to hide the form e.g.
Code:
Option Explicit

Private Sub btnOK_Click()
Me.Hide
Me.Tag = 1
End Sub

Private Sub btnCancel_Click()
Me.Hide
Me.Tag = 0
End Sub
The processing then all takes place in the main macro. The code goes in an ordinary module.

Code:
Option Explicit

Private RS As Object
Private CN As Object
Private numrecs As Long, q As Long
Private strWidth As String

Sub Main()
Dim oFrm As New DrawingNumberEntryForm
    With oFrm
        xlFillList ListOrComboBox:=.DrawingNumberUf, _
                   iColumn:=1, _
                   strWorkbook:="C:\Temp\ItemSheet.xlsx", _
                   strRange:="CollRange", _
                   RangeIsWorksheet:=False, _
                   RangeIncludesHeaderRow:=True
        .Show
        If .Tag = 0 Then GoTo lbl_Exit
        'Do stuff with ofrm
    End With
lbl_Exit:
    Unload oFrm
    Set oFrm = Nothing
    Exit Sub
End Sub

Private Function xlFillList(ListOrComboBox As Object, _
                            iColumn As Long, _
                            strWorkbook As String, _
                            strRange As String, _
                            RangeIsWorksheet As Boolean, _
                            RangeIncludesHeaderRow As Boolean, _
                            Optional PromptText As String = "[Select Item]")


    If RangeIsWorksheet = True Then
        strRange = strRange & "$]"
    Else
        strRange = strRange & "]"
    End If

    Set CN = CreateObject("ADODB.Connection")


    If RangeIncludesHeaderRow Then
        CN.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                  "Data Source=" & strWorkbook & ";" & _
                                  "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
    Else
        CN.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                  "Data Source=" & strWorkbook & ";" & _
                                  "Extended Properties=""Excel 12.0 Xml;HDR=NO"";"
    End If


    Set RS = CreateObject("ADODB.Recordset")
    RS.CursorLocation = 3

    RS.Open "SELECT * FROM [" & strRange, CN, 2, 1    'read the data from the worksheet

    With RS
        .MoveLast
        numrecs = .RecordCount
        .MoveFirst
    End With

    With ListOrComboBox
        .ColumnCount = RS.Fields.Count
        If RS.RecordCount > 0 Then
            .Column = RS.GetRows(numrecs)
        End If

        strWidth = vbNullString
        For q = 1 To .ColumnCount
            If q = iColumn Then
                If strWidth = vbNullString Then
                    strWidth = .Width - 4 & " pt"
                Else
                    strWidth = strWidth & .Width - 4 & " pt"
                End If
            Else
                strWidth = strWidth & "0 pt"
            End If
            If q < .ColumnCount Then
                strWidth = strWidth & ";"
            End If
        Next q
        .ColumnWidths = strWidth
        If TypeName(ListOrComboBox) = "ComboBox" Then
            .AddItem PromptText, 0
            If Not iColumn - 1 = 0 Then .Column(iColumn - 1, 0) = PromptText
            .ListIndex = 0
        End If
    End With

    'Cleanup
    If RS.State = 1 Then RS.Close
    Set RS = Nothing
    If CN.State = 1 Then CN.Close
    Set CN = Nothing

lbl_Exit:
    Exit Function
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #6  
Old 04-05-2016, 11:40 PM
highrise955 highrise955 is offline UserForm Dropdown List Not Populating Windows 10 UserForm Dropdown List Not Populating Office 2013
Advanced Beginner
UserForm Dropdown List Not Populating
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Actually you are doing nothing wrong. The error was mine. The code has lost a line
I'll fix it on the web site, but in the meantime, I have corrected it below.

The userform primarily needs code to set a Tag of 0 for cancel and 1 for proceeding associated with the command buttons and in both cases to hide the form e.g.
Graham,

Here is my userform after I added your updated code...

Code:
Option Explicit
Private RS As Object
Private CN As Object
Private numrecs As Long, q As Long
Private strWidth As String

Private Sub CANCELbutton_Click()
Me.Hide
Me.Tag = 0
Application.Quit
End Sub

Private Sub OKbutton_Click()
ActiveDocument.SelectContentControlsByTitle("Drawing Number").Item(1).Range.Text = DrawingNumberUf
Me.Hide
Me.Tag = 1
lbl_Exit:
Unload Me
Exit Sub
End Sub

Sub Main()
Dim oFrm As New DrawingNumberEntryForm
    With oFrm
        xlFillList ListOrComboBox:=.DrawingNumberUf, _
                   iColumn:=1, _
                   strWorkbook:="C:\Temp\ItemSheet.xlsx", _
                   strRange:="Table1", _
                   RangeIsWorksheet:=False, _
                   RangeIncludesHeaderRow:=True
        .Show
        If .Tag = 0 Then GoTo lbl_Exit
        'Do stuff with ofrm
    End With
lbl_Exit:
    Unload oFrm
    Set oFrm = Nothing
    Exit Sub
End Sub

Private Function xlFillList(ListOrComboBox As Object, _
                            iColumn As Long, _
                            strWorkbook As String, _
                            strRange As String, _
                            RangeIsWorksheet As Boolean, _
                            RangeIncludesHeaderRow As Boolean, _
                            Optional PromptText As String = "[Select Item]")


    If RangeIsWorksheet = True Then
        strRange = strRange & "$]"
    Else
        strRange = strRange & "]"
    End If

    Set CN = CreateObject("ADODB.Connection")


    If RangeIncludesHeaderRow Then
        CN.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                  "Data Source=" & strWorkbook & ";" & _
                                  "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
    Else
        CN.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                  "Data Source=" & strWorkbook & ";" & _
                                  "Extended Properties=""Excel 12.0 Xml;HDR=NO"";"
    End If


    Set RS = CreateObject("ADODB.Recordset")
    RS.CursorLocation = 3

    RS.Open "SELECT * FROM [" & strRange, CN, 2, 1    'read the data from the worksheet

    With RS
        .MoveLast
        numrecs = .RecordCount
        .MoveFirst
    End With

    With ListOrComboBox
        .ColumnCount = RS.Fields.Count
        If RS.RecordCount > 0 Then
            .Column = RS.GetRows(numrecs)
        End If

        strWidth = vbNullString
        For q = 1 To .ColumnCount
            If q = iColumn Then
                If strWidth = vbNullString Then
                    strWidth = .Width - 4 & " pt"
                Else
                    strWidth = strWidth & .Width - 4 & " pt"
                End If
            Else
                strWidth = strWidth & "0 pt"
            End If
            If q < .ColumnCount Then
                strWidth = strWidth & ";"
            End If
        Next q
        .ColumnWidths = strWidth
        If TypeName(ListOrComboBox) = "ComboBox" Then
            .AddItem PromptText, 0
            If Not iColumn - 1 = 0 Then .Column(iColumn - 1, 0) = PromptText
            .ListIndex = 0
        End If
    End With

    'Cleanup
    If RS.State = 1 Then RS.Close
    Set RS = Nothing
    If CN.State = 1 Then CN.Close
    Set CN = Nothing

lbl_Exit:
    Exit Function
End Function
Unfortunately, the combobox still isn't populating. Was I supposed to put all the code in the userform or the "Sub Main()" in the main document? (The form that actually calls the userform.)

I appreciate your help. I really am learning a lot thanks to you and others.
Reply With Quote
  #7  
Old 04-06-2016, 04:02 AM
gmayor's Avatar
gmayor gmayor is offline UserForm Dropdown List Not Populating Windows 10 UserForm Dropdown List Not Populating Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Code:
Option Explicit
Private Sub CANCELbutton_Click()
Me.Hide
Me.Tag = 0
End Sub

Private Sub OKbutton_Click()
Me.Hide
Me.Tag = 1
End Sub
is what goes in the userform code module. This hides the userform and passes the control to the calling macro Sub Main() which continues based on the value of Me.Tag.

The parts that you added to the above go in the code that calls the userform e.g. as follows. The Excel function also goes in the same module as Sub Main.

Code:
Sub Main()
Dim oFrm As New DrawingNumberEntryForm
    With oFrm
        xlFillList ListOrComboBox:=.DrawingNumberUf, _
                   iColumn:=1, _
                   strWorkbook:="C:\Temp\ItemSheet.xlsx", _
                   strRange:="CollRange", _
                   RangeIsWorksheet:=False, _
                   RangeIncludesHeaderRow:=True
        .Show
        If .Tag = 0 Then GoTo lbl_Exit 'Cancel was selected
        ActiveDocument.SelectContentControlsByTitle("Drawing Number").Item(1).Range.Text = .DrawingNumberUf.Text
    End With
lbl_Exit:
    Unload oFrm
    Set oFrm = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #8  
Old 04-06-2016, 05:06 AM
highrise955 highrise955 is offline UserForm Dropdown List Not Populating Windows 10 UserForm Dropdown List Not Populating Office 2013
Advanced Beginner
UserForm Dropdown List Not Populating
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Code:
Option Explicit
Private Sub CANCELbutton_Click()
Me.Hide
Me.Tag = 0
End Sub

Private Sub OKbutton_Click()
Me.Hide
Me.Tag = 1
End Sub
is what goes in the userform code module. This hides the userform and passes the control to the calling macro Sub Main() which continues based on the value of Me.Tag.

The parts that you added to the above go in the code that calls the userform e.g. as follows. The Excel function also goes in the same module as Sub Main.

Code:
Sub Main()
Dim oFrm As New DrawingNumberEntryForm
    With oFrm
        xlFillList ListOrComboBox:=.DrawingNumberUf, _
                   iColumn:=1, _
                   strWorkbook:="C:\Temp\ItemSheet.xlsx", _
                   strRange:="CollRange", _
                   RangeIsWorksheet:=False, _
                   RangeIncludesHeaderRow:=True
        .Show
        If .Tag = 0 Then GoTo lbl_Exit 'Cancel was selected
        ActiveDocument.SelectContentControlsByTitle("Drawing Number").Item(1).Range.Text = .DrawingNumberUf.Text
    End With
lbl_Exit:
    Unload oFrm
    Set oFrm = Nothing
    Exit Sub
End Sub
Gotcha!

I'll give it a try.
Reply With Quote
  #9  
Old 04-07-2016, 11:33 AM
highrise955 highrise955 is offline UserForm Dropdown List Not Populating Windows 10 UserForm Dropdown List Not Populating Office 2013
Advanced Beginner
UserForm Dropdown List Not Populating
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Code:
Option Explicit
Private Sub CANCELbutton_Click()
Me.Hide
Me.Tag = 0
End Sub

Private Sub OKbutton_Click()
Me.Hide
Me.Tag = 1
End Sub
is what goes in the userform code module. This hides the userform and passes the control to the calling macro Sub Main() which continues based on the value of Me.Tag.

The parts that you added to the above go in the code that calls the userform e.g. as follows. The Excel function also goes in the same module as Sub Main.

Code:
Sub Main()
Dim oFrm As New DrawingNumberEntryForm
    With oFrm
        xlFillList ListOrComboBox:=.DrawingNumberUf, _
                   iColumn:=1, _
                   strWorkbook:="C:\Temp\ItemSheet.xlsx", _
                   strRange:="CollRange", _
                   RangeIsWorksheet:=False, _
                   RangeIncludesHeaderRow:=True
        .Show
        If .Tag = 0 Then GoTo lbl_Exit 'Cancel was selected
        ActiveDocument.SelectContentControlsByTitle("Drawing Number").Item(1).Range.Text = .DrawingNumberUf.Text
    End With
lbl_Exit:
    Unload oFrm
    Set oFrm = Nothing
    Exit Sub
End Sub
Graham,

It's working great! Thanks a lot.

I have one final issue I was hoping I could figure out on my own but I'm not having any luck. Maybe you can offer a suggestion or two?

Below is the sample table I am using in Excel. Currently the range include all three columns.



Populating the "Drawing Number" using this code...

Code:
ActiveDocument.SelectContentControlsByTitle("Drawing Number").Item(1).Range.Text = .DrawingNumberUf.Text
in the Sub Main() module works perfectly. However, I am trying to figure out how to populate other content controls with the "Rev" and "Name" values. I will be adding a few other columns shortly and will need to do the same for them.

Any suggestions or links to your web site where you cover this would be greatly appreciated.

As always, thanks for your help.
Reply With Quote
  #10  
Old 04-08-2016, 05:30 AM
gmaxey gmaxey is offline UserForm Dropdown List Not Populating Windows 7 32bit UserForm Dropdown List Not Populating Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

If you are populating a multicolumn listbox then your code would look something like this:

Code:
ActiveDocument.SelectContentControlsByTitle("Drawing Number").Item(1).Range.Text = .DrawingNumberUf.Column(0)
ActiveDocument.SelectContentControlsByTitle("Rev").Item(1).Range.Text = .DrawingNumberUf.Column(1)
ActiveDocument.SelectContentControlsByTitle("Name").Item(1).Range.Text = .DrawingNumberUf.Column(2)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #11  
Old 04-08-2016, 05:56 AM
highrise955 highrise955 is offline UserForm Dropdown List Not Populating Windows 10 UserForm Dropdown List Not Populating Office 2013
Advanced Beginner
UserForm Dropdown List Not Populating
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
If you are populating a multicolumn listbox then your code would look something like this:

Code:
ActiveDocument.SelectContentControlsByTitle("Drawing Number").Item(1).Range.Text = .DrawingNumberUf.Column(0)
ActiveDocument.SelectContentControlsByTitle("Rev").Item(1).Range.Text = .DrawingNumberUf.Column(1)
ActiveDocument.SelectContentControlsByTitle("Name").Item(1).Range.Text = .DrawingNumberUf.Column(2)

Awesome! Works exactly how I want it to.

Thanks for taking the time to respond.
Reply With Quote
  #12  
Old 04-10-2016, 08:29 AM
highrise955 highrise955 is offline UserForm Dropdown List Not Populating Windows 10 UserForm Dropdown List Not Populating Office 2013
Advanced Beginner
UserForm Dropdown List Not Populating
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Graham (or anyone else),

I need some assistance on how I would modify the following code so I could use an Access (Access 2016) database as my data source to populate content controls. Any information, be it code or links, will be appreciated.

Code:
Sub Main()

Dim oFrm As New DrawingNumberEntryForm 'userform to retrieve initial drawing number
    With oFrm
        xlFillList ListOrComboBox:=.DrawingNumberUf, _
                   iColumn:=1, _
                   strWorkbook:="C:\Temp\ItemSheet2.xlsx", _
                   strRange:="TableXXX", _
                   RangeIsWorksheet:=False, _
                   RangeIncludesHeaderRow:=True
        .Show
        If .Tag = 0 Then GoTo lbl_Exit 'Cancel was selected
        'ActiveDocument.SelectContentControlsByTitle("Drawing Number").Item(1).Range.Text = .DrawingNumberUf.Text
        
        If .DrawingNumberUf.Column(3) = "N" Then
            ActiveDocument.Unprotect '("PreciseTF")
            ActiveDocument.Tables(2).Delete
            ActiveDocument.SelectContentControlsByTitle("Cert Type").Item(1).Range.Text = .DrawingNumberUf.Column(4)
            ActiveDocument.Protect NoReset:=True, Password:="", Type:=wdAllowOnlyFormFields
        End If
                   
        
        ActiveDocument.SelectContentControlsByTitle("Drawing Number").Item(1).Range.Text = .DrawingNumberUf.Column(0)
        ActiveDocument.SelectContentControlsByTitle("Revision").Item(1).Range.Text = .DrawingNumberUf.Column(1)
        ActiveDocument.SelectContentControlsByTitle("Part Description").Item(1).Range.Text = .DrawingNumberUf.Column(2)
        
    End With

lbl_Exit:
    Unload oFrm
    Set oFrm = Nothing
    Exit Sub
End Sub

--------------------------------------------------------------------------------

Private Function xlFillList(ListOrComboBox As Object, _
                            iColumn As Long, _
                            strWorkbook As String, _
                            strRange As String, _
                            RangeIsWorksheet As Boolean, _
                            RangeIncludesHeaderRow As Boolean)


    If RangeIsWorksheet = True Then
        strRange = strRange & "$]"
    Else
        strRange = strRange & "]"
    End If

    Set CN = CreateObject("ADODB.Connection")


    If RangeIncludesHeaderRow Then
        CN.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                  "Data Source=" & strWorkbook & ";" & _
                                  "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
    Else
        CN.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                  "Data Source=" & strWorkbook & ";" & _
                                  "Extended Properties=""Excel 12.0 Xml;HDR=NO"";"
    End If


    Set RS = CreateObject("ADODB.Recordset")
    RS.CursorLocation = 3

    RS.Open "SELECT * FROM [" & strRange, CN, 2, 1    'read the data from the worksheet

    With RS
        .MoveLast
        numrecs = .RecordCount
        .MoveFirst
    End With

    With ListOrComboBox
        .ColumnCount = RS.Fields.Count
        If RS.RecordCount > 0 Then
            .Column = RS.GetRows(numrecs)
        End If

        strWidth = vbNullString
        For q = 1 To .ColumnCount
            If q = iColumn Then
                If strWidth = vbNullString Then
                    strWidth = .Width - 4 & " pt"
                Else
                    strWidth = strWidth & .Width - 4 & " pt"
                End If
            Else
                strWidth = strWidth & "0 pt"
            End If
            If q < .ColumnCount Then
                strWidth = strWidth & ";"
            End If
        Next q
        .ColumnWidths = strWidth
        
    End With

    'Cleanup
    If RS.State = 1 Then RS.Close
    Set RS = Nothing
    If CN.State = 1 Then CN.Close
    Set CN = Nothing

lbl_Exit:
    Exit Function
End Function
Here is my table (DrawingNumberTable) in my Access database (DrawingNumberdB):

Reply With Quote
  #13  
Old 04-10-2016, 11:35 AM
gmaxey gmaxey is offline UserForm Dropdown List Not Populating Windows 7 32bit UserForm Dropdown List Not Populating Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

HR955

Graham and I both have published information on populating listboxes on our websites. It is time for you to start catching your own fish.

http://gregmaxey.com/word_tip_pages/...functions.html
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #14  
Old 04-10-2016, 11:54 AM
highrise955 highrise955 is offline UserForm Dropdown List Not Populating Windows 10 UserForm Dropdown List Not Populating Office 2013
Advanced Beginner
UserForm Dropdown List Not Populating
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
HR955

Graham and I both have published information on populating listboxes on our websites. It is time for you to start catching your own fish.

http://gregmaxey.com/word_tip_pages/...functions.html

I more than agree. That is why I asked for any links. Thanks!
Reply With Quote
  #15  
Old 04-10-2016, 12:10 PM
highrise955 highrise955 is offline UserForm Dropdown List Not Populating Windows 10 UserForm Dropdown List Not Populating Office 2013
Advanced Beginner
UserForm Dropdown List Not Populating
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
HR955

Graham and I both have published information on populating listboxes on our websites. It is time for you to start catching your own fish.

http://gregmaxey.com/word_tip_pages/...functions.html
Your demo "Populate UserForm ListBox or ComboBox w\Advanced Functions" is awesome.

Just the "bait" I was looking for. Thanks again!

P.S. As with Graham's website, I have made a donation to yours.
Reply With Quote
Reply

Tags
combobox, populate, userform



Similar Threads
Thread Thread Starter Forum Replies Last Post
UserForm Dropdown List Not Populating Excel List Box not populating worksheet field upon submit Shane.Hutchison Excel Programming 1 10-22-2015 12:24 PM
Help!! Dropdown List christo16 Word 1 06-29-2015 05:18 AM
Dropdown list, Macro shield5 Excel Programming 7 10-27-2013 01:51 AM
UserForm Dropdown List Not Populating Need help populating dropdown box antztaylor Word 3 11-06-2012 05:46 PM
UserForm Dropdown List Not Populating Populating ComboBox or Drop Down list with contents of a text field Billy_McSkintos Word VBA 1 09-13-2011 05:50 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:03 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft