Solved

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

Posted on 2011-02-11
7
296 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
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.

 

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

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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;…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

895 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

17 Experts available now in Live!

Get 1:1 Help Now