docmd running before it's supposed to... (1 Viewer)

mikewood1980

Registered User.
Local time
Today, 16:50
Joined
May 14, 2008
Messages
45
Hi

I have the following code:

Code:
Dim rsWallCons As Recordset
            
            sQuery = "select * from tblWallConstructions where ((tblWallConstructions.chrTestID) like '*" & Me.cboReportID & "*')"
            
            Set rsWallCons = dbcurr.OpenRecordset(sQuery)
            
            MsgBox ("matching wall constructions = " & rsWallCons.RecordCount)
            
            rsWallCons.MoveFirst <<<<<CRASHES HERE !!!!
             Do Until rsWallCons.EOF
                        'delete linings
                        DoCmd.RunSQL ("delete * from tblLining where ((tblLining.intLiningID) = " & rsWallCons!intLiningID1 & ")")
                            
                            
                        DoCmd.RunSQL ("delete * from tblLining where ((tblLining.intLiningID) = " & rsWallCons!intLiningID2 & ")")
                        MsgBox ("should have deleted linings...")
                        
                        'delete wall leaves
                        DoCmd.RunSQL ("delete * from tblWallLeaves where ((tblWallLeaves.intWallLeavesID) = " & rsWallCons!intLeafID1 & ")")
                        DoCmd.RunSQL ("delete * from tblWallLeaves where ((tblWallLeaves.intWallLeavesID) = " & rsWallCons!intLeafID2 & ")")
                                      
                'then delete that record...
                rsWallCons.Delete
                rsWallCons.MoveNext
            Loop

The code crashes at the point marked (rsWallCons.MoveFirst) - the message box above shows 0 records. So far, this makes sense, except that there should be records in rsWallCons. Even more strangely, the records that should be in there are deleted successfully by the following SQL statements. Given that the code doesnt get this far I don't know how this happens???! Does anybody know why this might be - I'm completely confused!

Just to recap - I need to now a) why there are apparently no records in rsWallCons b) why the SQL statements execute before they should and c) if the are no records in rsWallCons - how do the SQL statements delete the right records??!

Thanks in advance for your help

Mike:confused:
 

KenHigg

Registered User
Local time
Today, 11:50
Joined
Jun 9, 2004
Messages
13,327
a. I think you have to traverse the entire recordset by doing a .movelast before the count will display the correct number.
 

MarkK

bit cruncher
Local time
Today, 08:50
Joined
Mar 17, 2004
Messages
8,187
If RecordCount is zero or EOF is true when you open a recordset, then there are no records. In your case it must be that your SQL is not selecting any.
What I would do is cut and paste this ...
Code:
"select * from tblWallConstructions where chrTestID like '*YourCBOData*')"
... where 'YourCBOData' is a value from the combo you expect should yield records, cut and paste this into a Query and troubleshoot it there.

Also, when you open a DAO.Recordset, if there is a first record it is current, so there's never an immediate need to explicitly MoveFirst.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 16:50
Joined
Sep 12, 2006
Messages
15,710
if there might be no records then before you do a

rst.movefirst

either
a) set an error handler to detect the errror

or

b) if rst.eof then 'same thing - you are already at end of file, so move operation will fail

its similar if you try to do an indexed read

rst.findfirst "expression"

in this case you need to do a

if rst.nomatch to see if read succeeded or not

but rst.nomatch doesnt work until you do a rst operation, so it isnt available in your case.
 

boblarson

Smeghead
Local time
Today, 08:50
Joined
Jan 12, 2001
Messages
32,059
Also, you should explicitly declare your recordset objects so instead of:

Dim rsWallCons As Recordset

you should do

Dim rsWallCons As DAO.Recordset
 

Users who are viewing this thread

Top Bottom