98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Importing Data from MS Access
Importing Data from MS Access
Resolved · Medium Priority · Version 2010
Sarah has attended:
Excel VBA Advanced course
Importing Data from MS Access
Hello,
I'm wondering if anybody can point me in the right direction here - I've probably gone wrong somewhere really silly!
I have written the following code (well most of it = some of it is clearly borrowed from the coursework!), which looks through an array and creates an Access recordset for each variable within the array. It's then ordered by the Version ID so I only need the first record in the table, which I want to return to Excel.
The problem is, it returns no results. If the query is run manually then it works fine so I must have gone wrong linking to the database somewhere...
Ignore the Range("F7").Value for now - that's just there to test if the code works & I'll be adding a rowcount to the loop when this bit works.
Thank you lots! :)
Kind Regards,
Sarah
Set Con1 = New ADODB.Connection
Con1.ConnectionString = ConnString 'Zero in on the database
'Now open up the connection
Con1.Open
For d = LBound(PRNumber) To UBound(PRNumber)
If PRNumber(d) <> "" Then
Set ForecastRS = New ADODB.Recordset
ForecastSQL = "SELECT ForecastTable.[Version ID], ForecastDetailTable.Total FROM ForecastTable "
ForecastSQL = ForecastSQL & "INNER JOIN ForecastDetailTable ON ForecastTable.[Forecast ID] = ForecastDetailTable.[Forecast ID] "
ForecastSQL = ForecastSQL & "WHERE ForecastTable.PRNumber = 'PRNumber(d)' And ForecastTable.VersionStatus = '" & CurrentForecastVersion & "' And ForecastDetailTable.Year = 'Whole Project'"
ForecastSQL = ForecastSQL & " ORDER BY ForecastTable.[Version ID] DESC"
Call ForecastRS.Open(ForecastSQL, Con1)
If ForecastRS.RecordCount > 0 Then
ForecastRS.MoveFirst
Range("F7").Value = ForecastRS!Total
End If
Set ForecastRS = Nothing
End If
Next d
ForecastRS.Close
Con1.Close
Set Conn1 = Nothing
Set ForecastRS = Nothing
RE: Importing Data from MS Access
Hi Sarah
Thanks for getting in touch. Most of it looks OK, but there's one line I'm questioning:
Call ForecastRS.Open(ForecastSQL, Con1)
The Call keyword is usually reserved for running another macro.
What happens when you get rid of Call?
Kind regards
Gary Fenn
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
RE: Importing Data from MS Access
Hi Gary,
Thanks for getting back to me so quickly. :)
I've taken the Call keyword out but am now getting a syntax error - it seems to be expecting an "=".
I tried changing it to:
ForecastRS.Open = (ForecastSQL) &
ForecastRS.Open = (ForecastSQL, Con1)
but neither of these work. I expect it's something really small I'm missing!
Kind Regards,
Sarah
RE: Importing Data from MS Access
Hi Sarah
Have you tried this:
ForecastRS.Open ForecastSQL
This should process your concatenated SQL string. OPEN can take an = sign, when it does it is expecting to see a table at the point.
Kind regards
Gary Fenn
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
RE: Importing Data from MS Access
Hi Gary,
I've sorted this now - I gave up & just put the query into the database so I now have this, which gives me the data dump I need:
Set Con1 = New ADODB.Connection
Con1.ConnectionString = ConnString 'Zero in on the database
'Now open up the connection
Con1.Open
Set ForecastRS = New ADODB.Recordset 'Fire it up
ForecastRS.Open Source:=ForecastSQL, ActiveConnection:=Con1
'Copy data over - no formatting or column headings as we just need the data
ThisWorkbook.Sheets("AccessData").Range("A1").CopyFromRecordset ForecastRS
ForecastRS.Close
Con1.Close
Set Conn1 = Nothing
Set ForecastRS = Nothing
Thanks for your help! :)
Kind Regards,
Sarah
RE: Importing Data from MS Access
Hi Sarah
Great approach! Glad you got it sorted.
Kind regards
Gary Fenn
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
Training information:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Excel tip:Generate randon numbersSome types of analysis require you to use randomly generated numbers. You can also use randomly generated numbers to quickly populate an Excel spreadsheet. There's an easy function you can use to do this automatically. Here are a few of the ways you can use it: |