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

Append Current Record on a Form using Form Button

2
Hi All,

I have two tables: tblLicensedPrem and tblLicensedPremHistory (these tables are identical).

tblLicensedPrem contains records for licensed premises. Over time details of a licensed premises change: e.g. the premises changes its company name, opening hours, manager, telephone number etc

What I would like to do is add a button to a form that when clicked:

- Firstly, appends the CURRENT record, in its current state, into the table tblLicensedPremHistory
- Secondly, allows editing of the current record so details can be updated (although I am not to worried about this step at the moment).

I think I am best off doing this in VBA – however I am new to this and struggling.

As a test (I’m trying to take this on one stage at a time!) I have added a button named cmdArchiveData to my form and as a starter tried to copy only the record with PremID equal to 1, and only the first three fields in tblLicensedPrem. This event is running off of the OnClick Event of the form button. However for some reason this is not working.

Can anyone tell me where I am going wrong?

Regards,

Kevin


-----CODE---

Private Sub cmdArchiveData_Click()
'Run Archive - Append to tblLicensedPremHistory

Dim db As Database
Dim strSQLAp As String

Set db = CurrentDb

strSQLAp = "INSERT INTO tblLicensedPremHistory( Prem_ID, LicNumber, PremName ) "
strSQLAp = strSQLAp & "SELECT tblLicensedPrem.Prem_ID, "
strSQLAp = strSQLAp & "tblLicensedPrem.LicNumber, "
strSQLAp = strSQLAp & "tblLicensedPrem.PremName, "
strSQLAp = strSQLAp & "FROM tblLicensedPrem "
strSQLAp = strSQLAp & "WHERE tblLicensedPrem.Prem_ID = 1;"

db.Execute strSQLAp

End Sub
Apr 3 '08 #1
3 2883
JustJim
407 Expert 256MB
Hi All,

I have two tables: tblLicensedPrem and tblLicensedPremHistory (these tables are identical).

tblLicensedPrem contains records for licensed premises. Over time details of a licensed premises change: e.g. the premises changes its company name, opening hours, manager, telephone number etc

What I would like to do is add a button to a form that when clicked:

- Firstly, appends the CURRENT record, in its current state, into the table tblLicensedPremHistory
- Secondly, allows editing of the current record so details can be updated (although I am not to worried about this step at the moment).

I think I am best off doing this in VBA – however I am new to this and struggling.

As a test (I’m trying to take this on one stage at a time!) I have added a button named cmdArchiveData to my form and as a starter tried to copy only the record with PremID equal to 1, and only the first three fields in tblLicensedPrem. This event is running off of the OnClick Event of the form button. However for some reason this is not working.

Can anyone tell me where I am going wrong?

Regards,

Kevin


-----CODE---

Private Sub cmdArchiveData_Click()
'Run Archive - Append to tblLicensedPremHistory

Dim db As Database
Dim strSQLAp As String

Set db = CurrentDb

strSQLAp = "INSERT INTO tblLicensedPremHistory( Prem_ID, LicNumber, PremName ) "
strSQLAp = strSQLAp & "SELECT tblLicensedPrem.Prem_ID, "
strSQLAp = strSQLAp & "tblLicensedPrem.LicNumber, "
strSQLAp = strSQLAp & "tblLicensedPrem.PremName, "
strSQLAp = strSQLAp & "FROM tblLicensedPrem "
strSQLAp = strSQLAp & "WHERE tblLicensedPrem.Prem_ID = 1;"

db.Execute strSQLAp

End Sub
Hi,
Instead of working directly into the history table, try opening a recordset based on that table and appending your record to that recordset. Key words to check in the helpfiles would be recordset, addnewand of course, don't forget to update.

Jim
Apr 3 '08 #2
KevinC
2
Hi Jim,

But wouldn't it be easier to just run one append query? I will actually want to append all fields from the original table into the history table and it seems a single append query should work for this - or am I incorrent?

I am getting the following error at present when I run my code give above:

Run-time error '3134':
Syntax error in INSERT INTO statment.

When I go into debug the arrow points to the line ---> db.Execute strSQLAp

Regards,

Kevin
Apr 3 '08 #3
JustJim
407 Expert 256MB
Hi Jim,

But wouldn't it be easier to just run one append query? I will actually want to append all fields from the original table into the history table and it seems a single append query should work for this - or am I incorrent?

I am getting the following error at present when I run my code give above:

Run-time error '3134':
Syntax error in INSERT INTO statment.

When I go into debug the arrow points to the line ---> db.Execute strSQLAp

Regards,

Kevin
OK, I thought you wanted to do it one at a time as you moved through the records on your form.

The INSERT INTO clause may have problems with the spaces and brackets in the first line of the SQL block and doesn't need the semi-colon at the end.

Here is a way to do the duplicate checking and insertion in one go using VBA. Of course declarations need to be made, field names changed to suit you and the WHERE clause changed as well.
Expand|Select|Wrap|Line Numbers
  1. '   Find unmatched Programme entries in table "tblNewArrivals"/"Tbl_Programmes" and open that data as a recordset
  2. strSQL = "SELECT tblNewArrivals.School, tblNewArrivals.Campus " & _
  3. "FROM tblNewArrivals LEFT JOIN Tbl_Programmes ON tblNewArrivals.School = Tbl_Programmes.School_No " & _
  4. "WHERE (((Tbl_Programmes.School_No) Is Null) AND ((Tbl_Programmes.Campus_No) Is Null));"
  5. Set rsNewProg = dbNAP.OpenRecordset(strSQL)
  6.  
  7. '   also open the real programmes table as a recordset
  8. strSQL = "SELECT * from Tbl_Programmes"
  9. Set rsProg = dbNAP.OpenRecordset(strSQL)
  10.  
  11. '   Add records from new programmes to real programmes table
  12. Do Until rsNewProg.EOF
  13.  
  14.     With rsProg
  15.         .AddNew
  16.             !School_No = rsNewProg!School
  17.             !Campus_No = rsNewProg!Campus
  18.             !NAP_Provider_ID = 9 '  no information in tblNewArrivals for these fields, but they are required in the
  19.             !Outpost_Host_ID = 9 '  table to fulfill Referential Integrity requirements.  ID #9 is a "No Provider" entry
  20.         .Update
  21.     End With
  22.  
  23.     rsNewProg.MoveNext
  24.  
  25. Loop
Enjoy

Jim
Apr 3 '08 #4

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

Similar topics

2
by: Tony | last post by:
Hello, I am having difficulty in getting the current record of the current form to show after pressing a command button that takes me to another form. The command button takes me to another...
2
by: Paul Wagstaff | last post by:
Hi there I have 2 tables: tblAccuracy & tblClearance Users add new records to tblAccuracy using frmRegister. Under specific conditions I need to append the current record from frmRegister into...
4
by: DBQueen | last post by:
I have a subform which is in Continuous Forms view. I have added a button to the bottom of the page to move to the next record using the button wizard (result: DoCmd.GoToRecord , , acNext). I...
1
by: Richard Coutts | last post by:
I have a Continuous Form where each record has a button that activates another form that simplifies entering values into the record. The activated form has the equivalent of a "Done" button. I'd...
2
by: Ray Holtz | last post by:
I have a form that shows a single record based on a query criteria. When I click a button it is set to use an append query to copy that record to a separate table, then deletes the record from the...
11
by: kabradley | last post by:
Hello Everyone, So, thanks to nico's help I was finally able to 'finish' our companies access database. For the past week or so though,I have been designing forms that contain a subform and an...
2
easydoesit
by: easydoesit | last post by:
Hello all, I am looking for a way to be able to enter data into fields on a form, then be able to e-mail a report that shows only that record. This is what I have thus far: At the end of my...
2
by: SJ1000 | last post by:
Hi, I think I have a simple question that I just can't figure out. I want to have a command button on a form (via a macro) run a query to append the data on that form to another table.I want it to...
3
by: hikosj | last post by:
Hi all, I have a problem with a query in access that I cant seem to figure out. I have a form named frmRecruitment with a subform named sfrmParticipant. At the moment I am using an append query to...
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
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
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...

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.