Solved

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

Posted on 2011-02-11
7
294 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:seediff
Comment Utility
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
Comment Utility
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
Comment Utility
You may need to apply a Date format to the appropriate columns, after doing the import.
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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

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

13 Experts available now in Live!

Get 1:1 Help Now