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

Addnew method for similar field name

I have table with more then 50 field with similar name: answer1, answer2, answer3, answer4.....answer 50.
i want to make it simple with for next function. but my code doesn't accepted by the VBA. Why?

Expand|Select|Wrap|Line Numbers
  1. dim rst as adodb.recordset
  2. set rst=new adodb.recordset
  3.  
  4. for i=1 to 50
  5.     rst.addnew
  6.     rst!answer & i="my answer"&i
  7. next
Dec 24 '12 #1

✓ answered by ADezii

I'm assuming that your Question is how to I Add Data to a Table with sequentially numbered Fields (same Base Name). Assuming you have a Table named tblTest, with 50 Answer Fields sequentially numbered Answer1, Answer2, Answer3...Answer50, then the Logic would be as follows:
Expand|Select|Wrap|Line Numbers
  1. Dim rst As ADODB.Recordset
  2. Dim intFldCtr As Integer
  3.  
  4. Set rst = New ADODB.Recordset
  5.  
  6. With rst
  7.   .Source = "tblTest"
  8.   .ActiveConnection = CurrentProject.Connection
  9.   .CursorType = adOpenKeyset
  10.   .LockType = adLockOptimistic
  11.     .Open
  12.  
  13.   .AddNew
  14.     For intFldCtr = 1 To 50
  15.       .Fields("Answer" & CStr(intFldCtr)) = (intFldCtr ^ 2)
  16.     Next
  17.   .Update
  18. End With
  19.  
  20. rst.Close
  21. Set rst = Nothing

4 2289
zmbd
5,501 Expert Mod 4TB
i want to make it simple with for next function. but my code doesn't accepted by the VBA. Why?
Very non-specific question there...
You need to actually tell us what it was that you wanted to have happen and then what really happened. Provide in errors with the EXACT error number AND text and if needed, a little example of the data... generic data is just fine, so long as it reflects the actual data one would be using

With that said:

Your database sounds like it is not normalized. Please take a look at the following:
A Tutorial for Access
Database Normalization and Table Structures.

I get this impression because this is what your table sounds like

{tbl_name
[table_primarykey]
[answer1] text(some limit)
[answer2] text(some limit)
[answer3] text(some limit)
[answer4] text(some limit)
(...)
[answer50] text(some limit)}
(note these are not RECORD entries, these are fields)

Your question appears to ask how to add
[answer51] text(some limit)

Your code is an attempt to add a new record:
[table_primarykey]; [answer1]; ...; [answer50]
(some current value); "Some question1"; ...; ""Some question50"
(some current value +1); "Some question1a"; ...; ""Some question50a"
(...)
(some current value +50); "Some question1zz"; ...; ""Some question50zz"

Your missing an .update just before the next at end of the loop too in order to save the record.
(Note: I've not actually vetted the syntax of your code. I've only taken a quick glance as I'm on may way out the door to the lab)


Please work thru the links and provide more information if I have misunderstood your question.
Dec 24 '12 #2
NeoPa
32,556 Expert Mod 16PB
Please have the courtesy not to post air-code. This code has certainly not been copied from an Access project as it is not formatted in a way that indicates it even might be. See Before Posting (VBA or SQL) Code, and please understand that we will expect you to comply with these very basic requirements before we will be willing to spend time trying to help you properly.

A very short answer to your why is that the syntax for your code is wrong in a large number of places. These would be found by a simple compile and should not be wasting any human time thinking about them.
Dec 24 '12 #3
zmbd
5,501 Expert Mod 4TB
I'm with Neopa on the posted code.

Please go to your vba editor in MS Access
Then go to the Ribbon/toolbar:> Tools :> Options
Editor tab:
Uncheck "Auto Syntax Check" all this does is add an annoying pop-up that says... you goofed... click ok... the line will already turn red when you mis-form the code.
Check Mark "Require Variable Declaration" (this will place the option explicate at the top of every new code module)
checkmark all of the remaining options.
I like an auto indent of 4; however, I wouldn't go less than 3 or more than 5 just as a personal preference.
The remaining stuff can be left as default.

Now go thru all of your form modules, standard modules, and class modules and make sure that the following two lines are the VERY first two lines in every one of these modules
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
Now compile your project.
Fix any errors
Re-compile your project
Repeat the above three lines until you can compile the project without an error.

As for your posted code:
Line 6 has so many things wrong with it that I should have caught it with a 5 second read. I can only attribute my miss to the several gulps of EggNogg before bed last night (luvs-me-nogg-n-egg) and the 5 hours of sleep... getting old... seems I need at least 6 or so to function now. Amazing that I used to be able to work on 15 minute "cat-naps" in my 20s.

Also line 6 re-affirming that you need to read thru the links I gave you.
Dec 24 '12 #4
ADezii
8,834 Expert 8TB
I'm assuming that your Question is how to I Add Data to a Table with sequentially numbered Fields (same Base Name). Assuming you have a Table named tblTest, with 50 Answer Fields sequentially numbered Answer1, Answer2, Answer3...Answer50, then the Logic would be as follows:
Expand|Select|Wrap|Line Numbers
  1. Dim rst As ADODB.Recordset
  2. Dim intFldCtr As Integer
  3.  
  4. Set rst = New ADODB.Recordset
  5.  
  6. With rst
  7.   .Source = "tblTest"
  8.   .ActiveConnection = CurrentProject.Connection
  9.   .CursorType = adOpenKeyset
  10.   .LockType = adLockOptimistic
  11.     .Open
  12.  
  13.   .AddNew
  14.     For intFldCtr = 1 To 50
  15.       .Fields("Answer" & CStr(intFldCtr)) = (intFldCtr ^ 2)
  16.     Next
  17.   .Update
  18. End With
  19.  
  20. rst.Close
  21. Set rst = Nothing
Dec 25 '12 #5

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

Similar topics

2
by: John Davis | last post by:
I want to know what's the differences between Request.Form("Field Name") and Request.QueryString("Field Name") OR they function exactly the same, which is to return the value of the field?? ...
7
by: Consuelo Guenther | last post by:
Hello, I have an asp page that has a function that connects to an Access database. I am using Javascript. Is there any way to reference the field name in the database through a variable in a sql...
1
by: Jon Trelfa | last post by:
I have been fighting with this script for several days and I'm finally at the end of my rope. Here's the scenario: -I have to add an entry into 1 table, called "calendar" -I must retrieve the...
2
by: William Ortenberg | last post by:
I'm creating a new row in a table with via the AddNew method, where the table ID is an Autonumber field. I want to capture the value of the ID after I create the row. There are no other unique...
17
by: Jellicle | last post by:
There are some sturct arrays (or struct vectors), such as: struct S1_t { double keyField ; ..// other fields } S1_t s1;
1
by: BobM | last post by:
i read the thread entitled "Convert Field Value String to Actual Field in Expression" It appeared that the final solutions got moore complex not less complex MY PROBLEM IS: 1. get a value from...
2
by: Thammarat charoenchai. | last post by:
After I use BindingSource.AddNew Method. (in my code is OrderBindingSource.AddNew). How can I set field value. like Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As...
1
by: john | last post by:
LINQ works a lot like an Access DB Recordset in the way it functions, one of the things I could do in Access is refer to the Fields thru a Variable as below allowing me to Iterate over the...
4
by: salzan | last post by:
Hello, I'm trying to insert a new record in my recordset using AddNew method. I can't figure out how to declare values for the controls in my recordset. Can someone give me an example. Thank you...
6
by: kpfunf | last post by:
Ok, I hope I can explain this one well enough! I have tables that contain similar data but are not all uniform; I use queries to create uniform data sets (same field names, formatting, etc.). I...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
0
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...

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.