Solved

Excel VBA - insert data to an Access database

Posted on 2013-05-28
5
12,319 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
[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 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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