473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Better way to update two tables ?

TD
I need help understanding a better way to do this. I have an unbound
form named frmParts that has two subforms neither of which is bound to
frmParts. One subform is named frmSubParts and it's record source is
tblParts. The other subform is named frmSubAssemblies and it's record
source is tblAssemblies.

tblParts
RecID AutoNumber Primary Key
VendorPartNumber Text
ProtecPartNumber Text
PPN_Number Number
Description Text
Assy Yes/No

tblAssemblies
RecID AutoNumber Primary Key
Part_RecID Number Long
VendorPartNumber Text
ProtecPartNumber Text
Description Text
Qnty Number
I have a hidden text field named txtLink, who's control source is set
to "=Forms!frmParts!frmSubParts!RecID". On frmSubAssemblies I have the
Link Master Fields set to txtLink and the Link Child Fields set to
Part_RecID.

The purpose of this form is to setup part and assemblie information.
An assemblie is made up of two or more parts.

What I have now works but I'm looking for a better way to do this. I
have code that adds the part info to tblParts then retrieves the RecID
of the new part and then it writes the info into tblAssemblies;
tblParts.RecID gets written to tblAssemblies.Part_RecID.

Is there a way to use a query to do all of this?

Here is the code under the ADD button:

Private Sub cmdAdd_Click()
On Error GoTo Err_cmdAdd_Click

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim AppPath As String
Dim sql As String

If Me.chkAssy = True Then

If Nz(Me.txtPPN, "") = "" Or Nz(Me.txtDesc, "") = "" Or
Nz(Me.cboParts, "") = "" Or Nz(Me.txtQnty, "") = "" Then
MsgBox "Please fill in the appropriate fields!", vbOKOnly,
"WAIT!"
Me.txtVPN.SetFocus
GoTo Exit_cmdAdd_Click
End If

sql = ""
sql = sql & "INSERT INTO tblParts (VendorPartNumber,
ProtecPartNumber, PPN_Number, Description, Assy) "
sql = sql & "SELECT UCase(Forms!frmParts!txtVPN),
UCase(Forms!frmParts!txtPPN), "
sql = sql &
"CLng(Right(Forms!frmParts!txtPPN,Len(Forms!frmPar ts!txtPPN)-1)), "
sql = sql & "Forms!frmParts!txtDesc, Forms!frmParts!chkAssy;"

DoCmd.SetWarnings False
DoCmd.RunSQL sql
DoCmd.SetWarnings True

sql = ""
sql = sql & "SELECT tblParts.RecID FROM tblParts WHERE "
'sql = sql & "(((tblParts.VendorPartNumber)= '" &
[Forms]![frmParts]![txtVPN] & "') AND "
sql = sql & "(((tblParts.ProtecPartNumber)= '" &
[Forms]![frmParts]![txtPPN] & "'));"
'sql = sql & "((tblParts.Description)= '" &
[Forms]![frmParts]![txtDesc] & "'));"

AppPath = CurrentDb.Name
Set db = OpenDatabase(AppPath)

Set rst = db.OpenRecordset(sql, dbOpenSnapshot)

sql = ""
sql = sql & "INSERT INTO tblAssemblies (Part_RecID,
VendorPartNumber, "
sql = sql & "ProtecPartNumber, Description, Qnty) VALUES "
sql = sql & "('" & rst(0) & "', '" & Me.cboParts.Column(1) &
"', '" & Me.cboParts.Column(2) & "', "
sql = sql & "'" & Me.cboParts.Column(3) & "', '" & Me.txtQnty &
"');"

DoCmd.SetWarnings False
DoCmd.RunSQL sql
DoCmd.SetWarnings True

rst.Close
db.Close

Me.txtVPN = ""
Me.txtPPN = ""
Me.txtDesc = ""
Me.chkAssy = False
Me.cboParts = ""
Me.txtQnty = ""

Forms![frmParts]![frmSubParts].Requery
Forms![frmParts]![frmSubAssemblies].Requery

GoTo Exit_cmdAdd_Click

ElseIf Me.chkAssy = False Then

If Nz(Me.txtPPN, "") = "" And Nz(Me.txtDesc, "") = "" And
Nz(Me.cboParts, "") = "" And Nz(Me.txtQnty, "") = "" Then
MsgBox "Please fill in the appropriate fields!", vbOKOnly,
"WAIT!"
Me.txtVPN.SetFocus
GoTo Exit_cmdAdd_Click
End If

If (Nz(Me.txtPPN, "") <> "" And Nz(Me.txtDesc, "") <> "") Then

sql = ""
sql = sql & "INSERT INTO tblParts ( VendorPartNumber,
ProtecPartNumber, PPN_Number, Description ) "
sql = sql & "SELECT Forms!frmParts!txtVPN AS VPN,
Forms!frmParts!txtPPN AS PPN, "
sql = sql &
"CLng(Right(Forms!frmParts!txtPPN,Len(Forms!frmPar ts!txtPPN)-1)) AS
PPN_N, "
sql = sql & "Forms!frmParts!txtDesc AS [Desc];"

DoCmd.SetWarnings False
DoCmd.RunSQL sql
DoCmd.SetWarnings True

rst.Close
db.Close

Me.txtVPN = ""
Me.txtPPN = ""
Me.txtDesc = ""
Me.chkAssy = False
Me.cboParts = ""
Me.txtQnty = ""

Forms![frmParts]![frmSubParts].Requery
Forms![frmParts]![frmSubAssemblies].Requery

GoTo Exit_cmdAdd_Click

End If

If Nz(Me.cboParts, "") <> "" And Nz(Me.txtQnty, "") <> "" Then

If Forms![frmParts]![frmSubParts]![Assy] = False Then
MsgBox "This part is not an assembly", vbOKOnly,
"WAIT!"
GoTo Exit_cmdAdd_Click
End If

sql = ""
sql = sql & "INSERT INTO tblAssemblies (Part_RecID,
VendorPartNumber, "
sql = sql & "ProtecPartNumber, Description, Qnty) VALUES "
sql = sql & "('" & Forms![frmParts]![frmSubParts]![RecID] &
"', '" & Me.cboParts.Column(1) & "', "
sql = sql & "'" & Me.cboParts.Column(2) & "', '" &
Me.cboParts.Column(3) & "', '" & Me.txtQnty & "');"

DoCmd.SetWarnings False
DoCmd.RunSQL sql
DoCmd.SetWarnings True

rst.Close
db.Close

Me.txtVPN = ""
Me.txtPPN = ""
Me.txtDesc = ""
Me.chkAssy = False
Me.cboParts = ""
Me.txtQnty = ""

Forms![frmParts]![frmSubAssemblies].Requery

GoTo Exit_cmdAdd_Click

End If

End If

Exit_cmdAdd_Click:
Set rst = Nothing
Set db = Nothing
Exit Sub

Err_cmdAdd_Click:
MsgBox Err.Description
Resume Exit_cmdAdd_Click

End Sub

Jun 27 '06 #1
3 3791
Well, to start, it looks to me like you need three tables instead of two:
tblParts stores information about a particular part, like part numbers and a
description
tblAssemblies stores info about a particular assembly, again 'part' (i.e.
assembly) numbers and a description
tblPartsInAssemblies will store info about which parts are in which
assemblies, and quantities.

for example:

tblParts:
partID description
1 "screw"
2 "bolt"

tblAssemblies:
assemblyID description
1 'widget'
2 'gadget'

tblPartsInAssemblies
partID AssemblyID quantity
1 1 2
2 1 3
1 2 4

now with subforms you can see which parts belong to a particular assembly or
which assemblies include a particular part

"TD" <dl**@adsi-sc.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
I need help understanding a better way to do this. I have an unbound
form named frmParts that has two subforms neither of which is bound to
frmParts. One subform is named frmSubParts and it's record source is
tblParts. The other subform is named frmSubAssemblies and it's record
source is tblAssemblies.

tblParts
RecID AutoNumber Primary Key
VendorPartNumber Text
ProtecPartNumber Text
PPN_Number Number
Description Text
Assy Yes/No

tblAssemblies
RecID AutoNumber Primary Key
Part_RecID Number Long
VendorPartNumber Text
ProtecPartNumber Text
Description Text
Qnty Number
I have a hidden text field named txtLink, who's control source is set
to "=Forms!frmParts!frmSubParts!RecID". On frmSubAssemblies I have the
Link Master Fields set to txtLink and the Link Child Fields set to
Part_RecID.

The purpose of this form is to setup part and assemblie information.
An assemblie is made up of two or more parts.

Jun 27 '06 #2
TD

John Welch (remove remove) wrote:
Well, to start, it looks to me like you need three tables instead of two:


Thanks John for the reply. That's an interesting thought, I'll take a
look at it, but I guess what I really was wondering is this.

I have a one-to-many relationship setup based on tblParts.RecID and
tblAssemblies.Part_RecID.

The way the code is now I have to add the info to tblParts and save the
record before the autonumber is generated for RecID. Then I have to
search tblParts for the part I just added to retrieve the RecID for
that part. Then I have to add the parts to tblAssemblies inputting
tblParts.RecID into tblAssemblies.Part_RecID.

What I would really like is to use a query to add the data to both
tables and populate the linking fields (tblParts.RecID and
tblAssemblies.Part_RecID) without me having to populate them manually.

Any ideas?

Jun 27 '06 #3
TD

Anyone?

Jun 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an...
1
by: Rod | last post by:
I am writing my third ASP.NET application. Unfortunately for me, I get to work on these only once every few months (which doesn't help in trying to remember how to do everything). Anyway, I've...
2
by: msnews.microsoft.com | last post by:
Hello, I have the scenario. I m building an application either in asp.net or window application. This application is base on n-tier application model. Let us take example of Northwind Database in...
0
by: cwbp17 | last post by:
Have two oracle tables that have a FK relationship on ID column. Have one datagrid that displays all of the columns of both tables. What's the best approach on updating a row from the datagrid...
9
by: jaYPee | last post by:
I have search a lot of thread in google newsgroup and read a lot of articles but still i don't know how to update the dataset that has 3 tables. my 3 tables looks like the 3 tables from...
7
by: jaYPee | last post by:
I am having a problem on how to update view from sql server 2000 database. I have tried dataset and sql dataadapter but no luck. I'm using this view in datagrid and i want to update the datagrid if...
0
by: Ruslan Shlain | last post by:
I am pulling data from flat file and putting it in to the DB using dataset. Below i am including code how i did it. I am adding a row to a new table in the dataset and adding collumns to it one by...
10
by: rcamarda | last post by:
I have created a table that contains buckets to hold activitives of enrollment for each of our admissions officer for each day of an enrollment session. I have an UPDATE that builds rolling totals...
5
by: explode | last post by:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String) that creates a new oledbDataAdapter with insert update select and delete commads. I also added that commands can...
5
by: orabalu | last post by:
Hi Guys, Can you give me some examples for Incremental load in PL/SQL for Datawarehouse projects. Regards, Balu
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.