[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Excel VBA Update Access table

Posted on 2013-05-29
3
Medium Priority
?
581 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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

656 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