Add subform record count to text box?

uscarioca

New Member
Joined
Jul 7, 2004
Messages
16
Private Sub cmdSearch_Click()
Me.Refresh
Me.txtRegInfo = "Stores are in Region " & cmbRegion & " (" & cmbState & ")"
End Sub

Can someone help me understand how I can insert a record count variable from a subform qry? My qry populating my subform is called qryPL_Store_List. Lets say qryPL_Store_List has 37 records, I would want txtRegInfo = "37 Stores are in Region 26 (MO)".

Please note that I have got the cmbRegion and cmbState variables to work in the text box. I am stumped on the getting the record count.

Any help would be appreciated.
Thanks Chris
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

timothybard

Board Regular
Joined
Oct 8, 2008
Messages
112
You can try something like this:

Code:
  Dim cnn As ADODB.Connection
  Dim rst As ADODB.Recordset
  Set cnn = CurrentProject.Connection
  Set rst = New ADODB.Recordset
  Dim recordCount As Long

  rst.Open Me.subform.Form.RecordSource, cnn
  While Not rst.EOF
    rst.MoveNext
    recordCount = recordCount + 1
  Wend
  rst.Close
  
  Me.txtRegInfo= recordCount & " Stores are in Region " & cmbRegion & " (" & cmbState & ")"

Replace 'subform' with the name of your subform.
 

Watch MrExcel Video

Forum statistics

Threads
1,132,668
Messages
5,654,638
Members
418,147
Latest member
Shnn028

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
Top