Solved

how do i get msaccess data in excel sheet dynamically thru VB code in an Excel macro?

Posted on 2011-02-11
7
298 Views
Last Modified: 2012-05-11
how do i get msaccess data in excel sheet dynamically thru VB code in an Excel macro?
basically i want to create a excel sheet which gets data from MSaccess table every few seconds without the end-user seeing the database password , so it is hidden from him when the macros is hidden
thanks
0
Comment
Question by:seediff
  • 4
7 Comments
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 34876096
You can pull Access data into an Excel workbook using CopyFromRecordset, as in the code below:
Dim appExcel As New Excel.Application
   Dim cnn As ADODB.Connection
   Dim wkb As Excel.Workbook
   Dim sht As Excel.Worksheet
   Dim strWorkbook As String
   Dim strRange As String
   Dim rst As ADODB.Recordset
   Dim rng As Excel.Range
   Dim strWorkbookName As String
   
   DoCmd.SetWarnings False
   strPrompt = "Enter workbook name (no extension)"
   strTitle = "Workbook name"
   strDefault = "New Access Data"
   strWorkbookName = InputBox(strPrompt, strTitle, strDefault)
   
   Set cnn = CurrentProject.Connection
   Set rst = New ADODB.Recordset
   
   'Create a recordset based on a select query.
   rst.Open Source:="qryContacts", _
      ActiveConnection:=cnn.ConnectionString, _
      CursorType:=adOpenForwardOnly
      
   'Export query
   Set wkb = appExcel.Workbooks.Add
   appExcel.Visible = True
   strWorkbook = Application.CurrentProject.Path & "\" & strWorkbookName
   wkb.SaveAs FileName:=strWorkbook
   Set sht = wkb.Sheets(1)
   strRange = "A1"
   Set rng = sht.Range(strRange)
   rng.CopyFromRecordset rst
   rst.Close

Open in new window

0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 34876097
As far as the "dynamically" part is concerned -- please be more specific about what you mean.
0
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 500 total points
ID: 34876112
The above code works from Access, but of course it could also work from Excel, pulling data from Access.  Here is an example of that:
Call GetCn(dbcon:=adoconn, _
      dbrs:=adors, _
      sqlstr:=strSQL, _
      dbfile:=strDatabaseNameAndPath, _
      usernm:="", _
      pword:="confidential")
   
   shtImport.Range(strTargetRange).CopyFromRecordset adors
   
==================================

Public Sub GetCn(ByRef dbcon As ADODB.Connection, _
   ByRef dbrs As ADODB.Recordset, sqlstr As String, _
   dbfile As String, usernm As String, pword As String)

On Error GoTo ErrorHandler

   Set dbcon = New ADODB.Connection
   
   With dbcon
      .Provider = "Microsoft.Jet.OLEDB.4.0"
      .Properties("Jet OLEDB:Database Password") = pword
      .Open ConnectionString:=dbfile
   End With
   
   Set dbrs = New ADODB.Recordset
   
   'Read-only
   dbrs.Open Source:=sqlstr, _
      ActiveConnection:=dbcon, _
      CursorType:=adOpenStatic, _
      LockType:=adLockReadOnly
   
ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number & "; Description: " & _
      Err.Description
   Resume ErrorHandlerExit

End Sub

Open in new window

0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:seediff
ID: 34876941
thanks a lot
this worked well
but only thing now is that excel converts even the numbers to a date format
if i change the format back to num bers, on refresh/rerunning the macro it overwrites again with some weird date formating
how can this be prevented?

0
 
LVL 24

Expert Comment

by:broomee9
ID: 35221180
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 35222405
You may need to apply a Date format to the appropriate columns, after doing the import.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

861 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