Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS Access - If Not Rs.EOF Issue Loop issue

Posted on 2012-03-18
13
Medium Priority
?
655 Views
Last Modified: 2012-03-19
Once again I need help.  How is a better way to write this RS.EOF code?  WHat I need is sort of a loop, but its not looping really because it applies to the next record and different txt control on the form.  Basically it works now but if there is not five records then I get an error and it stops after the second instance if there are only two records in the recordset.  

    Set rs = db.OpenRecordset(strSql, dbOpenSnapshot)
    
        rs.MoveFirst
            Me!txtBdl1 = rs("inv_tagid") & " - " & rs("inv_rec_date")
            If Not rs.EOF Then
                    rs.MoveNext
                    Me!txtBdl2 = rs("inv_tagID") & " - " & rs("inv_rec_date")
                If Not rs.EOF Then
                    rs.MoveNext
                    Me!txtBdl3 = rs("inv_tagID") & " - " & rs("inv_rec_date")
                If Not rs.EOF Then
                    rs.MoveNext
                    Me!txtBdl4 = rs("inv_tagID") & " - " & rs("inv_rec_date")
                If Not rs.EOF Then
                    rs.MoveNext
                    Me!txtBdl5 = rs("inv_tagID") & " - " & rs("inv_rec_date")
                End If
            End If
        End If
    End If
        
        rs.Close
        Set rs = Nothing

Open in new window

0
Comment
Question by:pskeens
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37736518
Use an array to store the results instead of individual variables and a loop to get the data.  Something like this:

dim txtBdl(6), lcount
lcount = 0
do until rs.EOF
  txtBdl(lcount ) = rs("inv_tagid") & " - " & rs("inv_rec_date")
  lcount = lcount + 1
  rs.MoveNext
loop

Open in new window

0
 
LVL 2

Author Comment

by:pskeens
ID: 37736533
That did not work.  I did not get any errors, however nothing happened.
0
 
LVL 9

Expert Comment

by:Orcbighter
ID: 37736552
You need to move to the first record. I believe that is the statement that was missing.
Thus:

dim txtBdl(6), lcount
lcount = 0

rs.MoveFirst

while Not rs.EOF
  txtBdl(lcount ) = rs("inv_tagid") & " - " & rs("inv_rec_date")
  lcount = lcount + 1
  rs.MoveNext
End While
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 2

Author Comment

by:pskeens
ID: 37736559
That did not work either.  However I changed the "End While" to "Wend", but still did nothing

Dim txtBdl(6), lcount
lcount = 0

rs.MoveFirst

While Not rs.EOF
  txtBdl(lcount) = rs("inv_tagid") & " - " & rs("inv_rec_date")
  lcount = lcount + 1
  rs.MoveNext
Wend
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 37736651
You could count the records before entering the loop:
  Set rs = db.OpenRecordset(strSql, dbOpenSnapshot)
  If Not rs.EOF Then    
    rs.MoveLast
    If rs.RecordCount >= 5 Then    
        rs.MoveFirst
            Me!txtBdl1 = rs("inv_tagid") & " - " & rs("inv_rec_date")
        rs.MoveNext
            Me!txtBdl2 = rs("inv_tagID") & " - " & rs("inv_rec_date")
        rs.MoveNext
            Me!txtBdl3 = rs("inv_tagID") & " - " & rs("inv_rec_date")
        rs.MoveNext
            Me!txtBdl4 = rs("inv_tagID") & " - " & rs("inv_rec_date")
        rs.MoveNext
            Me!txtBdl5 = rs("inv_tagID") & " - " & rs("inv_rec_date")
    End If
  End If
  rs.Close
  Set rs = Nothing

Open in new window

/gustav
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37736653
I modified one of my ASP pages to test this and this is what works.  It also matches the code in my Access book as far as the Do While loop goes.  I did the second loop at the end to show that the data was in the array.

<%@ LANGUAGE = VBScript %>
<%  Option Explicit		%>

<html>
    <head>
        <title>Dave's Simple ADO Query on Musician's 2 MDB</title>
		<style type="text/css">
<!-- 
td { background-color: white; } 
-->
</style>
    </head>

    <body bgcolor="White" topmargin="10" leftmargin="10">

		<!-- Display Header -->

		<font size="4" face="Arial, Helvetica">
		<b>Simple ADO Query with ASP</b></font><br>
    
		<hr size="1" color="#000000">

		Contacts within the Musicians Database:<br><br>

		<%
			Dim oConn		
			Dim oRs		
			Dim filePath		
			Dim Index
			Dim txtBdl(6), lcount		
			
			' Map authors database to physical path
			filePath = Server.MapPath("Musicians2.mdb")

			' Create ADO Connection Component to connect
			' with sample database	

			Set oConn = Server.CreateObject("ADODB.Connection")
			oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filePath
			
			' Execute a SQL query and store the results
			' within recordset
			
			Set oRs = oConn.Execute("SELECT ID, FirstName, LastName, HomePhone From Addresses")
		%>

		<table border="0" cellpadding="1" cellspacing="1" summary="" bgcolor="#808080">
		<%  
			lcount = 0
			oRs.MoveFirst
			
			Do while ((Not oRs.eof) AND (lcount < 6)) %>

				<tr>
					<% txtBdl(lcount) = oRs.Fields("FirstName").Value & " - " & oRs.Fields("LastName").Value %>
						<td valign=top>&nbsp;<% = txtBdl(lcount)%></TD>
					<% 
  					lcount = lcount + 1 %>
				</tr>
            
				<% oRs.MoveNext 
			Loop 
		%>

		</table>

		<%   
			oRs.close
			oConn.close
			lcount = 0
			%>
			<br>
		<%
			Do While (txtBdl(lcount) <> "") AND (lcount < 6)
				Response.write txtBdl(lcount) & " :: "
				lcount = lcount + 1
			Loop				  			 
		%>
		

</body>
</html>

Open in new window

0
 
LVL 2

Author Comment

by:pskeens
ID: 37737328
Thanks.  Counting the records before does not help.  Sometimes all 5 text boxes will be filled but then there will be times that only one, two, or even none of them will be.  This is why I was trying to use the loop through.  

Dave I will give this a shot and see what happens.
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 37737367
> Counting the records before does not help.  
> Sometimes all 5 text boxes will be filled but then there will be times
> that only one, two, or even none of them will be.  

??
What does it matter if the textboxes are filled? Your original code doesn't care.
And why does counting not help? A count of 4 or less was exactly the cause of your trouble.

Please elaborate.

/gustav
0
 
LVL 18

Expert Comment

by:Sham Haque
ID: 37737497
How about this?
rather than re-iterating over the different text boxes by name, use an incrementing variable

rs.MoveFirst

Dim i

i = 1
Do While Not rs.EOF
Me!txtBdl & i = rs.Fields(0).Value & " - " & rs.Fields(1).Value
i = i + 1
rs.MoveNext
Loop

Open in new window

0
 
LVL 2

Author Comment

by:pskeens
ID: 37737589
gbshahaq that did not work.  

Gustav:  The code above on counting records means that it will only go through the movenext steps IF there are more than 5 records, right?   I need it to complete the steps until it runs out of rows of data up to 5.
0
 
LVL 51

Accepted Solution

by:
Gustav Brock earned 2000 total points
ID: 37737636
OK. Then use code like gb's

Dim intI As Integer
intI = 1
While Not rs.EOF
  Me("txtBdl" & intI & "").Value = rs.Fields(0).Value & " - " & rs.Fields(1).Value
  intI = intI + 1
  rs.MoveNext
Wend
rs.Close

Open in new window

/gustav
0
 
LVL 2

Author Closing Comment

by:pskeens
ID: 37739713
BINGO!  We have a working code.  

I cant thank ALL OF YOU enough!  Thanks for hanging with me on this.
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 37739890
You are welcome!

/gustav
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question