Trying to populate a combobox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PGM

    Trying to populate a combobox

    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._ComObj ect" . 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.Masterserve r WHERE F2 like 't%'"

    strConn = "Provider=SQLOL EDB.1;Password= vbuser;Persist Security
    Info=True;User ID=VBuser;Initi al Catalog=Masters erver;Data
    Source=patmtest \sqlexpress"

    MyConnObj = New ADODB.Connectio n

    MyConnObj.Open( strConn)

    cmdselect = New ADODB.Command

    cmdselect.Activ eConnection = MyConnObj

    cmdselect.Comma ndText = strSQL

    myRecSet = New ADODB.Recordset

    myRecSet.Open(s trSQL, MyConnObj, ADODB.CursorTyp eEnum.adOpenKey set,
    ADODB.LockTypeE num.adLockOptim istic, ADODB.CommandTy peEnum.adCmdTex t)

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

    ComboBox1.Refre sh()

    myRecSet.MoveFi rst()

    For i = 1 To myRecSet.Record Count

    ComboBox1.Items .Add(myRecSet.F ields("F2"))

    Next i

    myRecSet.Close( )

    MyConnObj.Close ()

    myRecSet = Nothing

    MyConnObj = Nothing



  • Armin Zingler

    #2
    Re: Trying to populate a combobox

    "PGM" <pmatthews@chw. orgschrieb
    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._ComObj ect" . Any
    suggestions are greatly appreciated

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

    ComboBox1.Items .Add(myRecSet.F ields("F2"))
    You add a field. It's ToString method returns the data type, which is
    "System._ComObj ect". The combobox always displays the string returned by
    the object's ToString method.

    Use
    myRecSet.Fields ("F2").Value.To String
    instead.


    Armin

    Comment

    • Armin Zingler

      #3
      Re: Trying to populate a combobox

      "Armin Zingler" <az.nospam@free net.deschrieb
      Use
      myRecSet.Fields ("F2").Value.To String
      instead.
      Of course,
      myRecSet.Fields ("F2").Value
      works, too (because of the reason given before).


      Armin

      Comment

      • PGM

        #4
        Re: Trying to populate a combobox

        A novice at work

        "Armin Zingler" <az.nospam@free net.dewrote in message
        news:uM3fzXknIH A.4480@TK2MSFTN GP03.phx.gbl...
        "PGM" <pmatthews@chw. orgschrieb
        >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._ComObj ect" . Any
        >suggestions are greatly appreciated
        >
        >
        Is there a reason why you use COM Interop (ADO) instead of ADO.Net?
        >
        >
        >ComboBox1.Item s.Add(myRecSet. Fields("F2"))
        >
        You add a field. It's ToString method returns the data type, which is
        "System._ComObj ect". The combobox always displays the string returned by
        the object's ToString method.
        >
        Use
        myRecSet.Fields ("F2").Value.To String
        instead.
        >
        >
        Armin

        Comment

        Working...