Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel VBA - insert data to an Access database

Posted on 2013-05-28
5
Medium Priority
?
14,523 Views
Last Modified: 2013-05-29
Hi

I have a form in an Excel 2003 database with two text boxes.
The text boxes are "Name" and "Number"
What VBA code would I use to insert a record into a table called "Employees"
in an Access database in my "My Documents" folder

Thanks
0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 668 total points
ID: 39200746
You'd have to open a connection to the database. If you want to use DAO to do this:

Dim dbs As DAO.Database
Set dbs = OpenDatabase("Full path to your db")

You can then "execute" a SQL statement:

dbs.Execute "INSERT INTO Employees(Name, Number) VALUES('" & Worksheets("Sheet1").Range("A2").Value & "','" & Worksheets("Sheet1").Range("A3") & "')"

Obviously you'd need to change the path, worksheetname and Range.
0
 

Author Comment

by:Murray Brown
ID: 39201249
Hi. Thanks very much for the response, I tried that with my Access database and it didn't work. It said unrecognized database format. I am using Access 2007
0
 
LVL 35

Assisted Solution

by:[ fanpages ]
[ fanpages ] earned 668 total points
ID: 39201354
Hi,

From the Microsoft Visual Basic window within Microsoft Excel 2003, click the "Tools" menu, then the "References..." menu option.

Ensure that you have the following Reference selected (checked/ticked):
"Microsoft Office 12.0 Access Database Engine Object Library"

I am guessing you have the following selected instead:
"Microsoft DAO 3.6 Object Library"

BFN,

fp.
0
 
LVL 16

Assisted Solution

by:Jerry Paladino
Jerry Paladino earned 664 total points
ID: 39202625
The code below uses ADO to establish the connection.
Sub ExportDataToAccess()

    Dim cn As Object
    Dim strQuery As String
    Dim Name As String
    Dim Number As String
    Dim myDB As String

    'Initialize Variables
    Name = Worksheets("Sheet1").Range("A2").Value
    Number = Worksheets("Sheet1").Range("B2").Value
    
   'myDB = "C:\Users\username\Documents\EMP.accdb"
    myDB = "replace with the fully qualified path to your Access Db"

    Set cn = CreateObject("ADODB.Connection")

    With cn
        .Provider = "Microsoft.ACE.OLEDB.12.0"    'For *.ACCDB Databases
        .ConnectionString = myDB
        .Open
    End With

    strQuery = "INSERT INTO Employees ([Name], [Number]) " & _
               "VALUES (""" & Name & """, " & Number & "); "

    cn.Execute strQuery
    cn.Close
    Set cn = Nothing
    
End Sub

Open in new window

Enter the path to your Access database in the myDB variable above and adjust the ranges for the Name and Number variables for your worksheet.
Q-28140336-ADO-Write-to-AccessDb.xlsm
EMP.accdb
0
 

Author Closing Comment

by:Murray Brown
ID: 39203771
Thank you all very much
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question