Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command Button Question

Status
Not open for further replies.

Bill6868

Technical User
Mar 20, 2007
96
0
0
US
I’m developing a donor database for our non-profit. I have a table which holds the donor’s profile info…one donor – one profile, with an ID Record Number [DonorID]. This donor makes multiple donations and the donations are held in a separate table which holds the Donor’s ID number and creates a donation Record ID number. So I have a one-to-many relationship between the donor profile table and the donor donation table.

Here’s my question: On the donor’s profile form (frmDonor) I want to place a command button that when clicked will open up the new donation record form (frmNewDonation) and have that donor’s ID number inserted into the DonorID field. From there I could proceed with entering the donation info.

I could just open up the new donation form and type in the donor’s ID number and this would work for me but I don’t want my users to have to remember the number and type it in – I’d like to automate the process to eliminate any data entry errors.

This sounds like a simple thing to do but I just can’t seem to find a solution. Any suggestions would be greatly appreciated.

Bill6868
 
I would use the OpenArgs property of the form that is opened. The DoCmd.OpenForm method allows you to specify a string that can be read by the form being opened. Use the On Open event of the new form to set the value of the Donor ID.

Duane
Hook'D on Access
MS Access MVP
 
Duane,

Thank you for your suggestion. I have collected a lot of advice and tips from you and others from Tek-Tips over the years. All this is greatly appreciated and I keep all these nuggets in a database of its own for reference.

Re your advice on this particular question – I understand what you are suggesting, but I have been unable to figure out how to do this. This is a bit above my pay grade.

Would you be able to provide an example? I need a Command button on my Donor Profile form that will capture the Profile RecordID number, and then open up my new record donation form and have that Profile’s ID number inserted in the Donation form’s Donor ProfileID field – thus linking this donor with this donation. As I mentioned, I could just mentally note the Profile RecordID number and type it in on the donation form but I don’t want my users to do this because it opens up the possibility of a data entry error.

Bill6868
 
This is code from the NorthWind sample database. I made the OpenArgs red.

Code:
Sub AddProducts_Click()
[COLOR=#4E9A06]' This code created in part by Command Button Wizard.On Error GoTo Err_AddProducts_Click[/color]
    Dim strDocName As String
    strDocName = "Products"
    [COLOR=#4E9A06]' Open Products form in data entry mode and store SupplierID in
    ' the form's OpenArgs property.[/color]
    DoCmd.OpenForm strDocName, , , , acAdd, , [COLOR=#EF2929][b]Me!SupplierID[/b][/color]
[COLOR=#4E9A06]    ' Close Product List form.[/color]
    DoCmd.Close acForm, "Product List"
[COLOR=#4E9A06]    ' Give ProductName control focus.[/color]
    Forms![Products]!ProductName.SetFocus    
Exit_AddProducts_Click:
    Exit Sub
Err_AddProducts_Click:
    MsgBox Err.Description
    Resume Exit_AddProducts_Click
End Sub

The On Open event of the Products form has this code:
Code:
Private Sub Form_Open()
    If IsNull(Me.OpenArgs) Then
        Exit Sub
    Else
[COLOR=#4E9A06]       'Use the value from OpenArgs[/color]
        Me!SupplierID = Me.OpenArgs
        Me!SupplierID.DefaultValue = Me.OpenArgs
    End If
End Sub


Duane
Hook'D on Access
MS Access MVP
 
Duane,

Most appreciated! I can already see several ways I can use this trick.

Here's my solution as per your suggestion:


Donor Profile Form's Command Button code is as follows:

Private Sub Command2_Click()
DoCmd.OpenForm "FrmDonationNew", , , , acFormAdd, , Me!RecID

End Sub
---------------------------
Donation Form's “On Open” event is as follows:

Private Sub Form_Open(Cancel As Integer)
DoCmd.GoToRecord , , acNewRec
If IsNull(Me.OpenArgs) Then
Exit Sub
Else
Me!RecID = Me.OpenArgs
Me!RecID.DefaultValue = Me.OpenArgs
End If

End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top