Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default ODBC ControlSource

Hello,

I want to be able to query an ODBC database through a Combobox on a
VBA form. I can do it if the data is stored in cells on the
spreadsheet but I have other info that is on the mainframe system that
I need to access.

Any help would be appreciated.

Thanks
Dean
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 79
Default ODBC ControlSource

Hi Dean,

Use the macro recorder to find the appropriate source and connection type.
After turning on the recorder from the Tools/Macro menu, go to the Data menu
and choose Get External Data / New Database Query. Choose the data source
type from the first dialog. If it is not in the list, you will have to try
something else. If it is, another dialog will appear asking for the location
of the file. Find it and create a simple query and end the macro recording.
View the recorded macro for the details you seek.

HTH, Greg

"Dkso" wrote in message
om...
Hello,

I want to be able to query an ODBC database through a Combobox on a
VBA form. I can do it if the data is stored in cells on the
spreadsheet but I have other info that is on the mainframe system that
I need to access.

Any help would be appreciated.

Thanks
Dean



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default ODBC ControlSource

Greg,

Thanks, it does help but not with this problem.
What you have below will bring back everything on the table, in the instance
I need to use it that is not possible as there are over 172,000 records on
this table (and obviously only 65,532 will fit onto an Excel spreadsheet).

I have used a ComboBox in MS Access to achieve what I want to do but don't
know how to do it in Excel.
I want to click on the ComboBox or start typing an item code and the item be
displayed in the dropdown box.

Dean

"Greg Koppel" wrote in message
...
Hi Dean,

Use the macro recorder to find the appropriate source and connection type.
After turning on the recorder from the Tools/Macro menu, go to the Data

menu
and choose Get External Data / New Database Query. Choose the data source
type from the first dialog. If it is not in the list, you will have to try
something else. If it is, another dialog will appear asking for the

location
of the file. Find it and create a simple query and end the macro

recording.
View the recorded macro for the details you seek.

HTH, Greg

"Dkso" wrote in message
om...
Hello,

I want to be able to query an ODBC database through a Combobox on a
VBA form. I can do it if the data is stored in cells on the
spreadsheet but I have other info that is on the mainframe system that
I need to access.

Any help would be appreciated.

Thanks
Dean





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default ODBC ControlSource

"Dkso" wrote ...

172,000 records on
this table (and obviously only 65,532 will fit onto an Excel spreadsheet).
I have used a ComboBox in MS Access to achieve what I want to do but don't
know how to do it in Excel.
I want to click on the ComboBox or start typing an item code and the item be
displayed in the dropdown box.


This in a userform with a combo named ComboBox1 (check the connection
string for northwind):

Option Explicit

Private Sub UserForm_Activate()

Dim Con As Object
Dim rs As Object
Dim strConJet As String
Dim strSql1 As String

' Amend the following constants to suit
Const CONN_STRING_JET As String = "" & _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\" & _
"Microsoft Visual Studio\VB98\NWIND.mdb"

strConJet = CONN_STRING_JET

' Build sql statement
strSql1 = "SELECT LastName FROM Employees"

' Open connection
Set Con = CreateObject("ADODB.Connection")
With Con
.Open strConJet
Set rs = .Execute(strSql1)

' Write data to combo
ComboBox1.List = _
Excel.Application.Transpose(rs.GetRows)

.Close
End With

End Sub

Jamie.

--
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default ODBC ControlSource

Worked great guns at home, on the Northwind DB; boy was it fun trying it on the ODBC db at work. Any way I’ve got down to the bottom and now got a Type mismatch error.
Private Sub UserForm_Activate()

Dim Con As Object
Dim rs As Object
Dim strConJet As String
Dim strSql1 As String
' Amend the following constants to suit
Const CONN_STRING_JET As String = "DSN=MySN;" & _
"UID=MyUSID;" & _
"PWD=MyPWD;" & _
"SERVER=MySEVER;"

strConJet = CONN_STRING_JET
' Build sql statement
strSql1 = "SELECT LOC.LOC FROM DC5999D.LOC LOC"
' Open connection
Set Con = CreateObject("ADODB.Connection")
With Con
.Open strConJet
Set rs = .Execute(strSql1)
' Write data to combo
ComboBox1.List = _
Excel.Application.Transpose(rs.GetRows)
.Close
End With
End Sub

Error message is: -

Run time error ‘13’:
Type Mismatch
at Line "ComboBox1.List=_
Excel.Application.Tanspose(rs.GetRows)"
The LOC fieldof the LOC table is a STRING so I don't understand how it's a type Mismatch!
I have used the Record Macro in Excel to get the details for the SERVER, DSN etc. so those details should be correct.DC5999D is the database and LOC is the table.
Can anyone tell me where I'm ing wrong.

Thanks in advance

Dean

"Jamie Collins" wrote in message om...
"Dkso" wrote ...

172,000 records on
this table (and obviously only 65,532 will fit onto an Excel spreadsheet).
I have used a ComboBox in MS Access to achieve what I want to do but don't
know how to do it in Excel.
I want to click on the ComboBox or start typing an item code and the item be
displayed in the dropdown box.


This in a userform with a combo named ComboBox1 (check the connection
string for northwind):

Option Explicit

Private Sub UserForm_Activate()

Dim Con As Object
Dim rs As Object
Dim strConJet As String
Dim strSql1 As String

' Amend the following constants to suit
Const CONN_STRING_JET As String = "" & _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\" & _
"Microsoft Visual Studio\VB98\NWIND.mdb"

strConJet = CONN_STRING_JET

' Build sql statement
strSql1 = "SELECT LastName FROM Employees"

' Open connection
Set Con = CreateObject("ADODB.Connection")
With Con
.Open strConJet
Set rs = .Execute(strSql1)

' Write data to combo
ComboBox1.List = _
Excel.Application.Transpose(rs.GetRows)

.Close
End With

End Sub

Jamie.

--



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default ODBC ControlSource

"Dkso" wrote ...

Worked great guns at home, on the Northwind DB; boy was it fun trying it
on the ODBC db at work. Any way I ve got down to the bottom and now
got a Type mismatch error.
Private Sub UserForm Activate()

Dim Con As Object
Dim rs As Object
Dim strConJet As String
Dim strSql1 As String
' Amend the following constants to suit
Const CONN STRING JET As String = "DSN=MySN;" &
"UID=MyUSID;" &
"PWD=MyPWD;" &
"SERVER=MySEVER;"

strConJet = CONN STRING JET
' Build sql statement
strSql1 = "SELECT LOC.LOC FROM DC5999D.LOC LOC"
' Open connection
Set Con = CreateObject("ADODB.Connection")
With Con
.Open strConJet
Set rs = .Execute(strSql1)
' Write data to combo
ComboBox1.List =
Excel.Application.Transpose(rs.GetRows)
.Close
End With
End Sub

Error message is: -

Run time error 13 :
Type Mismatch
at Line "ComboBox1.List=
Excel.Application.Tanspose(rs.GetRows)"
The LOC fieldof the LOC table is a STRING so I don't understand how it's
a type Mismatch!
I have used the Record Macro in Excel to get the details for the SERVER,
DSN etc. so those details should be correct.DC5999D is the database and
LOC is the table.
Can anyone tell me where I'm ing wrong.


I can't see anything that would give that particular error. The SQL
looks odd (a table named LOC with a column named LOC and you are
aliasing the table as LOC?) but I guess it must be valid because you
are not getting an error on the Execute line. Perhaps post some more
details e.g. which database product, table schema, sample data, etc.

Jamie.

--
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
question about the ControlSource of a ComboBox delaney_55 Excel Programming 2 April 18th 04 03:56 PM
Controlsource David Coleman Excel Programming 5 December 28th 03 01:14 PM
ControlSource Compatibility VBA - VB6 John M[_4_] Excel Programming 2 October 19th 03 05:30 PM
ControlSource problem Christy[_2_] Excel Programming 1 August 28th 03 03:44 AM
Valid ControlSource values Jason Gatsby Excel Programming 0 August 4th 03 07:36 PM


All times are GMT +1. The time now is 04:45 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"