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

Excel VBA create new record in Access Table - 2007 Office

Hi, Good Morning Experts!
I'm trying to create a new record in Access from Excel VBA. I've got a button in the Excel file, that when pushed will put data from the excel sheet into a NEW record in Access. Here's what I've got so far, but it keeps failing on "Set rstTable = oAcc.Recordset.Open("C:\Documents and Settings\ecook\My Documents\SharePoint.mdb")"

Expand|Select|Wrap|Line Numbers
  1. Sub cmdPassDownItem_Click()
  2. Dim oAcc As Object
  3. Dim rstTable As Object
  4.  
  5. Set oAcc = CreateObject("Access.Application")
  6. Set rstTable = oAcc.Recordset.Open("C:\Documents and Settings\ecook\My Documents\SharePoint.mdb")
  7.     r = 20 ' the start row in the worksheet
  8.     Do While Len(Range("A" & r).Formula) > 0
  9.     ' repeat until first empty cell in column A
  10.         With rs
  11.             .AddNew ' create a new record
  12.             ' add values to each field in the record
  13.             .Fields("Title") = Range("A" & r).Value
  14.             .Fields("Country") = Range("B" & r).Value
  15.  
  16.             ' add more fields if necessary...
  17.             .Update ' stores the new record
  18.         End With
  19.         r = r + 1 ' next row
  20.     Loop
  21.     rs.Close
  22.     Set rs = Nothing
  23.     db.Close
  24.     Set db = Nothing
  25. End Sub  
Feb 23 '12 #1

✓ answered by ADezii

The general idea would be something similar to:
Expand|Select|Wrap|Line Numbers
  1. Dim oAcc As Object
  2. Dim rstTable As Object
  3.  
  4. Set oAcc = CreateObject("Access.Application")
  5.  
  6. 'Open Database in Microsoft Access window
  7. oAcc.OpenCurrentDatabase "C:\Documents and Settings\ecook\My Documents\SharePoint.mdb", True
  8.  
  9. oAcc.Visible = False
  10.  
  11. 'Create a Recordset based on <Table name>
  12. Set rstTable = oAcc.CurrentDb.OpenRecordset("<Table Name>")
  13.  
  14. With rstTable
  15.   .AddNew
  16.     ![Title] = "<Value>"
  17.     ![Country] = "<Value>"
  18.   .Update
  19. End With
  20.  
  21. oAcc.Quit
  22. Set oAcc = Nothing

3 7101
ADezii
8,834 Expert 8TB
The general idea would be something similar to:
Expand|Select|Wrap|Line Numbers
  1. Dim oAcc As Object
  2. Dim rstTable As Object
  3.  
  4. Set oAcc = CreateObject("Access.Application")
  5.  
  6. 'Open Database in Microsoft Access window
  7. oAcc.OpenCurrentDatabase "C:\Documents and Settings\ecook\My Documents\SharePoint.mdb", True
  8.  
  9. oAcc.Visible = False
  10.  
  11. 'Create a Recordset based on <Table name>
  12. Set rstTable = oAcc.CurrentDb.OpenRecordset("<Table Name>")
  13.  
  14. With rstTable
  15.   .AddNew
  16.     ![Title] = "<Value>"
  17.     ![Country] = "<Value>"
  18.   .Update
  19. End With
  20.  
  21. oAcc.Quit
  22. Set oAcc = Nothing
Feb 23 '12 #2
Thank you very much, this works like a charm!!
Feb 27 '12 #3
ADezii
8,834 Expert 8TB
You are quite welcome.
Feb 27 '12 #4

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

Similar topics

3
by: Mads Petersen | last post by:
I'm stuck in this code. Hope you can and will help me. I launch it from excel. I have made the following code work, but not as i whant. I need the ranges to be working with something like xlDown....
2
by: VMI | last post by:
Assuming I have a datarow (or a datarow collection), would it possible to create an Access table and dump the datarow(s) into that Access table? I wouldn't even need to create the fields in the...
1
by: VMI | last post by:
Assuming I have a datarow (or a datarow collection), would it possible to create an Access table and dump the datarow(s) into that Access table? I wouldn't even need to create the fields in the...
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...
1
by: dmkeith2 | last post by:
This is probably easy to do but I can't find it in the groups. How do I paste into an excel document without the MS Access headers? I just want to paste the data. Thanks
2
by: saddist | last post by:
Hello, I have and excel sheet with fields: Name | Surname | Dept. When I execute the following code, it imports 16 empty rows + filled ones DoCmd.TransferSpreadsheet acImport = 0,...
2
by: ALaurie10 | last post by:
I have an excel file in Accounting Format, my feild names go down the page and the data goes across. There are four values for each feild. After importing my excel file into a table, the feilds are...
6
by: grego9 | last post by:
I am trying to return a value from an access database by looking up a value in excel. This is the code I have copied from another source - but I cannot get it to work - I keep getting a run time...
3
by: nspader | last post by:
Hello All, This seems like it should be simple but I cannot figure it out. I am trying to set up a button click to import an excel file to a temp table then run an append query to add it to an...
6
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...

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.