Solved

Excel VBA get data from an Access table

Posted on 2013-05-29
4
512 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 view the data in a form or loop through it
I am used to the DataGridView in VB.net. Does anything like that
exist in VBA?


(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:murbro
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
Comment Utility
I suppose the closest thing to a GridView would be a listbox or a listview on  a userform

You could populate a listbox with something like this.
Sub LoadListBoxFromAccess()

    Dim cn As Object 
    Dim rst As Object
    Dim strQuery As String
    Dim arrData 


    '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 = "SELECT * FROM Employees"

    Set rst = CreateObject("ADODB.Recordset")
  
   rst.Open strQuery, cn


   arrData = rst.GetRows

   UserForm1.ListBox1.ColumnCount = rst.Fields.Count

   UserForm1.ListBox1.List = arrData

   ' or

   ' UserForm1.ListBox1.List = Application.Transpose(arrData)

   rst.Close
    Set rst = Nothing
   cn.Close

    Set cn = Nothing
    
End Sub 

Open in new window


The code for the listview would be more complicated.
0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks very much
0
 

Author Comment

by:murbro
Comment Utility
Thanks aikimark but imnorie's answer was enough. I appreciate the offer though
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Filter and delete 6 14
When a VBA code will not work in Excel 64bit? 2 17
onOpen 14 38
Redacting a row in Excel based on a term. 17 30
A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now