CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Question Saving listview data using addtolistview method

    Can anyone tell me .why i am getting object variable or with block variable not set.Kindly help me.any help would be Greatly appreciated.here is the code what i have written.here is the written code.
    Code:
    Public Sub Addtolistview()
    Dim con As adodb.Connection, rs As adodb.Recordset
    Set con = New adodb.Connection
    Call OpenConnection(con)
    If Not OpenConnection(con) Then
       MsgBox ("cannot open connection")
       Set con = Nothing
    End If
    'con.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=\\asfserver\itp$\Product_tabletest.mdb")
    'Call OpenConnection(con)
    Set rs = New adodb.Recordset
    rs.CursorLocation = adUseClient
    'strSql = "Select"
    'strSql = strSql & "[sup_name],[fax_no],[contact_person],[contact_no],[Office_address] from [Suppliers]"
    strSql = "SELECT Suppliers.sup_id, Suppliers.sup_name, ItemTypes.item_Type, Suppliers.Fax_No, Suppliers.Contact_Person, Suppliers.Contact_No, Suppliers.Office_Address, Suppliers.Email, Suppliers.Website FROM ItemTypes INNER JOIN Suppliers ON ItemTypes.Type_ID = Suppliers.Type_ID;"
    rs.Open strSql, con, adOpenDynamic, adLockOptimistic
    ListView1.View = lvwReport
    'ListView1.BorderStyle = ccFixedSingle
    ListView1.FullRowSelect = True
    ListView1.BorderStyle = ccNone
    'checking the width of listview control and divided by 5...so that all columns become of same width...here we used 5 because the control will have 5 columns
    colWidth = ListView1.Width / 5
    ListView1.ListItems.Clear
    
    'here we are adding the columns
    'you need similar no. of columns as much no. of listitems you want to add to the listview. now if you want to hide any column from displaying, just set its width to 0.
    
    'adding rows to listview
     With ListView1
    '   .ListItems.Clear
        If rs.RecordCount > 0 Then
            rs.MoveFirst
            While Not rs.EOF()
                Dim faxno, contactNo, officeAddress As String
                faxno = IIf(IsNull(rs!fax_no), "", rs!fax_no)
                contact = IIf(IsNull(rs!contact_person), "", rs!contact_person)
                suppliername = IIf(IsNull(rs!sup_name), "", rs!sup_name)
                contactNo = IIf(IsNull(rs!Contact_no), "", rs!Contact_no)
                officeAddress = IIf(IsNull(rs!office_address), "", rs!office_address)
                Set listitem = ListView1.ListItems.Add(, , supplier_name)
                listitem.Text = IIf(IsNull(rs!sup_name), "", rs!sup_name)
                listitem.SubItems(1) = rs.Fields("fax_no").Value & vbNullString
                listitem.SubItems(2) = rs.Fields("contact_person").Value & vbNullString
                listitem.SubItems(3) = rs.Fields("contact_no").Value & vbNullString
                listitem.SubItems(4) = rs.Fields("Office_address").Value & vbNullString
                rs.MoveNext
               Call CloseRecordset(rs)
            Wend
        End If
    End With
    Call CloseConnection(con)
    End Sub
    Last edited by firoz.raj; June 21st, 2009 at 07:30 AM.

  2. #2
    Join Date
    Jan 2000
    Location
    Saskatchewan, Canada
    Posts
    595

    Re: Saving listview data using addtolistview method

    I think you are getting that message because you are dim your connection and recordset within the AddtoListview sub and it is private to that sub. They are not dim'ed when you gosub the openconnection sub. You should dim them in the general declarations area. (at the top of your program)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured