PC Review


Reply
Thread Tools Rate Thread

connect to Access 2007

 
 
Dave B
Guest
Posts: n/a
 
      21st Oct 2007
In 2005, thanks to code posted on this board, I created a workbook
which connects to Access & retrieves data from recordsets. The code
works fine, except now when I try to use it with Access 2007 I get the
error:
"Unrecognized database format 'C:\...\myfile.accdb'

Is there a way to update the code so it will work with Access 2007?
Thanks in advance for any help! Here is the relevant section:

Public cnMinistry As ADODB.Connection
Private Function ConnectToDatabase() As Boolean
'instantiate the connection & connect
Set cnMinistry = New Connection
cnMinistry.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=" & strFileToOpen & ";"
cnMinistry.Open '<--- error happens here
If cnMinistry.State = adStateOpen Then ConnectToDatabase = True
End Function

References:
Visual Basic for Applications
Microsoft Excel 11.0 Object Library
OLE Automation
Microsoft Office 11.0 Object Library
Microsoft Forms 2.0 Object Library
Microsoft.ActiveX Data Objects 2.5 Library

Some more code that may have to change:

Private Sub ImportAccessTable(cn As ADODB.Connection, TableName As
String, TargetRange As Range)
Dim rs As ADODB.Recordset, intColIndex As Integer
Set TargetRange = TargetRange.Cells(1, 1)
Set rs = New ADODB.Recordset
rs.Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable
For intColIndex = 0 To rs.Fields.Count - 1 ' the field names
TargetRange.Offset(0, intColIndex).Value =
rs.Fields(intColIndex).Name
Next
TargetRange.Offset(1, 0).CopyFromRecordset rs ' the recordset data
rs.Close
Set rs = Nothing
End Sub

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q3JpbmdpbmcgRHJhZ29u?=
Guest
Posts: n/a
 
      21st Oct 2007
I don't know if it's related to the problem or not, but you may need to
change your references to point to the Excel 12 and Office 12 libraries.
Those libraries may be available on PCs which have been upgraded from Office
2003 to Office 2007, but will probably not be available on computers with
only Office 2007.

To check your connection string, try the following:
In Excel, start recording a macro.
Make a manual connection to the database (I use Excel 2003, so the menu may
be different, but something like Data - Import External Data - New Database
Query). Go through the steps to create a query, but it doesn't matter what
the query is as we're only checking the connection string, so just choose any
field from an appropriate table.
Once the data has been returned to Excel, stop the macro recording and look
at what's been recorded. Does the connection string match what you have in
your macro?
I'm guessing that if the database has been upgraded to Access 2007, then the
provider needs to change.

"Dave B" wrote:

> In 2005, thanks to code posted on this board, I created a workbook
> which connects to Access & retrieves data from recordsets. The code
> works fine, except now when I try to use it with Access 2007 I get the
> error:
> "Unrecognized database format 'C:\...\myfile.accdb'
>
> Is there a way to update the code so it will work with Access 2007?
> Thanks in advance for any help! Here is the relevant section:
>
> Public cnMinistry As ADODB.Connection
> Private Function ConnectToDatabase() As Boolean
> 'instantiate the connection & connect
> Set cnMinistry = New Connection
> cnMinistry.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;
> Data Source=" & strFileToOpen & ";"
> cnMinistry.Open '<--- error happens here
> If cnMinistry.State = adStateOpen Then ConnectToDatabase = True
> End Function
>
> References:
> Visual Basic for Applications
> Microsoft Excel 11.0 Object Library
> OLE Automation
> Microsoft Office 11.0 Object Library
> Microsoft Forms 2.0 Object Library
> Microsoft.ActiveX Data Objects 2.5 Library
>
> Some more code that may have to change:
>
> Private Sub ImportAccessTable(cn As ADODB.Connection, TableName As
> String, TargetRange As Range)
> Dim rs As ADODB.Recordset, intColIndex As Integer
> Set TargetRange = TargetRange.Cells(1, 1)
> Set rs = New ADODB.Recordset
> rs.Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable
> For intColIndex = 0 To rs.Fields.Count - 1 ' the field names
> TargetRange.Offset(0, intColIndex).Value =
> rs.Fields(intColIndex).Name
> Next
> TargetRange.Offset(1, 0).CopyFromRecordset rs ' the recordset data
> rs.Close
> Set rs = Nothing
> End Sub
>
>

 
Reply With Quote
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      21st Oct 2007
try using

Provider=Microsoft.Jet.OLEDB.12.0

You might read through these article:

http://support.microsoft.com/kb/247412
http://support.microsoft.com/kb/246335

Also check the name of your access file extension. MDB might be something
like ACDB

--
Regards,
Tom Ogilvy
--
Regards,
Tom Ogilvy


"Dave B" wrote:

> In 2005, thanks to code posted on this board, I created a workbook
> which connects to Access & retrieves data from recordsets. The code
> works fine, except now when I try to use it with Access 2007 I get the
> error:
> "Unrecognized database format 'C:\...\myfile.accdb'
>
> Is there a way to update the code so it will work with Access 2007?
> Thanks in advance for any help! Here is the relevant section:
>
> Public cnMinistry As ADODB.Connection
> Private Function ConnectToDatabase() As Boolean
> 'instantiate the connection & connect
> Set cnMinistry = New Connection
> cnMinistry.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;
> Data Source=" & strFileToOpen & ";"
> cnMinistry.Open '<--- error happens here
> If cnMinistry.State = adStateOpen Then ConnectToDatabase = True
> End Function
>
> References:
> Visual Basic for Applications
> Microsoft Excel 11.0 Object Library
> OLE Automation
> Microsoft Office 11.0 Object Library
> Microsoft Forms 2.0 Object Library
> Microsoft.ActiveX Data Objects 2.5 Library
>
> Some more code that may have to change:
>
> Private Sub ImportAccessTable(cn As ADODB.Connection, TableName As
> String, TargetRange As Range)
> Dim rs As ADODB.Recordset, intColIndex As Integer
> Set TargetRange = TargetRange.Cells(1, 1)
> Set rs = New ADODB.Recordset
> rs.Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable
> For intColIndex = 0 To rs.Fields.Count - 1 ' the field names
> TargetRange.Offset(0, intColIndex).Value =
> rs.Fields(intColIndex).Name
> Next
> TargetRange.Offset(1, 0).CopyFromRecordset rs ' the recordset data
> rs.Close
> Set rs = Nothing
> End Sub
>
>

 
Reply With Quote
 
=?Utf-8?B?dXJrZWM=?=
Guest
Posts: n/a
 
      21st Oct 2007
"Tom Ogilvy" wrote:

> try using
>
> Provider=Microsoft.Jet.OLEDB.12.0


> Also check the name of your access file extension. MDB might be something
> like ACDB
>


This connection string worked for me:

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myDatabase.accdb;"

Hope this helps.

--
urkec
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Access 2007 ADP project will not connect to SQL Server 2008 Chris Woodard Microsoft Access ADP SQL Server 2 2nd Oct 2009 07:12 PM
How do I connect to access 2007 data from excel? Marilyn Myers Microsoft Excel Misc 3 12th Dec 2008 04:15 PM
Using Access 2007 to connect to SQL Ted Microsoft Access ADP SQL Server 2 13th Nov 2008 06:32 PM
Access 2007 can't connect to SQL Server Klerman Microsoft Access ADP SQL Server 1 19th Jul 2008 04:19 PM
How do I get Access 2007 Upsizing Wizard to connect to SQLserver? =?Utf-8?B?TWlrZSBCLg==?= Microsoft Access External Data 2 1st Aug 2007 08:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:18 AM.