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
Solved

Excel VBA - insert data to an Access database

Posted on 2013-05-28
5
10,758 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

808 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