Counting records of linked table over local server.

mrmarc

Board Regular
Joined
Feb 3, 2014
Messages
79
Here is what I have tried.
Code:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim i As Integer
Dim x As String


Set db = OpenDatabase("S:\Traceability V2\Shore Fresh Logistics_be_be.accdb")

Set rs = db.OpenRecordset("tblPalletRecords", dbOpenTable)




For i = 0 To rs.RecordCount - 1
    
    
If IsNull(rs.Fields("PalletNumber")) = True Then
    
    GoTo NextOne1
    
   Else
    
    x = rs.Fields("PalletNumber")
       
            
    If x = Me.PalletNumber.Value Then
            
            Me.Status.Value = rs.Fields("Status")
    
          
    End If
        
            
End If
        
NextOne1:
        rs.MoveNext
        
    Next i


rs.Close
Set rs = Nothing
db.Close

I have 7 records in my linked table and it's only counting 4. Any ideas?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Where exactly in this code is any counting occurring?

Note that the code is checking the value of the "PalletNumber" field, and if it is null, it is skipping it.
 
Upvote 0
Hi Joe,

It's counting when "For i = 0 to rs.RecordCount -1". Basically I am trying to count ALL records so that my loop knows how many to search.

And yes, there are no Null values in the PalletNumber field.
 
Upvote 0
Thanks Joe.
My records count is now working. BUT now im getting a "No Current Record" error.
Code:
Set db = OpenDatabase("S:\Traceability V2\Shore Fresh Logistics_be_be.accdb")

Set rs = db.OpenRecordset("tblPalletRecords", dbOpenTable)


If rs.EOF Then
      z = 0
   Else
      rs.MoveLast
End If




For i = 0 To rs.RecordCount - 1


    
If IsNull(rs.Fields("PalletNumber")) = True Then
    
    GoTo NextOne1
    
   Else
    
    x = rs.Fields("PalletNumber")
       
            
    If x = Me.PalletNumber.Value Then
            
          
            Me.Status.Value = rs.Fields("Status")
    
          
    End If
        
            
End If
        
NextOne1:
        rs.MoveNext
        
    Next i

Any Ideas?
 
Upvote 0
If you are going to loop through all your records after your move to the end (to get your count), you will need to move back to the beginning.
So before your "For ..." loop, you will need a ".MoveFirst" command.

If you do a Google search on "Access VBA loop through recordset", you can see different methods of looping through recordsets in Access VBA.
 
Upvote 0
Joe. Thank you so much. Its working now. I cannot tell you how much I appreciate people like you who are will to help.

Here is my final code for anyone else experiencing the same problem:

Code:
Set db = OpenDatabase("S:\Traceability V2\Shore Fresh Logistics_be_be.accdb")

Set rs = db.OpenRecordset("tblPalletRecords", dbOpenTable)


If rs.EOF Then
      z = 0
   Else
      rs.MoveLast
      rs.MoveFirst
      z = rs.RecordCount
End If


For i = 0 To z - 1


    
If IsNull(rs.Fields("PalletNumber")) = True Then
    
    GoTo NextOne1
    
   Else
    
    x = rs.Fields("PalletNumber")
       
            
    If x = Me.PalletNumber.Value Then
            
            Me.Status.Value = rs.Fields("Status")
    
          
    End If
        
            
End If
        
NextOne1:
        rs.MoveNext
        
    Next i


rs.Close
Set rs = Nothing
db.Close
 
Upvote 0
Yeah, recordsets can be a little tricky to work with if you are not used to them.
Glad we got it working!
 
Upvote 0
Joe,

After a few more entries, I'm having the same problem.

There are 13 records in the table but z is only equal to 10.

It seems like it's not recognizing the recent entries.
 
Upvote 0
There is no need to count records. Just begin your loop:
Code:
Set db = OpenDatabase("S:\Traceability V2\Shore Fresh Logistics_be_be.accdb")

Set rs = db.OpenRecordset("tblPalletRecords", dbOpenTable)

If Not rs.EOF Then
	Do While Not rs.EOF
		
		If Not IsNull(rs.Fields("PalletNumber")) Then
		
			If rs.Fields("PalletNumber") = me.PalletNumber.Value Then
				Me.Status.Value = rs.Fields("Status")
			End If
			  
		End If
		
		rs.MoveNext
	
	Loop
End If
        

rs.Close
Set rs = Nothing
db.Close

YOu probably shouldn't even be looping. The effect of this loop is that if you find a "match" and then another one, the first get's wiped out so it's pointless. If there is only one "match" to find, query for it directly:
Code:
dim sql As String
sql = "SELECT Status FROM tblPalletRecords WHERE PalletNumber = " & Me.PalletNumber.Value
set rs = db.OpenRecordset(sql)
If Not rs.EOF Then
    Me.Status.Value = rs.Fields("Status")
End if
rs.Close
set rs = Nothing
db.Close
 
Upvote 0

Forum statistics

Threads
1,221,893
Messages
6,162,662
Members
451,781
Latest member
DylantheD

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top