Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / March 2005

Tip: Looking for answers? Try searching our database.

VBA Updating two tables from the same form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Graham - 03 Mar 2005 22:29 GMT
I have a database with two tables.  I am using a form with two unbound
text boxes and combo boxes to capture user input.  The problem is I
want to be able to update two tables from the same form.

The form has three fields first, surname and gender.

txtFirst
txtSurn
cboGender    (values: M; F; U)

The two tables have the following fields

tblName
fields:
    Person    (autonumber)
    First    (text)
Surn    (text)

tblGender
fields:
    Person    (autonumber)
    Gender    (text)

Here is my code to update the first table:

   Dim rstIndi As ADODB.Recordset
   Dim txtINDI As Integer
   Set rstIndi = New ADODB.Recordset
   
   rstIndi.Open "NAME", CurrentProject.Connection, adOpenStatic,
adLockOptimistic
   If rstIndi.Supports(adAddNew) Then
       With rstIndi
           .AddNew
           .Fields("First") = txtFirst
           .Fields("Surn") = txtSurn
           .Update
           cmdReset_Click
       End With
   End If
   
   rstIndi.Close
   
   Set rstIndi = Nothing

Now the problem is when the First and Surn fields are added to tblName
it works fine, incrementing the autonumber field as required.  How do
I copy the autonumber field from tblName to tblGender and add the
value selected in cboGender on the form to the field Gender, all of
course from the same submit function in the form?

Thanks

Graham
Rob Parker - 04 Mar 2005 06:42 GMT
It seems to me that you're making life much too hard for yourself.  Why not
simply put all your fields into a single table, tblPeople, and bind your
form to that table.  Each of the fields you have in both tables refer to a
single attribute of a particular person; normal database design principles
would have all this data in a single table.

If you must have two tables, then the Person field in tblGender CANNOT be an
autonumber if it is to be used as the foreign key for a person in tblName
where the primary key is (unless you've done something really strange) the
autonumbered Person field - it must be a long integer.  But again, there
seems to be no reason to do it that way.

HTH,

Rob

> I have a database with two tables.  I am using a form with two unbound
> text boxes and combo boxes to capture user input.  The problem is I
[quoted text clipped - 50 lines]
>
> Graham
Graham - 04 Mar 2005 16:38 GMT
OK understood.  

So I have changed the person field to a long integer in tblGender.
Now how do I update both tables from the one form?

Thanks

Graham
Rob Parker - 05 Mar 2005 00:02 GMT
The simplest way is to set up your data structure correctly, and combine the
two tables, as I suggested.  Use a form bound to the (single) table, with
the name textboxes and the gender combo-box bound to the appropriate fields
in the table, and it all just happens.

Do you have any compelling reason for keeping two tables (apart from making
life harder for yourself)?  Do you have any compelling reason for using an
unbound form/fields?

Rob

> OK understood.
>
[quoted text clipped - 4 lines]
>
> Graham
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.