Solved

Excel VBA - insert data to an Access database

Posted on 2013-05-28
5
9,835 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DBF to ... Converter 5 45
Excel Conditional Formatting in a Macro 4 26
Excel Hyperlink Question 4 31
simple vba query 8 22
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

867 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

15 Experts available now in Live!

Get 1:1 Help Now