SQL Combobox

jwbatey07

New Member
Joined
Apr 14, 2020
Messages
33
Office Version
  1. 365
Platform
  1. Windows
i'm trying to get the combobox to come back with my list of customer name, p21_view_customer.customer_name. something is wrong and I can't figure it out. Please help.
Code:
Private Sub ComboBox1_Change()
With Application
    .Calculation = xlManual
    .EnableEvents = False
    .ScreenUpdating = False
End With
Dim stSQL As String, sBook As String, rst As ADODB.Recordset, k As Integer, vaData As Variant
Dim objConn As ADODB.Connection
Dim ConnectionString As String
ConnectionString = "Driver={SQL Server}; "
Set objConn = New ADODB.Connection
stSQL = "SELECT  [p21_view_customer].[credit_status], [p21_view_customer].[customer_name], [p21_view_customer].[customer_id] " & _
        "FROM  [dbo].[p21_view_customer] "
With objConn
    .CursorLocation = adUseClient
    .Open ConnectionString
    Set rst = .Execute(stSQL)
End With
With rst
    Set .ActiveConnection = Nothing
    k = .Fields.Count
    vaData = .GetRows
End With
ComboBox1.List = vaData
objConn.Close
Set rst = Nothing
Set objConn = Nothing
End Sub
 
Last edited by a moderator:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
GetRows returns an array transposed from how you actually want it, so use:

Code:
ComboBox1.Column = vaData

and make sure that your combobox's ColumnCount property is set appropriately.
 
Upvote 0
Thank you for the response. i've changed "list" to "column". I'm a beginner when it comes to VBA so I'm a little lost on how to set the property on the ColumnCount. Something did occur when I changed "list" to column. I'm now getting back data but the data coming back is from field p21_view_customer.credit_status and not p21_view_customer.customer_name.
 
Upvote 0
i'm not sure if what i'm attempting is possible? I've tried so many things and it's not working. Maybe you can let me know if this is possible, I have a database that has customer account information. i would like a single combobox and a single textbox on a form. i want to be able to query a customer using the combobox. Once the customer name is entered or selected, I would like to bring back the customer's credit status in the textbox. i do not need the data being brought back into a sheet.
 
Upvote 0
You’ve returned three fields in your query, so the columncount should be 3. You can adjust the column widths and bound column to display/store whichever fields) you like.
 
Upvote 0
that makes sense. I adjusted the sequence in which I had the fields from the table in and I got the customer names to populate in the combobox because I made that the first field in the statement. I now need to figure out the how to assign columns. When a customer is selected I want to have the textbox tell me their current credit status in text form. thank you!
 
Upvote 0
Can you give me an idea of how I need to edit my code? It makes sense the solution but I'm having trouble with the code portion.
 
Upvote 0
something like this?


Private Sub ComboBox1_Change()
With Application
.Calculation = xlManual
.EnableEvents = False
.ScreenUpdating = False
End With
Dim stSQL As String, rst As ADODB.Recordset, k As Integer, vaData As Variant
Dim objConn As ADODB.Connection
Dim ConnectionString As String
ConnectionString = "Driver={SQL Server};
Set objConn = New ADODB.Connection
stSQL = "SELECT [p21_view_customer].[customer_name], [p21_view_customer].[credit_status], [p21_view_customer].[customer_id] " & _
"FROM [dbo].[p21_view_customer] "
With objConn
.CursorLocation = adUseClient
.Open ConnectionString
Set rst = .Execute(stSQL)
End With
With ComboBox1.Column
.RowSource = stSQL
.ColumnCount = 3
.BoundColumn = 1

End With
objConn.Close
Set rst = Nothing
Set objConn = Nothing
bClear = True

End Sub
 
Upvote 0
Code:
With ComboBox1
.Column = rst.GetRows
.ColumnCount = 3
   .BoundColumn = 1
end with

for example.
 
Upvote 0

Forum statistics

Threads
1,214,986
Messages
6,122,611
Members
449,090
Latest member
vivek chauhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top