473,503 Members | 12,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Duplicating Record Problem

76 New Member
Hello All,

I am trying to code a command button on a form to create a duplicate record and renumber it. The code I have in the command button currently is;

DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70 'Paste Append
NumberNewRecord
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

The NumberNewRecord sub is as follows:

[Prospect Number] = DMax("[prospect number]", "PR Only Info", "[list name] = [Forms]![PR Only Info]![cbListFilter]") + 1
If IsNull([Prospect Number]) Then [Prospect Number] = 1[List Name] = cbListFilter.Value


What currently happens with this code is that it adds a new record on form and appears to show the duplicate data in the controls. However, the numbering does not appear to be working. Also, when I move off the new record and then move back, the record still exists, but is empty. Any ideas?

Thanks,
Josh
Dec 5 '06 #1
6 1637
NeoPa
32,557 Recognized Expert Moderator MVP
Not many experts use macros :(.
We all use VBA code as it's better in almost every way.
It's particularly more readable (although code tags help too).
If you were to redo using VBA I'm sure you'd get more help.
If not, just leave it here and hope that someone can help. Good luck whichever.
Dec 5 '06 #2
jpatchak
76 New Member
This code is in VBA. The first part of the code was generated by the command button wizard. If there is a better way to code it without using the docmd method, please advise.
Dec 6 '06 #3
NeoPa
32,557 Recognized Expert Moderator MVP
Yes, you're right.
I was confusing calling menu items with macros. :embarrassed:
This is, nevertheless very difficult to read and understand without trawling through the documentation (typical wizard created code :().
Unfortunately I can't advise on alternative code as I have no idea what it's trying to do.
For that same reason I'm sorry I can't help you with this question.
Dec 6 '06 #4
missinglinq
3,532 Recognized Expert Specialist
I tried using the Wizard generated code to copy a record, like yourself, found it to be too full of problems, and finally "rolled" my own. Basically, I copy the record's values I want carried forward to the new record into variables, goto a new record, then assign these variables to the appropriate controls on the new record. For controls that you want to change, you can leave blank, fill with a message telling the user to fill them in, or, as in your case, fill in a new value. Not sure exactly what you mean by "re-numbering" the record, but if it involves incrementing a custom ID number number, yo could use something like DMAX + 1.

Expand|Select|Wrap|Line Numbers
  1. 'Copy fields from original record to variables
  2. NewField1 = Me.YourField1
  3. NewField2 = Me.YourField2
  4. NewField3 = Me.YourField3
  5.  
  6. 'Go to a new record
  7. DoCmd.GoToRecord , , acNewRec
  8.  
  9. 'Plug in old values into new record
  10. Me.YourField1.Value = NewField1
  11. Me.YourField2.Value = NewField2
  12. Me.YourField3.Value = NewField3
  13.  
  14. 'Set the fields your user wants to change to blanks (not really necessary) 
  15. Me.YourField4.Value = ""
  16. Me.YourField5.Value = ""
  17. Me.YourField6.Value = ""
  18.  
  19. If any of these fields to be changed is marked as Required in their table's Design Grid, leaving them blank will throw an error, so you'll need to change these to something like this: 
  20.  
  21. Me.YourField4.Value = "Enter New Value for YourField4 "
  22. Me.YourField5.Value = "Enter New Value for YourField5 "
  23. Me.YourField6.Value = "Enter New Value for YourField6 "
  24.  
As my signature insinuates, this is only one of several ways to do the job! Good luck!
Dec 6 '06 #5
jpatchak
76 New Member
This solution had occured to me, however this form is huge (over 100 controls), so writing the code to copy each control's value would be tedius and inefficient. I was hoping that there was a way to copy the whole record and then just change the two fields that I needed to. Is there a way maybe with DAO? Sorry, I am new to database programming.
Dec 6 '06 #6
jpatchak
76 New Member
This is the solution I came up with - seems to be working alright. Basically, it gets the primary key of the active record and opens a DAO recordset with only this record and the values of all of the fields I want to keep are copied into an array. A new record is then added to this record set. The fields are then populated with the data in the array. The fields I want to change[List Name] is then entered. The field [Prospect Number] is generated by my ReNumber sub and the form is requeried. Does anyone see any potential issues here?

strList = InputBox("Enter List Name")
Dim db As DAO.Database
Dim IQrst As DAO.Recordset
Dim IQstrSQL As String
Dim i As Integer
Dim lngMaster
lngMaster = [ID]
Dim NumFields As Long
IQstrSQL = "Select * FROM [PR Only Info] WHERE [ID] = " & CStr(lngMaster)
Set db = CurrentDb
Set IQrst = db.OpenRecordset(IQstrSQL, dbOpenDynaset)
NumFields = IQrst.Fields.Count - 1
Dim TempArray()
ReDim TempArray(3 To NumFields)
For i = 3 To NumFields
TempArray(i) = IQrst.Fields(i).Value
Next i
With IQrst
.AddNew
.Update
.MoveFirst
Do
If IQrst![ID] <> lngMaster Then
.Edit
For i = 3 To NumFields
.Fields(i).Value = TempArray(i)
Next i
IQrst![List Name] = strList
.Update
End If
.MoveNext

Loop Until .EOF
.Close
End With
Set IQrst = Nothing
Set db = Nothing
ReNumber strList
Requery
Dec 6 '06 #7

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

Similar topics

1
367
by: pwys | last post by:
Hello to everyone WOndering if there anyone could help me with this. I have a Primary & a secondary table with the unique key (InvoiceNo) Waht i want is to make duplicate copy of a selected...
7
2082
by: Paolo | last post by:
I know I should not be doing this, but I find it very useful. I have a database in Access which stores data for a small company. Sometimes we need to add similar information to different tables....
6
2496
by: Robin S. | last post by:
**Eric and Salad - thank you both for the polite kick in the butt. I hope I've done a better job of explaining myself below. I am trying to produce a form to add products to a table (new...
8
5748
by: Josetta | last post by:
I have found a wealth of information here on how to duplicate records in forms with subforms. I have adapted code found here to work with my forms. It works beautifully the first time I hit the...
2
2418
by: mavmavv | last post by:
I have a Form where I have created a duplicate record button, no problem... The subform is where my problem lies. The subform displays data matching the mainform's ID, these two values are...
1
2036
by: samdev | last post by:
I have used the wizard to create a Duplicate Record command button on a form. It works fine for a single duplicate but if I want to duplicate an additional record I receive an error: "The...
2
2601
by: Kanashii | last post by:
First off - I want to thank all who respond / try to help out and even those who took a moment to read over my question. Please be patient as I am -very- new to VB in general and unfortunately...
2
1163
by: kkiger | last post by:
My problem is this, I can set up a command button to duplicate a record everytime I click on it. What happens is that I get a record but it will be at the end of the table. For example, if I am on...
4
2382
by: Barno77 | last post by:
Everytime I edit a record, and hit update I get duplicating lines in my Details View. Here's my If statement: If (Len(e.OldValues("AutoNumber")) > 0) Then sql = "UPDATE SHIP_LOG SET...
0
7212
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,...
1
7017
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
7470
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
5604
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,...
1
5026
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...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.