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

Code to Add New Row in MS Access

34
My Form has a List Box that need to be updated when someone type stuff in ComboPC,ComboFOPT&txtRiskPT. Also, the Table [Product Types2] needs to be updated.

I have a command button [New] with the following code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub New_Click()
  3.  
  4. Dim stDocName As String
  5.  
  6. stDocName = "INSERT INTO [Product Types2] ([Product Classification],[Front Office Product Type],[Risk Product Type]) VALUES ('" & ComboPC.Value & "','" & ComboFOPT.Value & "','" & txtRiskPT & "')"
  7. DoCmd.RunSQL stDocName
  8.  
  9. End Sub
  10.  
  11.  
It works the first time, but not when you want to add the 2nd new row.
Oct 24 '06 #1
5 12833
MMcCarthy
14,534 Expert Mod 8TB
You need to requery the list box each time.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub New_Click()
  3. Dim strSQL As String ' Just a naming convention
  4.  
  5. strSQL = "INSERT INTO [Product Types2] ([Product Classification],[Front Office Product Type],[Risk Product Type]) VALUES ('" & ComboPC.Value & "','" & ComboFOPT.Value & "','" & txtRiskPT & "')"
  6.  
  7. DoCmd.RunSQL strSQL
  8.  
  9. Me.ListBoxName.Requery
  10.  
  11. End Sub
  12.  
  13.  
Oct 25 '06 #2
Riun
34
Thank you MMcCarthy, I appreciate it, but it still doesn't work. The first time the Form is opened and a row is added it works, and after that it doesn't want to add a new row. Any other suggestions, Please? One of my other forms with exactly the same code does work, though...
Oct 25 '06 #3
MMcCarthy
14,534 Expert Mod 8TB
Thank you MMcCarthy, I appreciate it, but it still doesn't work. The first time the Form is opened and a row is added it works, and after that it doesn't want to add a new row. Any other suggestions, Please? One of my other forms with exactly the same code does work, though...
Is it possible that the problem lies in the underlying table rather than the list box. Are you trying to add data that would violate the unique index of a field. What is the table structure?
Oct 25 '06 #4
Riun
34
my table has no indexes/primary keys. I found a solution, though: the difference between the firste forms (that works) and the second, was that the first reads the info to insert into table, out of text boxes, the second has one combo box to read from, thus, I wrote the value in the combo box to a text box, and from there, inserted it into the table!

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Combo51_BeforeUpdate(Cancel As Integer)
  2.     txtBook.Value = Combo51.Value
  3.     txtBook.Requery
  4. End Sub
  5.  
  6. Private Sub New_Click()
  7. 'DoCmd.SetWarnings False
  8.  
  9. Dim strDisplay As String
  10. Dim intResponse As Integer
  11. Dim strPrompt As String
  12. Dim stDocName As String
  13.  
  14.     If Focussed.Value = True Then strDisplay = "Yes"
  15.     If Focussed.Value = False Then strDisplay = "No"
  16.  
  17. txtID = ""
  18. If txtID = "" Or IsNull(txtID) Then
  19.         stDocName = "INSERT INTO [Product Type p Portfolio2] (Book,[Front Office Product Type],[Focussed Area?]) VALUES ('" & txtBook.Value & "','" & [FO PT].Value & "','" & Focussed.Value & "')"
  20.         DoCmd.RunSQL stDocName
  21.         Me.List55.Requery
  22.  
  23. End If
  24.  
  25. End Sub
  26.  
Don't know if this is the only answer, but it worked for now. Thank you for your help, MMcCarthy!
Oct 25 '06 #5
my table has no indexes/primary keys. I found a solution, though: the difference between the firste forms (that works) and the second, was that the first reads the info to insert into table, out of text boxes, the second has one combo box to read from, thus, I wrote the value in the combo box to a text box, and from there, inserted it into the table!

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Combo51_BeforeUpdate(Cancel As Integer)
  2.     txtBook.Value = Combo51.Value
  3.     txtBook.Requery
  4. End Sub
  5.  
  6. Private Sub New_Click()
  7. 'DoCmd.SetWarnings False
  8.  
  9. Dim strDisplay As String
  10. Dim intResponse As Integer
  11. Dim strPrompt As String
  12. Dim stDocName As String
  13.  
  14.     If Focussed.Value = True Then strDisplay = "Yes"
  15.     If Focussed.Value = False Then strDisplay = "No"
  16.  
  17. txtID = ""
  18. If txtID = "" Or IsNull(txtID) Then
  19.         stDocName = "INSERT INTO [Product Type p Portfolio2] (Book,[Front Office Product Type],[Focussed Area?]) VALUES ('" & txtBook.Value & "','" & [FO PT].Value & "','" & Focussed.Value & "')"
  20.         DoCmd.RunSQL stDocName
  21.         Me.List55.Requery
  22.  
  23. End If
  24.  
  25. End Sub
  26.  
Don't know if this is the only answer, but it worked for now. Thank you for your help, MMcCarthy!
____________________


This solution is not working for me, The tables are getting updated by what i put after single quotes ,

INSERT INTO CHILDPROJECT ( MASTERPROJECT, CHILDPROJECT )
VALUES (' " & mprj.value " ', ' " &chprj.value & " ');

this inserts "&mprj.value&" and " &chprj.value & "


Can u plz suggest some , its urgent.!
Jul 1 '08 #6

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

Similar topics

4
by: Si | last post by:
Hi Guys I am using this code to execute an Access VBA function from ASP: strDbName = strDataSource & "data\webjobs.mdb" Set objAccess = Server.CreateObject("Access.Application")...
2
by: Dutchy | last post by:
Hi there, After spending several hours trying all I could imagine and search for in Google I gave up. In a continuous form I want to sort the choosen column by clicking the header (label) of...
4
by: sea | last post by:
I have a database in Access 2002 but I am unable to view code or write any modules when logged in with a limited user account using Windows XP, service pack 2 -- no problems when logging in as...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
1
by: garry.oxnard | last post by:
Can anyone help me to solve a problem which involves switching from Access to Excel (then back to Access) programatically please? I have an Excel template which, on open, also opens an Access...
0
by: peridian | last post by:
Hi, I have the below code to transfer code modules between Access database files. I have three problems, only one of which is annoying. a) How do you prevent the Save As box from appearing...
15
by: colemanj4 | last post by:
Here is what I have so far, it loops while the PW is incorrect, or until cancel is selected. I want it to lock the tables for adds, deletes, and edits when cancel is selected, and if the PW is...
55
by: lovecreatesbea... | last post by:
Do you check all error conditions for all library calls in you code? Is it necessary to check all errors? Is it convenient to check all errors (or how to make code clean and readable with mass of...
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: 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
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
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.