Adding record for 2 or more tables?

chobo321321

Registered User.
Local time
Yesterday, 21:00
Joined
Dec 19, 2004
Messages
53
I'm completely lost on this one. I'm working on an "add record" button, but I have to populate fields from two different tables with it.

All the fields except the "Genre" Field are from the same table
addrecords6yc.gif


This is what I have so far, as you can see I'm currently only opening a recordset from one table.

Code:
Private Sub cmdAddRecord_Click()
Dim conAdd As ADODB.Connection
Dim rstAdd As ADODB.Recordset
Dim strSQL As String

Set conAdd = CurrentProject.Connection
Set rstAdd = New ADODB.Recordset

strSQL = "SELECT * FROM tblAlbums"

rstAdd.Open strSQL, conAdd, adOpenStatic, adLockOptimistic
With rstAdd
    .AddNew
    !Album = Me.txtAlbum.Value
    !NOT = Me.txtNOT.Value
    !YOR = Me.txtYOR.Value
    !Artist = Me.txtArtist.Value
    !RecordLabel = Me.txtRecordLabel.Value
    !GenreID = Me.cboGenreID.Value    ''From different table
    .Update
    .Close
End With

Set rstAdd = Nothing
Set conAdd = Nothing

End Sub
 
You don't need to write any code at all if you use a bound form. Create a query that selects the columns you want on the form. Use the query as the RecordSource for the form. Choose the correct field name for each control from the list in the control's ControlSource property.

If you make the form bound from the beginning by specifying a query as the RecordSource when you create the form, each control will be bound when you add it as long as you choose fields from the field list.
 
I'm not sure I follow. I created a bound form that uses a query that has all the fields I want to update, but that doesn't help for adding records to a table. I still need to write code for the "add records" button, so it updates the record with the values from the fields on the form.

** update:

Oh nevermind, it does update the tables automatically the form was just pointing at the wrong query. Thanks.
 
Last edited:
Add Record buttons generally replace the function of the ">*" button on the built in Navigation Bar. In other words, it brings up an "empty" form. Save record buttons only have a single line of code (except for the error trapping)

DoCmd.RunCommand acCmdSaveRecord
 

Users who are viewing this thread

Back
Top Bottom