Have subform key default to main form primary key when invok

tstuve

New Member
Joined
Apr 29, 2002
Messages
19
Hi,

I'm trying to fix a contacts database problem. The main form "contacts" has a command button to open a "names" form in which to view names associated with a specific contact (contactID is primary key on main form). I can page through the names just fine, but when I go to add a new name, the contactID goes back to 0.

The code for the command button and names form are below. Thanks in advance for any help you can offer.

Tom

Here's the code for the command button on the main "contacts" form:

Private Sub Command50_Click()
On Error GoTo Err_Command50_Click

Dim stDocName As String
Dim stLinkCriteria As String


stDocName = "Names"
stLinkCriteria = "[ContactID]=" & Me![ContactID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
'Form_Contacts.Requery


Exit_Command50_Click:
Exit Sub

Err_Command50_Click:
MsgBox Err.Description
Resume Exit_Command50_Click

End Sub

Heres the code for the names form:

On Error GoTo Err_cmdNextRec_Click


DoCmd.GoToRecord , , acNext

Exit_cmdNextRec_Click:
Exit Sub

Err_cmdNextRec_Click:
MsgBox Err.Description
Resume Exit_cmdNextRec_Click

End Sub
Private Sub cmdAdd_Click()
On Error GoTo Err_cmdAdd_Click


DoCmd.GoToRecord , , acNewRec

Exit_cmdAdd_Click:
Exit Sub

Err_cmdAdd_Click:
MsgBox Err.Description
Resume Exit_cmdAdd_Click

End Sub
Private Sub delete_Click()
On Error GoTo Err_delete_Click


DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70

Exit_delete_Click:
Exit Sub

Err_delete_Click:
MsgBox Err.Description
Resume Exit_delete_Click

End Sub


Private Sub Form_Load()

End Sub

Private Sub save_Click()
On Error GoTo Err_save_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_save_Click:
Exit Sub

Err_save_Click:
MsgBox Err.Description
Resume Exit_save_Click

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Re: Have subform key default to main form primary key when i

So are you wanting to add another name for the contact that is currently visible in the Contacts form?

If so, then try something like this:

Code:
Private Sub cmdAdd_Click() 
On Error GoTo Err_cmdAdd_Click 


DoCmd.GoToRecord , , acNewRec 
Me.txtContactID = Forms!Contacts!ContactID

Exit_cmdAdd_Click: 
Exit Sub

This assumes that txtContactID is the name of the text box on the Names form that holds the value of ContactID - change to the name of your text box. Let me know if it works,

-Russell
 
Upvote 0

Forum statistics

Threads
1,221,526
Messages
6,160,340
Members
451,637
Latest member
hvp2262

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