Solved

Excel VBA get data from an Access table

Posted on 2013-05-29
4
521 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
ID: 39206215
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
ID: 39212512
Thanks very much
0
 

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

920 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

15 Experts available now in Live!

Get 1:1 Help Now