Trying to populate a combobox

P

PGM

I am trying to populate a combo box using a simple SQL query to a database
but all I get in the combo box is "System._ComObject" . Any suggestions are
greatly appreciated

On Error Resume Next

Dim strSQL As String

Dim strConn As String

Dim i As Integer

strSQL = "SELECT F2 FROM dbo.Masterserver WHERE F2 like 't%'"

strConn = "Provider=SQLOLEDB.1;Password=vbuser;Persist Security
Info=True;User ID=VBuser;Initial Catalog=Masterserver;Data
Source=patmtest\sqlexpress"

MyConnObj = New ADODB.Connection

MyConnObj.Open(strConn)

cmdselect = New ADODB.Command

cmdselect.ActiveConnection = MyConnObj

cmdselect.CommandText = strSQL

myRecSet = New ADODB.Recordset

myRecSet.Open(strSQL, MyConnObj, ADODB.CursorTypeEnum.adOpenKeyset,
ADODB.LockTypeEnum.adLockOptimistic, ADODB.CommandTypeEnum.adCmdText)

MsgBox("Total Number of records = " & myRecSet.RecordCount) 'Test SQL query

ComboBox1.Refresh()

myRecSet.MoveFirst()

For i = 1 To myRecSet.RecordCount

ComboBox1.Items.Add(myRecSet.Fields("F2"))

Next i

myRecSet.Close()

MyConnObj.Close()

myRecSet = Nothing

MyConnObj = Nothing
 
A

Armin Zingler

PGM said:
I am trying to populate a combo box using a simple SQL query to a
database but all I get in the combo box is "System._ComObject" . Any
suggestions are greatly appreciated


Is there a reason why you use COM Interop (ADO) instead of ADO.Net?

ComboBox1.Items.Add(myRecSet.Fields("F2"))

You add a field. It's ToString method returns the data type, which is
"System._ComObject". The combobox always displays the string returned by
the object's ToString method.

Use
myRecSet.Fields("F2").Value.ToString
instead.


Armin
 
A

Armin Zingler

Armin Zingler said:
Use
myRecSet.Fields("F2").Value.ToString
instead.

Of course,
myRecSet.Fields("F2").Value
works, too (because of the reason given before).


Armin
 
P

PGM

A novice at work

Armin Zingler said:
Is there a reason why you use COM Interop (ADO) instead of ADO.Net?



You add a field. It's ToString method returns the data type, which is
"System._ComObject". The combobox always displays the string returned by
the object's ToString method.

Use
myRecSet.Fields("F2").Value.ToString
instead.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top