Solved

Excel VBA - insert data to an Access database

Posted on 2013-05-28
5
9,380 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:murbro
5 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 167 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:murbro
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 167 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 166 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:murbro
ID: 39203771
Thank you all very much
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel VBA - copying formats 7 22
Dynamic Excel Countdown Graphic 21 23
Added a column screws up code 5 17
In or Between 2 0
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now