[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Excel VBA Update Access table

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

834 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