Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Import - Access Data through ADO

Hi,

I am doing a project in Excel, where I have to import data from an access
database to current workbook. Data comes from an access query, which has some
custom functions (UDF), which work fine when I run the query in Access. When
I try ADO (from Excel macro) to run an SQL statement on that query, It pops
up an error that my custom function in Access can not be calculated. Is there
a way around this issue? I am posting the code I have written. Any help is
much appreciated.

Sub GetDataFromAccess()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim qrystr As String
qrystr = CreateQryStr()

Set con = New ADODB.Connection
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;data source=" &
CostCentres.Range("dbpath")

Set rs = New ADODB.Recordset
rs.Open qrystr, con, adOpenStatic, adLockReadOnly, adCmdText
Sheets("Data").Range("A2").CopyFromRecordset rs

Ok:
If rs.State = adStateOpen Then
rs.Close
End If

If con.State = adStateOpen Then
con.Close
End If
Set rs = Nothing
Set con = Nothing
Exit Sub

ErrInMacro:
MsgBox Err.Description
Resume Ok
End Sub

Function CreateQryStr() As String
Dim LastRow As Integer
Dim qrystr As String
Dim i As Integer
Dim quote As String
Dim Middle As String
Dim sLast As String


quote = """"
Middle = quote & " Or (CostCentre)=" & quote
sLast = "));"
With CostCentres

LastRow = .Cells(.Rows.Count, 4).End(xlUp).Row

'If there is no cost code on Cost centres page then exit
If .Cells(LastRow, 4).Value = "Cost Centres" Then
MsgBox "No cost codes. Exiting...", vbCritical, "Error"
GoTo Ok
End If

'create qry string
qrystr = "SELECT AID, FirstName, Status, HC, Maternity, CostCentre,
Area, Period"
qrystr = qrystr & " FROM qryHCBase WHERE "
qrystr = qrystr & "(((CostCentre)=" & quote

For i = 5 To LastRow
If i = 5 Then
qrystr = qrystr & .Cells(i, 4).Value & Middle
Else
qrystr = qrystr & .Cells(i, 4).Value
End If
Next

qrystr = qrystr & quote & sLast
End With

CreateQryStr = qrystr

Ok:
Exit Function

ErrInMacro:
MsgBox Err.Description
CreateQryStr = ""
Resume Ok
End Function

--
Anant
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 249
Default Import - Access Data through ADO

Might want to use the TransferSpreadsheet method from Access.

http://msdn2.microsoft.com/en-us/lib...ffice.10).aspx

--

Regards,

Dave Patrick ....Please no email replies - reply in newsgroup.
Microsoft Certified Professional
Microsoft MVP [Windows]
http://www.microsoft.com/protect

"Anant Basant" wrote:
Hi,

I am doing a project in Excel, where I have to import data from an access
database to current workbook. Data comes from an access query, which has
some
custom functions (UDF), which work fine when I run the query in Access.
When
I try ADO (from Excel macro) to run an SQL statement on that query, It
pops
up an error that my custom function in Access can not be calculated. Is
there
a way around this issue? I am posting the code I have written. Any help is
much appreciated.

Sub GetDataFromAccess()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim qrystr As String
qrystr = CreateQryStr()

Set con = New ADODB.Connection
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;data source=" &
CostCentres.Range("dbpath")

Set rs = New ADODB.Recordset
rs.Open qrystr, con, adOpenStatic, adLockReadOnly, adCmdText
Sheets("Data").Range("A2").CopyFromRecordset rs

Ok:
If rs.State = adStateOpen Then
rs.Close
End If

If con.State = adStateOpen Then
con.Close
End If
Set rs = Nothing
Set con = Nothing
Exit Sub

ErrInMacro:
MsgBox Err.Description
Resume Ok
End Sub

Function CreateQryStr() As String
Dim LastRow As Integer
Dim qrystr As String
Dim i As Integer
Dim quote As String
Dim Middle As String
Dim sLast As String


quote = """"
Middle = quote & " Or (CostCentre)=" & quote
sLast = "));"
With CostCentres

LastRow = .Cells(.Rows.Count, 4).End(xlUp).Row

'If there is no cost code on Cost centres page then exit
If .Cells(LastRow, 4).Value = "Cost Centres" Then
MsgBox "No cost codes. Exiting...", vbCritical, "Error"
GoTo Ok
End If

'create qry string
qrystr = "SELECT AID, FirstName, Status, HC, Maternity, CostCentre,
Area, Period"
qrystr = qrystr & " FROM qryHCBase WHERE "
qrystr = qrystr & "(((CostCentre)=" & quote

For i = 5 To LastRow
If i = 5 Then
qrystr = qrystr & .Cells(i, 4).Value & Middle
Else
qrystr = qrystr & .Cells(i, 4).Value
End If
Next

qrystr = qrystr & quote & sLast
End With

CreateQryStr = qrystr

Ok:
Exit Function

ErrInMacro:
MsgBox Err.Description
CreateQryStr = ""
Resume Ok
End Function

--
Anant


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Import - Access Data through ADO

Thanks Dave, I will try that.
--
Anant


"Dave Patrick" wrote:

Might want to use the TransferSpreadsheet method from Access.

http://msdn2.microsoft.com/en-us/lib...ffice.10).aspx

--

Regards,

Dave Patrick ....Please no email replies - reply in newsgroup.
Microsoft Certified Professional
Microsoft MVP [Windows]
http://www.microsoft.com/protect

"Anant Basant" wrote:
Hi,

I am doing a project in Excel, where I have to import data from an access
database to current workbook. Data comes from an access query, which has
some
custom functions (UDF), which work fine when I run the query in Access.
When
I try ADO (from Excel macro) to run an SQL statement on that query, It
pops
up an error that my custom function in Access can not be calculated. Is
there
a way around this issue? I am posting the code I have written. Any help is
much appreciated.

Sub GetDataFromAccess()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim qrystr As String
qrystr = CreateQryStr()

Set con = New ADODB.Connection
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;data source=" &
CostCentres.Range("dbpath")

Set rs = New ADODB.Recordset
rs.Open qrystr, con, adOpenStatic, adLockReadOnly, adCmdText
Sheets("Data").Range("A2").CopyFromRecordset rs

Ok:
If rs.State = adStateOpen Then
rs.Close
End If

If con.State = adStateOpen Then
con.Close
End If
Set rs = Nothing
Set con = Nothing
Exit Sub

ErrInMacro:
MsgBox Err.Description
Resume Ok
End Sub

Function CreateQryStr() As String
Dim LastRow As Integer
Dim qrystr As String
Dim i As Integer
Dim quote As String
Dim Middle As String
Dim sLast As String


quote = """"
Middle = quote & " Or (CostCentre)=" & quote
sLast = "));"
With CostCentres

LastRow = .Cells(.Rows.Count, 4).End(xlUp).Row

'If there is no cost code on Cost centres page then exit
If .Cells(LastRow, 4).Value = "Cost Centres" Then
MsgBox "No cost codes. Exiting...", vbCritical, "Error"
GoTo Ok
End If

'create qry string
qrystr = "SELECT AID, FirstName, Status, HC, Maternity, CostCentre,
Area, Period"
qrystr = qrystr & " FROM qryHCBase WHERE "
qrystr = qrystr & "(((CostCentre)=" & quote

For i = 5 To LastRow
If i = 5 Then
qrystr = qrystr & .Cells(i, 4).Value & Middle
Else
qrystr = qrystr & .Cells(i, 4).Value
End If
Next

qrystr = qrystr & quote & sLast
End With

CreateQryStr = qrystr

Ok:
Exit Function

ErrInMacro:
MsgBox Err.Description
CreateQryStr = ""
Resume Ok
End Function

--
Anant


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 249
Default Import - Access Data through ADO

You're welcome.

--

Regards,

Dave Patrick ....Please no email replies - reply in newsgroup.
Microsoft Certified Professional
Microsoft MVP [Windows]
http://www.microsoft.com/protect

"Anant Basant" wrote:
Thanks Dave, I will try that.
--
Anant

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Import Data from Access toby131 Excel Discussion (Misc queries) 3 October 27th 09 11:47 PM
Can't import data to access Amin Excel Discussion (Misc queries) 3 September 26th 08 04:56 AM
Import Data From Access! Please Help!! greenfalcon[_10_] Excel Programming 1 July 10th 06 08:28 PM
Problem with data import from Access Frankie Excel Discussion (Misc queries) 0 May 20th 06 11:28 AM
Import Data from Access via DAO LHC01 Excel Programming 3 August 12th 04 11:24 AM


All times are GMT +1. The time now is 03:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"