Making a count record function work

Skip Bisconer

Who Me?
Local time
Today, 10:51
Joined
Jan 22, 2008
Messages
285
I would like to get two table record count results to two unbound textboxes on a form. My need is to compare record counts on two tables to make sure they are the same before proceeding with a month end process.

I have located a function through this site that is suppose to return a record count of a table however I don't seem to be able to get it to return anything but errors using the click event of a command button. Obviously I don't know how to use this function. Can some one show me the correct way to use this function in a sub routine?

Thanks for looking at my problem

Code:
Public Function Table_RecordCount(sTable As String) As Long
    Dim rs As DAO.Recordset
 
    Set rs = CurrentDb.OpenRecordset(sTable)
 
    If Not (rs.EOF) Then
        rs.MoveLast
        Table_RecordCount = rs.RecordCount
    End If
 
    Set rs = Nothing
End Function
 
What error do you get? How are you calling it? I would include an "Else" to set the return value to zero if the recordset is empty.
 
One of the things I tried was caused a variable not defined.

Private Sub Command42_Click()
Dim rs As DAO.Recordset
Set rs = CompletedMonthlyTransaction

Table_RecordCount

End Sub
 
Try simply this in the command button:

MsgBox Table_RecordCount "TableName"
 
Exactly what is your code now and what line throws the error?
 
Sub command42_click()
msgbox Table_RecordCount "CompletedMonthlyTransaction"
'MsgBox Table_RecordCount "TableName"
End Sub
 
Sorry, in the message box you'd need parentheses. Try:

MsgBox Table_RecordCount("CompletedMonthlyTransaction")
 
Thanks Paul that worked well. Can you tell how I can put this result in a text box? I tried me.textbox.value = Table_RecorcdCount("CompletedMonthlyTransactions") in the before update event and it didn't return anything.
 
I just tested that and it worked fine (I tried it behind a button and in the before update event of another control). Can you post a sample?
 
It works fine for me on a button but when I use this code on an unbound textbox called CountRecords and open the form there the textbox is blank.
I have attached the images in Design view and Form View. The table has 326 records posted to it at this point.

Code:
Private Sub CountRecords_BeforeUpdate(Cancel As Integer)
Me.CountRecords.Value = Table_RecordCount("CompletedMontlyTransaction")
End Sub
 

Attachments

  • frmPostandClear.jpg
    frmPostandClear.jpg
    48.4 KB · Views: 129
  • frmPostandClearFormView.jpg
    frmPostandClearFormView.jpg
    34.5 KB · Views: 121
You have it in the update event of the textbox itself, which won't fire until the user enters something in that textbox. If you want the box populated when you open the form, try the load event of the form.
 
No problem, Skip. Glad we got it working for you.
 

Users who are viewing this thread

Back
Top Bottom