Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-11
7
Medium Priority
?
302 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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:Tracy
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

604 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