Question : How to assign a control of a subform equal to the control id that is an autonumber on the main form.

I also want to autopopulate a text box on my subform base on a combo box selection, and when the text box get’s auto-populated a new line get’s added for a new selection and the changes are stored for that particular control id on the main form. I want to be able to do this using ADO recordsets.

My situation is as follows: I'm building a db tool for auditors to audit claims on. The claim is displayed on the main form, and an auditor will assign errors by using the subform's cbo box.  

Table setup:
AuditTbl                                     D_ErrorsTbl
audit_id - Pk, Autonumber;       error_id - PK, Autonumber
claim_id                                    error_code
                                                 error_description
                                                 audit_id - FK

-Main form: (D_AuditFrm) with recordset base on D_AuditTbl.
Text box: txtaudit_idmain with control source "audit_id" and txtclaim_id with control source claim_id.
-Subform: (D_ErrorsTblSubform) with recordset base on D_ErrorsTbl:
Controls: txterror_id, txterror_description, cboerror_code and txtaudit_idsub

My first question is how do I assign txtaudit_idsub on subfom to be the same as audit_id on the main form using vba?

Second situation is, I’m using dlookup for my subform and it’s auto-populating a control text box on my subform correctly, but that’s it. What I need it to do is to add another line, for a new selection for auditors, just in case there’s another error, while keep in mind the txtaudit_idsub get’s also populated with the same txtaudit_idmain on the main form.

Design of my form:
Main Form                     
       txtaudit_idmain              
       txtclaim_id
Subform                   
txterror_id      txterror_description      cboerror_code       txtaudit_idsub

'>>> Main Form
Private Sub Form_Open(Cancel As Integer)
Dim rst As ADODB.Recordset
Dim strSQL As String
Set rst = New ADODB.Recordset

strSQL = "SELECT AuditTbl.*" & _
"FROM AuditTbl"

rst.CursorLocation = adUseClient  'Enable's form to be editable.
rst.Open strSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic  

Set Me.Recordset = rst    '<-----  This sets the form's recordset property to your updateable recordset

Me.txtaudit_idmain.ControlSource = "audit_id"
Me.txtclaim_id.ControlSource = "claim_id"

End Sub

'>>>  Subform
Private Sub Form_Open(Cancel As Integer)
Dim rst1 As ADODB.Recordset
Dim strSQL1 As String
Set rst1 = New ADODB.Recordset

strSQL1 = "SELECT D_ErrorsTbl.*" & _
"FROM D_ErrorsTbl"
 
rst1.CursorLocation = adUseClient  'Enable's form to be editable.
rst1.Open strSQL1, CurrentProject.Connection, adOpenDynamic, adLockOptimistic  

Set Me.Recordset = rst1    

End Sub

Answer : How to assign a control of a subform equal to the control id that is an autonumber on the main form.

Sorry about the timeliness of  your response, I've been travelling and have not been able to get online often.

The ONLY thing you need to do to show your records on the subform is to set the Recordset in the CURRENT event of your MAIN form. You don't need to do anything in the Subform in order to show those records. I would strongly suggest you get this working first, then worry with other things.

It's pretty straight forward - whenever your mainform record changes, you need to change the recordset of your subform. The mainform's Current event is the best way to do this. You can use the Open event of the mainform to set the "main" recordset, but you then must also use the Current event to set the subform's recordset.

Just curious: Why does your work require you to use a recordset when working with Access data (assuming, that is, you're working with Access data).

So in the CURRENT event of your MAIN FORM, AFTER setting the Recordset of your Main form:

Dim rstSF As ADODB.Recordset
Dim strSQL As String
Set rstSF = New ADODB.Recordset

'///  Added
strSQL = "SELECT D_ErrorsTbl.error_id, D_ErrorsTbl.error_code, " & _
"D_ErrorsTbl.error_description, D_ErrorsTbl.audit_id " & _
"FROM D_ErrorsTbl " & _
"WHERE (((D_ErrorsTbl.audit_id)= " & Nz(Me.txtauditid_main, 1) & "))"
   
rstSF.CursorLocation = adUseClient  'Enable's form to be editable.
rstSF.Open strSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic  

Set Me.NameOfYOurSubformCONTROL.Form.Recordset = rstSF  


Also, there is no reason to set the ControlSource in code; you can just set those items in Design view, assuming they don't change. However, if you must do this in code for some unknown reason, you can do this from the MAIN FORM:

Me.NameOfYourSubformControl.Form.txterror_id.ControlSource = "error_id"
Me.NameOfYourSubformControl.Form.txterror_description.ControlSource = "error_description"
Me.NameOfYourSubformControl.Form.cboerror_code.ControlSource = "error_code"

NameOfYourSubformControl is the name of the CONTROL on your mainform, and may or may not be named the same as the form you're using as a Subform.


Random Solutions  
 
programming4us programming4us