Question getting 2 data or more from table with same condition using loop

Joined
Jun 17, 2014
Messages
7
Programming Experience
Beginner
hi,
so i want to get 2 data or more from table with same condition using loop
here is my table

upload.png

and here is my code

 Private Sub COBAPANGGIL()
        Conn()
        Dim status = "Ready"
        rsSet.Open("Select * from tblDataStatus where status ='" & status & "'", Con, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
        Dim COBAARRAY() As String
        Dim i As Integer = 0
        For i = 0 To rsSet.EOF
            COBAARRAY(i) = rsSet.Fields.Item("ID").Value
            MsgBox(COBAARRAY(i))
            rsSet.Close()
            rsSet.Open("Select * from tblDatadiri where ID ='" & COBAARRAY(i) & "'", Con, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
            Dim COBA = rsSet.Fields.Item("Nama").Value
            MsgBox(COBA)
            MsgBox(COBAARRAY + COBA)
            rsSet.MoveNext()
        Next
end sub


but when i try it, it get error like this

Untitled.png


help me please, thank you :))
 
Last edited by a moderator:
First things first, why are you using ADO at all? You're not using VB6 so why use VB6 data access technology? You're using VB.NET so you should be using .NET data access technology, i.e. ADO.NET.
 
well it's because most people in my place still using ADO, can you help me please?

Does that mean that you can use ADO.NET or you can't because, if you can, you should. There's really no good reason for using ADO in a VB.NET app unless it is an upgraded VB6 app and that includes familiarity. If it's new VB.NET code then it should use ADO.NET. If you can, do.

As for the issue, I don't use ADO so I'm not sure of all the details but it seems obviously bad practice to me to close a Recordset and open a new inside a loop that is supposed to be looping through that Recordset.

Regardless of that, now that I look at your code again I see that the issue really has nothing to do with the data access at all. You are declaring an array variable but never creating an array object, yet trying to set an element of that array by index. If the array doesn't exist then you can't set an element.
 

Latest posts

Back
Top