Solved

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

Posted on 2011-02-11
7
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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.

691 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