Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VBA get data from an Access table

Posted on 2013-05-29
4
Medium Priority
?
589 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:Murray Brown
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Norie earned 2000 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:Murray Brown
ID: 39212512
Thanks very much
0
 

Author Comment

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

Featured Post

Technology Partners: 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 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…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

972 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