ADO离开加入问题

时间:2017-08-18 21:49:00

标签: sql vb6 ado

我想做一个LEFT JOIN如下:

sSQL10 = "SELECT workers.WorkerID, Assignements.* From (workers "
sSql11 = "LEFT JOIN Assignements "
sSQL12 = " ON workers.WorkerID = Assignements.WorkerID) WHERE Assignements.opendate111=#" & Me.OpenDate & "#"
sSQL = sSQL10 & sSql11 & sSQL12
rs_worker.Open sSQL, cn, adOpenStatic, adLockOptimistic
With rs_worker
    Do While Not .EOF
        MsgBox .Fields("WorkerID").value
        .MoveNext
    Loop
End With

虽然rs_worker有一条具有WorkerID值的记录,但msgbox会返回错误。

MsgBox .Fields(1).value

确实返回一个值,。field(1).name显示为 assignements.WorkerID ,而我希望能够将其引用为 rs_worker!WorkerID

2 个答案:

答案 0 :(得分:1)

字段(1)不是workers.WorkerID,因为它是基数0.将来在访问字段时不要使用索引。使用Fields.Items("FieldNameHere").Value

请看Effecient way to access field

答案 1 :(得分:0)

rs_worker.Open sSQL, cn, adOpenStatic, adLockOptimistic
With rs_worker
    If NOT.BOF Then
        .MoveFirst
        Do While Not .EOF
            MsgBox .Fields(0).value
            .MoveNext
        Loop
    Else
        msgbox "no records found"

    End If
End With