View Full Version : Change RecorSource Property Of Subform inside Main Form


hallian92
05-09-2002, 06:56 AM
Hi,

Is there any way I can change the Record Source Property of my subform inside the Main Form programmatically. I have a subform inside my Main Form. What I want is whenever I click a Button that is inside my Main Form I want to change the Record Source Property of my subform. At design time I have set the following as follows:

Main Form:
Record Source = query1

subform:
Record Source = query1

When I click the Button inside my Main Form at run-time I want to change the Record Source Property as follows:

sub Button_Click()
Dim rstSerialNumbers As Recordset
Dim dbsSerialNumbers As Database
Dim qdfSerialNumbers As QueryDef
Dim strSQLSerialNumbers As String
Set dbsSerialNumbers = CurrentDb
Set qdfSerialNumbers = dbsSerialNumbers.CreateQueryDef("")
strSQLSerialNumbers = strSQLSerialNumbers & "SELECT * FROM [Items] WHERE "
strSQLSerialNumbers = strSQLSerialNumbers & "Ship Like " & "'*" & intItemIDTable & "*' And Shed=Yes "
strSQLSerialNumbers = strSQLSerialNumbers & "ORDER BY ItemID DESC; "
qdfSerialNumbers.sql = strSQLSerialNumbers
Set rstSerialNumbers = qdfSerialNumbers.OpenRecordset

'Here I have change the Record Source Property Of the Main For

Me.RecordSource = strSQLSerialNumbers

'Now I also want to cganhe the Record Source of the subform

Me.[subform].RecordSource = strSQLSerialNumbers
................
................

Can I change the subform Record Source Property if not is there any way I can achieve this. Thanks for any help.
I tried to use this but gives me an error

Me.<subformName>.RecordSource = SQLSTr

Thanks for any help.

Pat Hartman
05-09-2002, 05:18 PM
The following is code I use to set the recordsource in the subform's Open event:

Private Sub Form_Open(Cancel As Integer)
Dim strSQL As String
Dim strFilter As String
strFilter = "ERCID = " & Forms!subfrmqryERCDetailLOB!txtERCID
strFilter = strFilter & " AND ERCGroupLOB = " & Forms!subfrmqryERCDetailLOB!txtERCGroupLOB
strFilter = strFilter & " AND ERCDetailLOB = " & Forms!subfrmqryERCDetailLOB!txtERCDetailLOB
strFilter = strFilter & " AND KindOfReinsuranceCd = " & Forms!subfrmqryERCDetailLOB!txtKindOfReinsuranceCd
strSQL = "SELECT L.ERCID, L.ERCGroupLOB, L.ERCDetailLOB, L.KindOfReinsuranceCd, L.Layer, "
strSQL = strSQL & "L.Attachment, L.Limit, L.CreatedBy, L.CreatedDt, "
strSQL = strSQL & "L.UpdatedBy, L.UpdatedDt FROM dbo_ERCLayer AS L WHERE "
strSQL = strSQL & strFilter & ";"
Me.RecordSource = strSQL
End Sub