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?
 
Thanks xenou!

What I ended up doing is creating a local query that retrieved all the fields from the linked table. I am now using it like this:

Code:
Set db = CurrentDb

Set rs = db.OpenRecordset("qryStatusFind")




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
            MsgBox "Success"
            Me.Status.Value = rs.Fields("Status")
    
          
    End If
        
            
End If
        
NextOne1:
        rs.MoveNext
        
    Next i


rs.Close
Set rs = Nothing
db.Close

Do you see a problem with doing it this way?

And Yes, sometimes there are multiple "PalletNumbers" that need the information changed.

Thanks in advance.
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
The only problem I see is that (i.e., let's say you have two pallet numbers in the query that are equal to Me.PalletNumber.value):

first the loop encounters the first match and changes the value of me.status (fine)
second the loop encounters the second match and changes the value of me.status (again)

Now the first change is lost. So the code as written is doing unnecessary work, isn't it?
 
Upvote 0
Negative.

The first change is not lost. Both records are changed to the same status, which is what i want.

Thanks Xenou! Let me know if see anything else.
 
Upvote 0
Okay. Still no need for a loop though. Just query for the records you want, not all records and then looping through all records.
 
Upvote 0

Forum statistics

Threads
1,221,888
Messages
6,162,623
Members
451,778
Latest member
ragananthony7911

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