473,405 Members | 2,262 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,405 software developers and data experts.

Add records using VBA - Access 2007

I am using a form to add records to a table named "MASTER LIST". This forms pulls up existing records, allows the user to change certain fields, and then the user clicks an Add button. The Add button is suppose to create a new record based on the information on the form. Basically I am creating a new record based on the old record, but with certain fields updated. The Add button is driven by VBA code, see below, but it is not creating a new record.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Add_Click()
  2.     Dim db As DAO.Database
  3.     Dim rs As DAO.Recordset
  4.     Dim strSQL As String
  5.     On Error Resume Next
  6.  
  7.     Set db = CurrentDb
  8.     Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
  9.     CurrentDb.Execute "INSERT INTO [MASTER LIST] (" _
  10.         & "(Policy_Number," _
  11.         & "(Title," _
  12.         & "(Policy_Year," _
  13.         & "(Schedule," _
  14.         & "(Last_MPRM_ReviewDate," _
  15.         & "(Next_MPRM_ReviewDate," _
  16.         & "(Last_FEP_ReviewDate," _
  17.         & "(Next_FEP_ReviewDate," _
  18.         & "(Comments," _
  19.         & "(FEP_PolicyStatement," _
  20.         & "(Policy_HistoryChanges," _
  21.         & "(Orig_BlueWeb_PubDate," _
  22.         & "(Last_BlueWeb_PubDate," _
  23.         & "(Orig_FEPBlueOrg_PubDate," _
  24.         & "(Last_FEPBlueOrg_PubDate," _
  25.         & "(Keywords) VALUES " _
  26.         & "('" & Policy_Number.Value & "','" & Title.Value & "'," & Policy_Year.Value & ",'" & Schedule.Value & "','" & Last_MPRM_ReviewDate.Value & "','" & Next_MPRM_ReviewDate.Value & "','" & Last_FEP_ReviewDate.Value & "','" & Next_FEP_ReviewDate.Value & "','" & Comments.Value & "','" & FEP_PolicyStatement.Value & "','" & Policy_HistoryChanges.Value & "',#" & Format(Orig_BlueWeb_PubDate.Value, "mm/dd/yyyy") & "#,#" & Format(Last_BlueWeb_PubDate.Value, "mm/dd/yyyy") & "#,#" & Format(Orig_FEPBlueOrg_PubDate.Value, "mm/dd/yyyy") & "#,#" & Format(Last_FEPBlueOrg_PubDate.Value, "mm/dd/yyyy") & "#,'" & Keywords.Value & "');"
  27.  
  28.     rs.Close
  29.     db.Close
  30.     DoCmd.Close
  31.  
  32. End Sub 
Most fields are text, there is one field (Policy_Year) that is a number, and four fields at the end that are date fields. Can someone tell me if this code has an issue?

Thanks
Dec 27 '13 #1

✓ answered by Rabbit

You have a bunch of extraneous parentheses.

The insert syntax is:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tableName (
  2.    Field1,
  3.    Field2,
  4.    Field3
  5. ) VALUES (
  6.    ...
  7. )
What you have is:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tableName (
  2.    (Field1,
  3.    (Field2,
  4.    (Field3
  5. ) VALUES (
  6.    ...
  7. )

4 2115
Rabbit
12,516 Expert Mod 8TB
You have a bunch of extraneous parentheses.

The insert syntax is:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tableName (
  2.    Field1,
  3.    Field2,
  4.    Field3
  5. ) VALUES (
  6.    ...
  7. )
What you have is:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO tableName (
  2.    (Field1,
  3.    (Field2,
  4.    (Field3
  5. ) VALUES (
  6.    ...
  7. )
Dec 27 '13 #2
Hey Rabbit,

I tried, but my code still didn't work. I decided to try it a better way and it WORKED! The code is much cleaner, see below and thanks for your help.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Add_Click()
  2.     Dim db As DAO.Database
  3.     Dim rs As DAO.Recordset
  4.  
  5.     On Error Resume Next
  6.  
  7.     Set db = CurrentDb
  8.     Set rs = CurrentDb.OpenRecordset("SELECT * FROM [MASTER LIST]")
  9.     rs.AddNew
  10.     rs![Policy_Number] = Me.Policy_Number.Value
  11.     rs![Title] = Me.Title.Value
  12.     rs![Policy_Year] = Me.Policy_Year.Value
  13.     rs![Schedule] = Me.Schedule.Value
  14.     rs![Last_MPRM_ReviewDate] = Me.Last_MPRM_ReviewDate.Value
  15.     rs![Next_MPRM_ReviewDate] = Me.Next_MPRM_ReviewDate.Value
  16.     rs![Last_FEP_ReviewDate] = Me.Last_FEP_ReviewDate.Value
  17.     rs![Next_FEP_ReviewDate] = Me.Next_FEP_ReviewDate.Value
  18.     rs![Comments] = Me.Comments.Value
  19.     rs![FEP_PolicyStatement] = Me.FEP_PolicyStatement.Value
  20.     rs![Policy_HistoryChanges] = Me.Policy_HistoryChanges.Value
  21.     rs![Orig_BlueWeb_PubDate] = Me.Orig_BlueWeb_PubDate.Value
  22.     rs![Last_BlueWeb_PubDate] = Me.Last_BlueWeb_PubDate.Value
  23.     rs![Orig_FEPBlueOrg_PubDate] = Me.Orig_FEPBlueOrg_PubDate.Value
  24.     rs![Last_FEPBlueOrg_PubDate] = Me.Last_FEPBlueOrg_PubDate.Value
  25.     rs![Keywords] = Me.Keywords.Value
  26.     rs.Update
  27.  
  28.     rs.Close
  29.     Set rs = Nothing
  30.     db.Close
  31.     DoCmd.Close
  32. End Sub 
Dec 27 '13 #3
ADezii
8,834 Expert 8TB
Be careful with the
Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next
on Code Line# 5. Out of curiosity why are you using this approach instead of an Error Trap?
Dec 28 '13 #4
NeoPa
32,556 Expert Mod 16PB
I have reset the Best Answer post because :
  1. We generally discourage OPs (Original Posters - Those who ask the question) from awarding themselves the Best Answer because it's just too easy. Unless they come up with an appropriate answer that no-one else has helped towards. Otherwise it's a bit like cheating.
  2. The answer must be relevant to the question. An alternative solution that doesn't actually deal with the originally reported problem can be a valid and helpful post, but will normally not be considered as Best Answer.

Rabbit's post, on the other hand, deals directly with the underlying problem originally reported as the question of this thread and so is perfectly appropriate.
Dec 28 '13 #5

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

Similar topics

5
by: Hollywood | last post by:
Good day dear members of the comp.databases.ms-access newsgroup. Please I need your help on the following. I'm the administrator of an Access 2007 database. I would like my enterprise personnel...
1
by: Mientje | last post by:
I've made an Access 2007 database to store information about the lessonplans I have to make every schoolyear. I want to export the data form the table "Lesvoorbereiding" (Lessonplans in English) to...
3
by: khoward | last post by:
Hi, I have an Access 2007 database that contains customer contact information. There are over 8,000 that include name, organization (as a look-up column), email, phone, address, and events that...
3
by: brat33 | last post by:
I am trying to use a command button to automate a process for creating mailing labels from a query in Access 2007, going into word 2007. Current code looks like the following: 'Start MS Word Dim...
1
by: JMANTN | last post by:
I have data that sometimes needs to be uploaded to a 3rd party website and I’m trying to automate this process with Access 2007. I’m having a difficult time with my VBA syntax and with identifying...
1
newnewbie
by: newnewbie | last post by:
Hi, Short version: trying to export more than 65K of data from Access 2007 into Excel 2007 (important) through VBA. Access gives me 65K row limitation error, though I thought that was no longer...
0
by: cmcsween | last post by:
Will the Access 2007 runtime version run on machines with Windows 95, 98, and/or 2000 as the OS? Carol
1
by: Rick Aguayo | last post by:
In prior versions of MS Access we used to be able to use the wheel on the mouse to scroll thru records when a form is used. In MS Access 2007 we can not scroll thru the records....Or is there some...
0
by: Elaine Huseby | last post by:
I have a form "PartsAndInvoices that has all the parts in the database listed in split form view. I want the user to use Access 2007's filter section on the ribbon to choose mutiple filters....
2
by: kawprice60 | last post by:
I am a new user to Access 2007. I have to design a report that uses date prompts. My database has the following fields: date, total, under 30, service level I need to make a calculation on...
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: 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
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
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
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.