473,396 Members | 1,772 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,396 software developers and data experts.

Update table from another table.

Hello all,

I have a form that allows a user to add employees. I have written the code that uses the "on click" event from a button, attempting to add the employee's info into 2 tables, tblEmployees and tblDxx. The Dxx refers to the department number that the user selects from a drop down box.

I believe I almost have it. I'm trying to store most of the info into tblEmployees and then just use the Emp_ID for all of the tblDxx, so as not to keep storing the names and other info over and over.

Basically, I want to copy the Emp_ID-autonumber (that was just generated from tblEmployees) and insert it into tblDxx (whatever the user selects).

What I've done is just tell tblEmployees to go to the last record and get that value. Then, insert that value (just generated EMP_ID) into the chosen table.
Here's the code I have:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub btnAddEmp_Click()
  3. On Error GoTo Err_btnAddEmp_Click
  4.  
  5. Dim rs As ADODB.Recordset
  6. Dim LastValue As String
  7.  
  8. Set rs = New ADODB.Recordset
  9.  
  10.  
  11.     DoCmd.RunSQL "INSERT INTO tblEmployees ([FirstName], [LastName],[Shift])" & _
  12.     "VALUES (txtFirstName, txtLastName, cboShift);"
  13.  
  14.     rs.Open "SELECT * FROM tblEmployees", CurrentProject.Connection, adOpenKeyset, adLockReadOnly
  15.  
  16.     With rs
  17.     .MoveLast
  18.       'Get the value of the last record
  19.     LastValue = .Fields("Emp_ID").Value
  20.     End With
  21.  
  22.     rs.Close
  23.  
  24.    DoCmd.RunSQL "INSERT INTO " & Me.cboDept.Value & " ([Emp_ID])" & _
  25.     "VALUES (LastValue);"
  26.  
  27. Exit_btnAddEmp_Click:
  28.     Exit Sub
  29.  
  30. Err_btnAddEmp_Click:
  31.     MsgBox Err.Description
  32.     Resume Exit_btnAddEmp_Click
  33.  
  34. End Sub
  35.  
The problem I'm getting is that a dialouge box pops up asking for the "LastValue". The user shouldn't know this number because it should be the autogenereated EMP_ID.

If there is a better way to do this, I'm open to that as well.
Just tried this method because that's what made sense to me.

Any thoughts?
Thanks in advance!
Sep 13 '07 #1
1 2720
Figured out a better way. For those out there that would like to do the same or someting similar, you can do a ' DoCmd.RunSQL "INSERT INTO ... SELECT"'
statement. Breaking that down, you can tell the sql to INSERT INTO tblB (where you want the data to go) SELECT * FROM tblA (where the data resides)

My first attempt at doing this looked like:
Expand|Select|Wrap|Line Numbers
  1.  
  2. rs.Open "SELECT * FROM tblEmployees", CurrentProject.Connection, adOpenKeyset, adLockReadOnly
  3.  
  4.     'With rs
  5.      '   .MoveLast
  6.         'Get the value you want off the last record (newest date)
  7.      '   LastValue = .Fields("Emp_ID").Value
  8.       '  End With
  9.  
  10.    'rs.Close
  11.  
  12.    'DoCmd.RunSQL "INSERT INTO " & Me.cboDept.Value & " ([Emp_ID])" & _
  13.     '"VALUES (LastValue);"
  14.  
The much easier way to do this is:

Expand|Select|Wrap|Line Numbers
  1.  
  2. DoCmd.RunSQL "INSERT INTO " & Me.cboDept.Value & " ([Emp_ID])" & _
  3.     "SELECT Max(Emp_ID) FROM tblEmployees;"
  4.  
Enjoy!

This post can be closed if needed.
Sep 13 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
12
by: jimserac | last post by:
I had previously posted this in an Access forum with negative results so will try here. Although this question specifies an Access database, I also wish to accomplish this with a large MS SQL...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
3
by: rrh | last post by:
I am trying to update a field in one table with data from another table. The problem I'm running into is I need to base the update on a range of data in the 2nd table. Table 1 has: date field...
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
6
by: FayeC | last post by:
I really need help figuring this out. i have a db with mostly text fields but 2. The user_id field is an autonumber (key) and the user_newsletter is a number (1 and 0) field meaning 1 yes the ...
2
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
13
by: shookim | last post by:
I don't care how one suggests I do it, but I've been searching for days on how to implement this concept. I'm trying to use some kind of grid control (doesn't have to be a grid control, whatever...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.