运行时错误'-2147352571(80020005)类型不匹配

时间:2014-04-05 08:50:50

标签: excel vba

Run-time error '-2147352571(80020005) Type mismatch.AddItem rs(1)

Private Sub UserForm_Activate()
    If dbconn.State = adStateClosed Then dbconn.Open strConn

    Dim rs As New ADODB.Recordset

    ListBox1.Clear
    ListBox1.ColumnCount = 6
    rs.Open "select idClient,client_name,Address,City,state,country from  tblClients ", dbconn

    Do While rs.EOF = False
        i = i + 1
        With UserForm2.ListBox1
            .AddItem rs(1) '---------> getting ERROR HERE PLEASE LET ME KNOW
            .List(.ListCount - 1, 1) = rs(2)
            .List(.ListCount - 1, 2) = rs(3)
            .List(.ListCount - 1, 3) = rs(4)
            .List(.ListCount - 1, 4) = rs(5)
            .List(.ListCount - 1, 5) = rs(0)
        End With
        rs.MoveNext
    Loop
    rs.Close

    With cmbCountry
        .AddItem "United States"
        .AddItem "Canada"
        .AddItem "Germany"
        .AddItem "Australia"
    End With
    cmbCountry.ListIndex = 0
End Sub

2 个答案:

答案 0 :(得分:0)

我认为您应该在发生错误时启动调试器。然后在立即窗口中写@ECHO off TITLE REMOVE FILES @ECHO: SET /p _FileNumber= Enter file number You want to remove: del /q /s "C:\Directory1\*%_FileNumber%.*" del /q /s "C:\Directory2\*%_FileNumber%.*" PAUSE 。如果这引发错误,则rs(1)部分有问题,否则问题出在? rs(1)上。

在第一种情况下,请在监视窗口中监视rs,并确定其具有的属性。也许它没有默认属性,您可以在尝试.AddItem时通过该属性访问某些内容的第一个。我想您对某一领域感兴趣,所以我会尝试吗? rs(1)

在第二种情况下,rs(1)返回不可转换为字符串的内容。您可以尝试rs.Fields(1)来查看它是否可以转换,或在“监视”窗口中观看。但是从那里继续的方式取决于您找到的东西。

答案 1 :(得分:-1)

您不会显示数据库架构,但是从查看SQL看起来第一个字段似乎是数字(idClient)并且您尝试将其用于ListBox.AddItem,期待一个字符串。

所以,你只需转换如下:

With UserForm2.ListBox1
    .AddItem Str(rs(1))

    ' etc. ....

End With