473,395 Members | 1,581 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.

Adding a New Record to Access form using Visual Basic in Excel

I am searching for a way to create multiple records in Access 2000 from
a worksheet created in Excel. I'm pretty much self-taught with Visual
Basic so forgive me if this poorly described. Basically what happens
is, from Access, while in Form1, I click a button that opens Excel and
my worksheet. This worksheet contains a button with the following code
( I've replaced the sections where I assign cell values to the
variables with ellipses):

Dim oAcc As Object
...
Set oAcc = GetObject(, "Access.Application")
...
If oAcc.Forms!Form1.NewRecord = True Then ' check to ensure I don't
write over old data
oAcc.Forms!Form1![Amount] = curTotal
oAcc.Forms!Form1![Description] = strValue
oAcc.Forms!Form1![Notes] = strNotes
End if ' oAcc.Forms!Form1.NewRecord = True

This works just fine for one record; I close out excel with the program
and return to Access with the form filled in. However, now I'm trying
to expand to calculate multiple rows of the same formulas and create a
new record in Access for each row. From Access I would just run
DoCmd.GoToRecord , , acNewRec but I don't know how to reference
something like that from Excel. Any suggestions?

Feb 27 '06 #1
2 4044
On 26 Feb 2006 16:57:21 -0800, "super_dave_42"
<su***********@hotmail.com> wrote:

oAcc.DoCmd.GoToRecord,,acNewRec

-Tom.

I am searching for a way to create multiple records in Access 2000 from
a worksheet created in Excel. I'm pretty much self-taught with Visual
Basic so forgive me if this poorly described. Basically what happens
is, from Access, while in Form1, I click a button that opens Excel and
my worksheet. This worksheet contains a button with the following code
( I've replaced the sections where I assign cell values to the
variables with ellipses):

Dim oAcc As Object
...
Set oAcc = GetObject(, "Access.Application")
...
If oAcc.Forms!Form1.NewRecord = True Then ' check to ensure I don't
write over old data
oAcc.Forms!Form1![Amount] = curTotal
oAcc.Forms!Form1![Description] = strValue
oAcc.Forms!Form1![Notes] = strNotes
End if ' oAcc.Forms!Form1.NewRecord = True

This works just fine for one record; I close out excel with the program
and return to Access with the form filled in. However, now I'm trying
to expand to calculate multiple rows of the same formulas and create a
new record in Access for each row. From Access I would just run
DoCmd.GoToRecord , , acNewRec but I don't know how to reference
something like that from Excel. Any suggestions?


Feb 27 '06 #2
Thanks -Tom,

Unfortunately, 'oAcc.DoCmd.GoToRecord,,acNewRec' did not work. On the
up side, it gave me no error messages. I ended up with the last row of
data transfered into the correct fields of the current record, so it
would appear each row was just overwritten by the next. I came across a
Recordset object in the help files, and the AddNew method seems like a
good alternative to DoCmd. However, Excel does not recognize the
Recordset object (fyi, I have the MS Access 9.0 Obj Lib added to my
References). The closest thing I could see is an AccessObject type. So,
I tried the following:

Dim oAcc As Object
Dim rstTable As AccessObject
...
Set oAcc = GetObject(, "Access.Application")
Set rstTable = oAcc.Forms!Form1.Recordset
...
rstTable.AddNew
...

That didn't work either, and rstTable never changed its value from
"Nothing" after the Set command was processed. So I then thought to
just let VB handle the job of determining the type.

Dim oAcc As Object
Dim rstTable As Object
...
Set oAcc = GetObject(, "Access.Application")
Set rstTable = oAcc.Forms!Form1.Recordset
...
rstTable.AddNew
...

Voila! A brand-spanking-new record for each row of my data. I can go
sleep now. :^)

p.s. One bad side effect, which doesn't bother me but might be of
interest to others, is the fact that the NewRecord property
(oAcc.Forms!Form1.NewRecord) didn't seem to change value when I used
this new code. Honestly, I don't know if it even changed with my
original code; I might have just always been using a new record. I
suggest you test more thoroughly than I have done.

Feb 27 '06 #3

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

Similar topics

7
by: Mathew Hill | last post by:
I am a beginner to the more technical aspects of Microsoft Access (2000) and was wondering if any one can help. I have 3 buttons on a form which add, delete and search for a record. However, when I...
9
by: Pete | last post by:
Does anyone have a simple html vbscript or other type of snippet they can share that appends a record to a access database via ADO or DAO? I would like to allow users that don't have Microsoft...
8
by: Jerry | last post by:
I have an off-the-shelf app that uses an Access database as its backend. One of the tables contains a field with an "OLE Object" datatype. I'm writing some reports against this database, and I...
2
by: brenda.stow | last post by:
error msg " An error occured while referencing the object. You tried to run a visual basic procedure that improperly references a property or method of an object" This msg occurs everytime I add a...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: Omar | last post by:
Hi Developers, I am trying to access an Excel data file through a VB.Net application. I have the following code: =================================== VB.Net Code =================== Dim...
3
by: James Wong | last post by:
Dear all, I have an old VB6 application which can create and access Excel object. The basic definition statements are as follows: Dim appExcel As Object Dim wkb1 As Excel.Workbook Dim wks1...
3
by: Tull Clancey | last post by:
Hi, hope someone can help with the following, I'm a bit stuck! I have a database table (Access) with an AutoNumber field. When I add data to the table I want to use the AutoNumber field, format...
11
DUNXALEARE
by: DUNXALEARE | last post by:
Hello everyone! I have a new assignment. I just dont know how to create a data report using ms excell. I need to search particular group of data/records using Visual basic 6. in MS Access and...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.