Results 1 to 10 of 10

Thread: [RESOLVED] Error loading records into ListView

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    414

    Resolved [RESOLVED] Error loading records into ListView

    I'm trying to load two ListView controls with both listing the same entries, one ListView for tests to include and one for tests to exclude. Both the ListViews will only contain 1 column listing the tests.

    I load a recordset from my table to be loaded into the ListViews with CheckBoxes enabled. My recordset contains the correct records (I checked) but when trying to add a record to the ListViews I get 'Runtime Error 13: Type mismatch' on the line in red
    Code:
        Dim itmXIn As ListItem
        Dim itmXEx As ListItem
    
        mySQL = "SELECT * FROM PRG_Options WHERE OPT_Section = 'Medical' AND OPT_Combo = 'TestType'"
        rsTestType.Open mySQL, ADO_UserData, adOpenKeyset, adLockOptimistic
    
        LvwInclude.ListItems.Clear
        LvwExclude.ListItems.Clear
    
        Do While Not rsTestType.EOF()
          Set itmXin = LvwInclude.ListItems.Add(, , rsTestType("OPT_Option").Value)
          Set itmXEx = LvwExclude.ListItems.Add(, , rsTestType("OPT_Option").Value)
    
          rsTestType.MoveNext   ' Move to next record.
        Loop
        
        rsTestType.Close
        Set rsTestType = Nothing
        Set itmXin = Nothing
        Set itmXEx = Nothing
    What am I missing/doing wrong? Can't even get it loaded which should be the easy part

    My idea is when a test(s) is selected in one ListView to disable the same test in the other ListView as the same test can't be included and excluded at the same time.

    I'm stumped. Any help will be appreciated.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,368

    Re: Error loading records into ListView

    What is the value of that field at the time the error occurs?
    What data type is the field in the DB?
    Is the value Null?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    414

    Re: Error loading records into ListView

    It's a String in the database e.g. Circovirus or PBFD Virus.

    The value is not NULL. While trying to figure out why the error occurred I put a message box on the line before the error line showing the value and it's correct.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,368

    Re: Error loading records into ListView

    I do not see anything wrong with the line that adds to the listview and if it is a text field in the db then there should only be a type issue if the field has a null value in it.

    That said I see you are using a select * but only using 1 field from the table. You should be selecting only that field since that is all you are using and personally I would use the index number of that field rather than the name in the assignment as using the number is slightly faster but I would not think either of those things are related to your error message.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    414

    Re: Error loading records into ListView

    If I put On Error Resume Next before the Do While loop it loads the records into the ListView. I can see them in the ListView but if I select one of them the check mark in the checkbox appear and the program crash with 'Runtime error 35603: Invalid key'.

    So I added the same value in the key parameter but same error happens.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,183

    Re: Error loading records into ListView

    as you do not use the listitem objects, try adding the items to the listview without setting to the listitem
    also to avoid any problem with empty record fields, try add a nullstring

    Code:
    LvwInclude.ListItems.Add , , vbnullstring & rsTestType("OPT_Option").Value
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    414

    Re: Error loading records into ListView

    Quote Originally Posted by westconn1 View Post
    as you do not use the listitem objects, try adding the items to the listview without setting to the listitem
    also to avoid any problem with empty record fields, try add a nullstring

    Code:
    LvwInclude.ListItems.Add , , vbnullstring & rsTestType("OPT_Option").Value
    Loading them like this does load without error but still errors (Run-time error 35603: Invalid key) when I click on the checkboxes. No error when I click on the words themselves.

  8. #8
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    4,074

    Re: Error loading records into ListView

    How is the Listview initialized?
    Which properties have you set?

    What code do you have in the ListView1_ItemCheck event?
    Code:
    Option Explicit
    
    Private Sub Form_Load()
      With ListView1
        .Checkboxes = True
        .View = lvwReport
        .ColumnHeaders.Add , , "Column 1"
        .ColumnHeaders.Add , , "Column 2"
        .ColumnHeaders.Add , , "Column 3"
      End With
    End Sub
    
    Private Sub Command1_Click()
      With ListView1.ListItems.Add(, , "Yoohoo!")
        .SubItems(1) = "What's up?"
        .SubItems(2) = Time
      End With
    End Sub
    
    
    Private Sub ListView1_ItemCheck(ByVal Item As MSComctlLib.ListItem)
    ' what code do you have here??
    End Sub
    Last edited by Arnoutdv; Nov 24th, 2020 at 06:27 AM.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    414

    Re: Error loading records into ListView

    Added to the form and set the Checkboxes property to True and the View property to Report. But I only have one column so no SubItems.

    I thought I had no code in ItemCheck yet but see I have tried to access the ListView.ListItems.Item(Item).Checked property. Going to disable the ItemCheck and see what happens then.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    414

    Re: Error loading records into ListView

    Ok. I see that the Invalid key error message is generated by ListView.ListItems.Item(Item).Checked. Will have to look into that next.

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