473,396 Members | 1,900 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.

Create new record in VBA

thelonelyghost
109 100+
First off, I already know of one answer to my question, but it's horribly... clunky, to put it lightly. What I'm looking for is a better way to go about this. Now my obligatory header

Software: Access 2000
OS: Windows XP Professional SP3
Problem: Copying over the primary key (Autonumber) from Table A to Table B when creating a new record in Table A.

Sorry if the synopsis is confusing, allow me to explain. I have a database of records with data saying this and that. I have also set up a table to log which fields in each record change containing this information:
  1. ModificationID - Autonumber - (Primary Key)
  2. CTSLogNumber - Number(Long Integer)
  3. AuthorName - Text(50)
  4. FieldName - Text(255)
  5. dtModDate - Date/Time(Short Date) - Default Value: =Format(Now(),"MM/DD/YYYY")
  6. tmModDate - Text(8) - Input Mask: 00:00:00;0;# - Default Value: =Format(Now(),"HH:MM:SS")

What I'm looking for is a way to get the primary key from the table I'm logging, after running an INSERT INTO command in SQL. FYI most, if not all, of my record manipulation is done through SQL.

Clunky Solution: In the Data Entry form, VB code creates a random string of characters individual to the time, date, and author (such as the server time and date salted with the author's name???). Take that value and set it equal to one of the available fields in the table I'm monitoring and have the autonumber generate a new primary key. Then, as soon as that's done, I read what the value of the key is where the salted hash is located and copy that over to the appropriate field in my logging table. I'm then free to update the monitored table with the data I originally wanted to enter and the rest of the record in the log.

That's the best I can come up with for getting the automatically generated primary key from another table on record creation. Is there a better/easier way to do this?
Aug 9 '10 #1
4 5563
Steven Kogan
107 Expert 100+
This works... Instead of the debug.print you would put your code to add the log file record... Instead of 'AutoKey' you would put 'ModificationID', etc.

Expand|Select|Wrap|Line Numbers
  1.         Dim rst As DAO.Recordset
  2.         Dim strSQL As String
  3.         strSQL = "SELECT * " & _
  4.         "FROM [table] " & _
  5.         "WHERE 1=2"
  6.  
  7.         Set rst = CurrentDb.OpenRecordset(strSQL)
  8.         rst.AddNew
  9.  
  10.         rst("Description") = "My new record"
  11.         rst("Codetext") = "test data"
  12.  
  13.         rst.Update
  14.         rst.MovePrevious
  15.  
  16.         Debug.Print rst!AutoKey
  17.  
  18.         rst.Close
  19.         Set rst = Nothing
Aug 9 '10 #2
thelonelyghost
109 100+
(1)Thank you very much for your response, Steven Kogan. I had forgotten DAO has the function to create a new record.

(2)My apologies, I guess I was being unclear. I want to import Table A's autokey into the CTSLogNumber field of Table B. What is shown above is Table B. Changing around what table generates the key is not an option, either.

Another thing I realize upon rereading my OP is that this is a multi-user interface so something static like "I am a new record" for every user is out of the question. It could cause a major mishap with an UPDATE query if two people happen to use this form at the same time. In addition, I'll be porting this over to SQL Server with VB forms (and such) so I don't think the idea to create a new record and move back one will work. :(

So to reiterate, does anyone have a better solution than a salted hash like I was originally thinking? In other words, when creating a new record with DOA or ADO, is there a way to set the newly generated autonumber to a variable upon creation of the record?

EDIT: Woot! Broke the 100-post mark!
Aug 10 '10 #3
Steven Kogan
107 Expert 100+
I'm interested in seeing another solution as well. This one should work okay though.

I've modified the code so it is closer to what you are describing. Note that SQL Server has functions to do what you'd like, so if your data is stored in SQL Server you may want to use a stored procedure instead.

The code provided is a sample, of course. It should work in a multi-user situation, including when a different user adds a record while this code is running.

The recordset created will only see the newly added record: records added by other users will not be seen.

Note also that by using 1=2 in the where clause, you will not accidentally go to any existing record. It begins as an empty recordset.

Expand|Select|Wrap|Line Numbers
  1. Dim rst As DAO.Recordset 
  2.         Dim strSQL As String
  3.         Dim lngCTSKey as long
  4.         strSQL = "SELECT * " & _ 
  5.         "FROM [Table A] " & _ 
  6.         "WHERE 1=2" 
  7.  
  8.         Set rst = CurrentDb.OpenRecordset(strSQL) 
  9.         rst.AddNew 
  10.  
  11.         rst("AuthorName") = me.AuthorName 
  12.         rst("FieldName") = me.FieldName
  13.  
  14.         rst.Update 
  15.         rst.MovePrevious 
  16.  
  17.         lngCTSKey = rst!CTSKey 
  18.  
  19.         rst.Close 
  20.         Set rst = Nothing 
  21.  
  22.         ' Insert code to add new record to Table B here, using lngCTSKey which is the autonumber of Table A
  23.  
  24.  
Aug 10 '10 #4
thelonelyghost
109 100+
Ah I see now! Maybe I was just confused before as to what the code accomplished, but the most recent explanation dispelled any of that. Thanks a lot, Steven Kogan! I'll choose yours as the best answer as soon as I test it.
Aug 10 '10 #5

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

Similar topics

2
by: David Elliott | last post by:
I can create this: ?xml version="1.0" standalone="yes" ?> <ConfigOpt> <record> <Field_1>Text # 1</Field_1> <Field_2>Text # 2</Field_2> </record> </ConfigOpt>
5
by: Ilan Sebba | last post by:
When it comes to adding records in related tables, Access is really smart. But when I try to do the same using ADO, I am really stupid. Say I have two parent tables (eg Course, Student) and one...
4
by: amywolfie | last post by:
I've been trying to do something for about 3 days – I get close, but not 100%. I am trying to: Open frmRevisionHistory from a button on frmFeeInput. If there is NO RELATED RECORD, then I...
2
by: Shapper | last post by:
Hello, I need to create a new record in a database. The database has 3 fields: (autonumber), and (strings) When I create the record how should I create the value? Does the database...
2
by: mael.iosa | last post by:
Hi, I'm new to this group and fairly new to Access. I have a bunch of data, and after several other queries, I generate the following query which has two fields: Bin, Time 20 3.5 20 3.9 20...
3
by: gsoguerrilla | last post by:
Hi, I have limited knowledge in php and I am having trouble with uploading an image to a remote directory and resizing it if it's larger and renaming it to a unique id, while at the same time I...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
4
by: ringer | last post by:
I have a db where each record is a certain task to be done. Each record includes the date the task should be completed. Some of the tasks are one time only but some are recurring. I have been having...
3
by: =?Utf-8?B?Sm9obiBXYWxrZXI=?= | last post by:
Hi, Below is sample output in x12 EDI format. I'm using vb.net to create this output file but i'm not sure if .NET has any tools to make the coding more organized and/or efficient. Right now the...
6
by: JHite | last post by:
I'm using Access 2003 with MS XP. Have table/records with FirstName (text field), LastName (text field), plus other fields. User enters FirstName, LastName and other info in form. Before updating...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.