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

AddNew function in Access VBA

Hi,
I am using the AddNew function in VBA to populate my access table from
data in excel. The following is the piece of code:

With WriteRS
.AddNew
!column_Names(0) = "acctNo"
!B = "CoName"
!C = "acctMan"
.Update
.Bookmark = LastModified
End With
The column names in the access tables are A, B and C. If I hard code
these values it works fine.However, if I try to make it dynamic as done
above (!column_Names(0) = "acctNo") by storing column names in an
array and then retriving it it does not work. Is there a way I can get
the column names dynamically in AddNew function or is there any other
function that serves the same purpose as AddNew and allows dynamic
population of column names.

Thanks & Regards,
Amey Gupte.
Research Assistant,
Supply Chains Systems Lab,
Texas A&M University.

Sep 20 '06 #1
5 23576
Hi Amey

you can try something like this:

Dim DB As DAO.Database, RS As DAO.RecordSet, i As Integer
Set DB = CurrentDB
Set RS = DB.OpenRecordset("tbl1")
For i = 0 to Ubound(excelArray)
RS.AddNew
RS(0) = excelArray(i)
RS.Update
Next

Note: the excelArray in this example only has one column. If you use
ADO to read a range of Excel Values with say 10 columns you can do this:

Dim ... i As Integer, j As Integer
Do While Not RSexcel.EOF
RS.AddNew
For j = 0 to RSexcel.Fields.Count - 1
RS(j) = RSexcel(j)
Next
RS.Update
RSexcel.MoveNext
Loop
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Sep 20 '06 #2

I take it that column_Names is an array of the column names? If so then:-

With WriteRS
.AddNew
.Fields(column_Names(0)) = "acctNo"
!B = "CoName"
!C = "acctMan"
.Update
.Bookmark = LastModified
End With
--

Terry Kreft
<am********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi,
I am using the AddNew function in VBA to populate my access table from
data in excel. The following is the piece of code:

With WriteRS
.AddNew
!column_Names(0) = "acctNo"
!B = "CoName"
!C = "acctMan"
.Update
.Bookmark = LastModified
End With
The column names in the access tables are A, B and C. If I hard code
these values it works fine.However, if I try to make it dynamic as done
above (!column_Names(0) = "acctNo") by storing column names in an
array and then retriving it it does not work. Is there a way I can get
the column names dynamically in AddNew function or is there any other
function that serves the same purpose as AddNew and allows dynamic
population of column names.

Thanks & Regards,
Amey Gupte.
Research Assistant,
Supply Chains Systems Lab,
Texas A&M University.

Sep 20 '06 #3
You want to use the Fields collection of the recordset. I inserted a sample.

am********@gmail.com schreef:
Hi,
I am using the AddNew function in VBA to populate my access table from
data in excel. The following is the piece of code:

With WriteRS
.AddNew
!column_Names(0) = "acctNo"
.Fields(0) = "acctNo"
!B = "CoName"
!C = "acctMan"
.Update
.Bookmark = LastModified
End With
--
Bas Cost Budde
Holland
Sep 20 '06 #4
Hi Rich,
Thanks for the reply and sorry for the delay in replying. I tried the
code as suggested by you and it worked fine. However, as this method
does an Update after each row there were some performance issues, hence
we resorted to the DoCmd.TransferSpreadSheet method.

Thanks & Regards,
Amey Gupte.

Rich P wrote:
Hi Amey

you can try something like this:

Dim DB As DAO.Database, RS As DAO.RecordSet, i As Integer
Set DB = CurrentDB
Set RS = DB.OpenRecordset("tbl1")
For i = 0 to Ubound(excelArray)
RS.AddNew
RS(0) = excelArray(i)
RS.Update
Next

Note: the excelArray in this example only has one column. If you use
ADO to read a range of Excel Values with say 10 columns you can do this:

Dim ... i As Integer, j As Integer
Do While Not RSexcel.EOF
RS.AddNew
For j = 0 to RSexcel.Fields.Count - 1
RS(j) = RSexcel(j)
Next
RS.Update
RSexcel.MoveNext
Loop
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Sep 26 '06 #5
Hi Terry,
Thanks for the reply and sorry for the delay in replying. Your
assumption is correct, column_Names is the array of column names. I
tried the code as suggested by you, but it was not working correctly,
may be I was doing some mistakes. However, as this method does an
Update after each row there were some performance issues, hence we
resorted to the DoCmd.TransferSpreadSheet method.

Thanks & Regards,
Amey Gupte.

Terry Kreft wrote:
I take it that column_Names is an array of the column names? If so then:-

With WriteRS
.AddNew
.Fields(column_Names(0)) = "acctNo"
!B = "CoName"
!C = "acctMan"
.Update
.Bookmark = LastModified
End With
--

Terry Kreft
<am********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi,
I am using the AddNew function in VBA to populate my access table from
data in excel. The following is the piece of code:

With WriteRS
.AddNew
!column_Names(0) = "acctNo"
!B = "CoName"
!C = "acctMan"
.Update
.Bookmark = LastModified
End With
The column names in the access tables are A, B and C. If I hard code
these values it works fine.However, if I try to make it dynamic as done
above (!column_Names(0) = "acctNo") by storing column names in an
array and then retriving it it does not work. Is there a way I can get
the column names dynamically in AddNew function or is there any other
function that serves the same purpose as AddNew and allows dynamic
population of column names.

Thanks & Regards,
Amey Gupte.
Research Assistant,
Supply Chains Systems Lab,
Texas A&M University.
Sep 26 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mark Dicken | last post by:
Hi All, I am trying to Pass A Collection To A FUNCTION ??? (Access 2000) I have a Class called BO (for Business Objects) Within BO I have a Function called ShowCollection Public Sub...
0
by: ss | last post by:
i read a few posts about global function access. well i am not interested in global functions. rather, i am seeking for a way to may my call in ASPX pages but not the code behind. for example:...
16
by: bobueland | last post by:
Look at the code below: # mystringfunctions.py def cap(s): print s print "the name of this function is " + "???" cap ("hello")
2
by: pkpatil | last post by:
Hi, Can a private composite object in a class access private or protected members of base class? For e.g. class composite { void memberFunction(); };
8
by: Sullivan WxPyQtKinter | last post by:
I am confused by the following program: def f(): print x x=12345 f() result is: 12345
3
by: dolphin | last post by:
Hello everyone! Can a static member function access non-static member? I think it is illegal.Is it right?
8
by: mguy27 | last post by:
We have about 2 dozen security officers who patrol about 120 buildings. They find defective or unlocked doors and write a "Trouble Report" on the door. This card gets turned in, where I have designed...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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
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...

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.