?
Solved

Excel VBA Update Access table

Posted on 2013-05-29
3
Medium Priority
?
532 Views
Last Modified: 2013-06-01
Hi

In my Excel VBA I am trying to interact with an Access database
I was given the following two ways to insert records.
Now I need the user to Update certain records.
What similar code would I use to do this?


(1)
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.

(2) 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
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
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
gowflow earned 2000 total points
ID: 39204958
Well you can try something in these lines

Sub UpdateSpecificRecord(FmStr As String, ToStr As String)

    Dim dbs As Object
    Dim rs As Object
    Dim SQL As String
    
    SQL = "SELECT * From Employees"
    Set dbs = OpenDatabase("Full path to your db")
    
    Set rs = dbs.OpenRecordset(SQL)
    
    rs.FindFirst "Name = '" & FmStr & "'"
    If rs.NoMatch Then
        MsgBox ("Record with Name = '" & FmStr & "' was not found !")
    Else
        rs.Edit
        rs("Name") = ToStr
        rs.Update
        MsgBox ("Record with Name = '" & FmStr & "' was found and updated to '" & ToStr)
    End If

End Sub

Open in new window


You enter the Sub with the value in FmStr with the Name you wish to change and also supply the ToStr that you wanted to be replaced with. If the item is found it replaces it if not it gives a message.

Rgds/gowflow
0
 

Author Comment

by:Murray Brown
ID: 39212516
Thanks very much
0
 
LVL 31

Expert Comment

by:gowflow
ID: 39212561
Your welcome
gowflow
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

770 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