Re: Trying to populate a Combo Box from SQL DB



1. You probably need to clear Combobox instead of Refreshing it before the
loop.
2. Most likely it is the server-side cursor, which always returns
RecordCount=-1.
This loop should work correctly:
Do Until myRecSet.EOF
...
myRecSet.MoveNext
Loop

2a. It never hurts to put a breakpoint and step the code a little bit.

3. This is the wrong group intended for Classic VB. So I can be totally
wrong here. For .NET you have [much] better chance to get correct answer in
one of the groups with dotnet in its name.

Dmitriy.




"PGM" <pmatthews@xxxxxxx> wrote in message
news:%23vK90KBnIHA.980@xxxxxxxxxxxxxxxxxxxxxxx
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




.



Relevant Pages


Loading