Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #6  
Old 10-19-2014, 03:24 AM
Faldinio Faldinio is offline Populate Word Drop-down list with Excel column then auto fill form fields with Excel data Windows 7 64bit Populate Word Drop-down list with Excel column then auto fill form fields with Excel data Office 2010 64bit
Novice
Populate Word Drop-down list with Excel column then auto fill form fields with Excel data
 
Join Date: Oct 2014
Posts: 4
Faldinio is on a distinguished road
Default

Following on from the tutorials you listed, I think a User Form will suffice.

I have tried to follow the instructions here: http://www.gmayor.com/Userform_ComboBox.html

However I cannot get it to work. There are no errors, but the ComboBox remains empty - it isn't populating it.

The code is exactly as in the tutorial

Code:
Option Explicit
Private rs As Object
Private cn As Object
Private numrecs As Long, q As Long
Private strWidth As String

Public Function xlFillList(ListOrComboBox As Object, _
strWorkbook As String, _
strRange As String, _
bisRangeASheet As Boolean)
'A function to fill a list or combo box with data from an Excel worksheet or a named range in a worksheet
'ListOrComboBox is the name of the list or combo box
'strWorkbook is the name of the Excel data file
'strRange is the part of the data file to be used
'bisRangeASheet - is the range 'strRange' a sheet (true) or a named range (false)


Set cn = CreateObject("ADODB.Connection")

'Alternative connection 1
'*********************************
cn.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & strWorkbook & ";" & _
"Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
'*********************************

'Alternative connection 2
'*********************************
'cn.Provider = "Microsoft OLE DB Provider for ODBC Drivers"
'cn.ConnectionString = "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & strWorkbook
'cn.Open
'*********************************

Set rs = CreateObject("ADODB.Recordset")
If bisRangeASheet = True Then
'strRange is a named worksheet use:
rs.Open "SELECT * FROM [" & strRange & "$]", cn, 2, 1 'read the data
Else
'strRange is a named range use:
rs.Open "SELECT * FROM [" & strRange & "]", cn, 2, 1 'read the data
End If

With rs
.MoveLast 'find the last record
numrecs = .RecordCount 'and note its number
.MoveFirst 'return to the start
End With

With ListOrComboBox 'load the records into the columns of the named list/combo box
.ColumnCount = rs.Fields.Count
.Column = rs.GetRows(numrecs)

'set the widths of the combo/list box columns (optional)
'the alternative is to display all the columns

'***********************
 strWidth = .Width - 2 & " pt;"
 For q = 2 To .ColumnCount
 strWidth = strWidth & "0 pt"
 If q < .ColumnCount Then
 strWidth = strWidth & ";"
 End If
 Next q
 .ColumnWidths = strWidth
'***********************
End With

'Cleanup
If rs.State = 1 Then rs.Close
If cn.State = 1 Then cn.Close
Set rs = Nothing
Set cn = Nothing
End Function

Sub CallUF()

Dim oFrm As UserForm1
Dim oVars As Word.Variables
Dim strTemp As String
Dim oRng As Word.Range
Dim i As Long
    
  Set oFrm = New UserForm1
  With oFrm
    .Show
  End With
    

End Sub
This is called when the UserForm is initialised.

Code:
Private Sub UserForm_Initialize()

xlFillList UserForm1.ComboBox1, "E:\path_to_file\test.xlsx", "Sheet1", True

End Sub
If the filename is incorrect then an error occurs, so I know it is checking for the data, it's just not putting it in the ComboBox...

Can anyone guide me here?

Thanks in advance.


Mark
Reply With Quote
 

Tags
auto-fill, content controls, cross platform



Similar Threads
Thread Thread Starter Forum Replies Last Post
Populate Word Drop-down list with Excel column then auto fill form fields with Excel data Auto Populate Word From to Excel File webber Word 1 10-02-2013 02:52 PM
Auto-populate from form fields kenelder Word 3 05-23-2013 07:50 AM
Populate Word Drop-down list with Excel column then auto fill form fields with Excel data drop down list from external data source Excel 2007? Intruder Excel 1 08-03-2012 05:41 AM
Populate Word Drop-down list with Excel column then auto fill form fields with Excel data Open Word w Excel & fill Word textboxes w info from Excel fields runtime error 4248 Joe Patrick Word VBA 2 01-30-2012 07:23 AM
Word Fill-in form with Excel spreadsheet karik Word 0 01-11-2010 08:45 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:36 AM.


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