tags:

views:

18

answers:

1

Hi,

I have the following code in a word 2007 macro where I populate a dropdown with customer names from an excel spreadsheet

Private Sub UserForm_Initialize()
Dim i As Integer
Dim cn As ADODB.Connection
Dim rsT As New ADODB.Recordset
Set cn = New ADODB.Connection
With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=CCustomers.xls;Extended Properties=Excel 8.0;"
.CursorLocation = adUseClient
.Open
End With
rsT.Open "Select distinct * from Customer", cn, adOpenStatic

i = 0

With rsT
' This code populates the combo box with the values
' in the YourNamedRange named range in the .xls file. this exampletable is 2 rows by 6 columns and is set as a named range.

Do Until .EOF
ComboBox_Company.AddItem (i)
ComboBox_Company.Column(0, i) = rsT.Fields(0).Value
.MoveNext
i = i + 1
Loop
End With
End Sub

So I I have a column with customer names and I created a named range (Customer) and it populates the dropdown. However, when I select a customer in the dropdown I want to populate two address fields with (1 street, 2 city) the customer's address.

Private Sub cbo_customer_Change()
            Dim customerName As String
            customerName = cbo_customer.Value
End Sub

The spreadsheet has about 10 columns, Customer in the first one and address1 in the 9th and address2 in the last one. How can I use the variable customer to populate the address fields? Do I have to create a new named range with all the fields and have something like select customer, address1, address2 from myRange where customer = customerName?

Any help is appreciated.

Thanks in advance.

A: 

You answered the question yourself, create a named range with the columns your need and then do the select:

"SELECT customer, address1, address2 FROM NewRange WHERE customer = '" & customerName & "'"

Into a new record set, and get the address fields from item 0 in it.

slomojo
Wow I didn't even tried it, didn't think it would work. However, if I have the query rsT.Open "Select Customer, Postcode, State, Country from Customers", cn, adOpenStatic how do I populate for example the textbox CompanyName.Value = the customer name? Tried this time to do CompanyName.Value = Customer but it didn't work
Morgan
does `cbo_customer.Value` not return the right value for Customer?
slomojo
I forgot the WHERE statement there (was too excited) rsT.Open "SELECT Customer, Postcode, State, Country FROM Customers WHERE Customer =" + customerName, cn, adOpenStatic throws me an error though, I'm not used to the VBA syntax, how should I correct the query statement?
Morgan
What error does it throw?
slomojo
syntax error (missing operator) in query expression 'Customer = someCustomer'.
Morgan
I think *perhaps* the customerName in the SQL query should be appearing in quotes. I forget how to do string escapes in VBA/VB though. I think it's """.
slomojo
slomojo
Morgan
Ok so I do Dim pc As String then pc = rsT.Fields("Postcode") CompanyAddress1.Value = pc
Morgan
Does that work as expected? Remember the result set is a list.
slomojo