EOF returns true even if i have a populated record

2019-07-04 16:47发布

问题:

Im trying to get rows from the columns in a recordset and then insert those in a table plain and simple.

The recordset is populated and i used .MoveFirst to start at the beginning of the rs, Still i get EOF true at the very start and it jumps out of the do while..

I have a similar function woorking but this one won't woork for some reason.

I can't figureout why... or how to fix this. Anny insight is welcome!

current Code ~

Public Function makeSäljare()
'Create rs
Dim rsData As ADODB.Recordset
Set rsData = New ADODB.Recordset
Dim sql As String

'Select what should be included in the rs.
rsData.Open "SELECT Forhandler, Selger FROM data", _
CurrentProject.Connection, adOpenDynamic, adLockOptimistic
rsData.MoveFirst


MsgBox rsData.GetString

'Manipulate each row of the result column.
Do While Not rsData.EOF


sql = "INSERT INTO säljare (Partner_Namn, Namn ) VALUES ('" & rsData!forhandler & "','" & rsData!Selger & "');"
MsgBox sql
'DoCmd.SetWarnings (False)
DoCmd.RunSQL (sql)
'DoCmd.SetWarnings (True)


rsData.MoveNext
'If rsData.EOF Then Exit Do

Loop

rsData.Close

End Function

It jumps out at Do While Not rsData.EOF..

回答1:

GetString leaves the recordset at EOF. MoveFirst again before Do While Not rsData.EOF

rsData.MoveFirst
MsgBox rsData.GetString
rsData.MoveFirst ' <-- add this
'Manipulate each row of the result column.
Do While Not rsData.EOF


回答2:

I didn't try your code, but this logically means that the recordset is empty. Check carefully if the query you executed on the db is correct.

In Access, use DAO instead of ADO and try this:

Set db = CurrentDb
set rsData = db.OpenRecordset("SELECT Forhandler, Selger FROM data", dbOpenDynaset)