I’m somewhat ashamed to admit it, but my ADO skill are rather limited (and even that is an understatement). Can someone show me the correct syntax for opening a connection to another database? (That is, a different database than the CurrentProject.)
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
ADO code needed (Any)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » ADO code needed (Any)
- This topic has 10 replies, 3 voices, and was last updated 21 years, 11 months ago.
AuthorTopicWSMarkLiquorman
AskWoody LoungerJune 26, 2003 at 11:42 pm #389685Viewing 0 reply threadsAuthorReplies-
WSMarkD
AskWoody LoungerJune 27, 2003 at 12:59 am #689294This is a simple example of opening a connection to Northwind.mdb from another database:
Public Sub adoTestConnection()
On Error GoTo Err_HandlerDim rst As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim strAppPath As String
Dim strCnn As String
Dim strSQL As String
Dim strMsg As String‘ If db is secured use this syntax:
‘ strCnn = “Provider=Microsoft.Jet.OLEDB.4.0;” & _
‘ “User ID=MyUserName;” & _
‘ “Password=MyPwd;” & _
‘ “Data Source=” & strAppPath & “MyApp.mdb;” & _
‘ “Jet OLEDB:System database=” & strAppPath & “MyApp.mdw;”‘ If db is not secured:
strCnn = “Provider=Microsoft.Jet.OLEDB.4.0;” & _
“Data Source=C:Program FilesMicrosoft OfficeOffice10SamplesNorthwind.mdb;”strSQL = “SELECT * FROM ORDERS;”
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordsetcnn.ConnectionString = strCnn
cnn.Open
rst.Open strSQL, cnn, adOpenStatic, adLockOptimistic
‘ Test:
strMsg = “There are ” & rst.RecordCount & ” records in the Orders table.”
MsgBox strMsg, vbInformation, “RECORD COUNT”rst.Close
cnn.CloseExit_Sub:
Set cnn = Nothing
Set rst = Nothing
Exit Sub
Err_Handler:
Select Case Err.Number
Case 0
Resume Exit_Sub
Case Else
strMsg = “Error No ” & Err.Number & “: ” & Err.Description
MsgBox strMsg, vbExclamation, “ADO CONNECTION ERROR”
Resume Exit_Sub
End SelectEnd Sub
Note if other db is secured you will need to provide valid username & password & specify path to workgroup file to open a connection, as shown in commented out block. This example opens read-only recordset. You’ll need to modify the rst.Open statement depending on what type of recordset you need – the ADO Help files may provide some guidance on correct arguments to use. For example, for an updatable recordset you’d use adOpenDynamic in place of adOpenStatic for CursorType argument. If updating records much of the syntax is similar (or identical to) DAO equivalent (except, as I usually forget, ADO has no “Edit” method….)
HTH
-
WSMarkLiquorman
AskWoody LoungerJune 27, 2003 at 1:20 am #689302 -
WSMarkLiquorman
AskWoody LoungerJune 27, 2003 at 1:44 am #689308I seem to be having some problems. Here is the function I got from MSKB. It works fine for a table within the same database. But from my Frontend database, I need to manipulate the autonumbers in tables in the Backend. So right now I’m hung up on that “Set cnn = CurrentProject.Connection” line of code.
Function ChangeSeed(strTbl As String, strCol As String, lngSeed As Long) As Boolean
‘You must pass the following variables to this function.
‘strTbl = Table containing autonumber field
‘strCol = Name of the autonumber field
‘lngSeed = Long integer value you want to use for next AutoNumber.Dim cnn As ADODB.Connection
Dim cat As New ADOX.Catalog
Dim col As ADOX.Column‘Set connection and catalog to current database.
Set cnn = CurrentProject.Connection
cat.ActiveConnection = cnnSet col = cat.Tables(strTbl).Columns(strCol)
col.Properties(“Seed”) = lngSeed
cat.Tables(strTbl).Columns.Refresh
If col.Properties(“seed”) = lngSeed Then
ChangeSeed = True
Else
ChangeSeed = False
End If
Set col = Nothing
Set cat = Nothing
Set cnn = NothingEnd Function
-
WScharlotte
AskWoody LoungerJune 27, 2003 at 2:14 am #689321Mark,
If the table is in the backend database, you need to make a connection to manipulate the table’s design there. So you can’t use the CurrentProject.Connection approach, you have to build the full connection. There’s an example in on-line help of using the Columns Collection in ADOX that ;might be helpful to you. And what kind of table is the backend, because “seed” isn’t a property exposed by the Jet OLE DB provider?
-
WSMarkLiquorman
AskWoody Lounger
-
-
WSMarkD
AskWoody LoungerJune 27, 2003 at 2:57 am #689328I usually only use ADOX with Current Project. Here is an example of opening connection to other than CurrentProject for data definition purposes:
Public Sub adoCreateNewTable()
On Error GoTo Err_HandlerDim cnn As ADODB.Connection
Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table
Dim strCnn As String
Dim strMsg As StringSet cnn = New ADODB.Connection
Set cat = New ADOX.Catalog
Set tbl = New ADOX.TablestrCnn = “Provider=Microsoft.Jet.OLEDB.4.0;” & _
“Data Source=C:Program FilesMicrosoft OfficeOffice10SamplesNorthwind.mdb;”cnn.ConnectionString = strCnn
cnn.Open strCnn
cat.ActiveConnection = cnntbl.Name = “Table1”
tbl.Columns.Append “PK_Field”, adInteger
tbl.Keys.Append “PrimaryKey”, adKeyPrimary, “PK_Field”
cat.Tables.Append tbl
cnn.CloseMsgBox “New table Table1 created in Northwind.mdb.”, vbInformation, “TEST MSG”
Exit_Sub:
Set cnn = Nothing
Set cat = Nothing
Set tbl = Nothing
Exit Sub
Err_Handler:
Select Case Err.Number
Case 0
Resume Exit_Sub
Case Else
strMsg = “Error No ” & Err.Number & “: ” & Err.Description
MsgBox strMsg, vbExclamation, “ADOX NEW TABLE ERROR”
Resume Exit_Sub
End SelectEnd Sub
Note the line highlighted in bold – when opening connection be sure to include connection string or this will not work! This example successfully created new table in Northwind.mdb. I’m afraid I don’t know if ADOX will let you reset “seed” for an AutoIncrement field.
HTH
-
WSMarkLiquorman
AskWoody Lounger -
WSMarkD
AskWoody LoungerJune 27, 2003 at 3:03 pm #689660Taking another look at this issue I had no problem setting connection to another .MDB (linked back table db, Northwind.mdb), tho’ resetting the AutoIncrement field proved to be tricky. Here is example of sub using ADO connection to reset an AutoIncrement (aka AutoNumber aka Identity aka Counter) field. Instead of using flaky ADOX to reset field attributes, used DDL SQL which is simpler & more reliable; use the ADO Connection Execute method to run the SQL statement:
Public Sub adoResetAutoIncrementField(ByRef strDbName As String, _
ByRef strTbl As String, _
ByRef strFld As String, _
ByRef lngSeedVal As Long, _
ByRef lngIncrementVal As Long)
On Error GoTo Err_HandlerDim cnn As ADODB.Connection
Dim strCnn As String
Dim strSQL As String
Dim strMsg As String‘ strDbName = “C:Program FilesMicrosoft OfficeOffice10SamplesNorthwind.mdb”
Set cnn = New ADODB.Connection
strCnn = “Provider=Microsoft.Jet.OLEDB.4.0;” & _
“Data Source=” & strDbName & “;”
cnn.ConnectionString = strCnn
cnn.Open strCnn‘ DDL syntax: “ALTER TABLE [Table1] ALTER COLUMN [RecordID] COUNTER (1000,4);”
strSQL = “ALTER TABLE [” & strTbl & “] ALTER COLUMN [” & strFld & “] ” & _
“COUNTER (” & lngSeedVal & “,” & lngIncrementVal & “);”cnn.Execute strSQL, , adCmdText Or adExecuteNoRecords
cnn.Close‘ Possible errors:
‘ strTbl = “Orders”, strFld = “OrderID”
‘ This generated error due to OrderID being used in relationships:
‘ Error No -2147467259: Cannot change field ‘OrderID’. _
It is part of one or more relationships. _
Also will get error if fld is defined as PK for table: _
Error No -2147467259: Invalid field data type.strMsg = “AutoNumber field ” & strFld & ” seed value has been reset to ” & _
lngSeedVal & ” in ” & strTbl & ” table. ” & _
“Increment Value reset to ” & lngIncrementVal & “.”
MsgBox strMsg, vbInformation, “TEST MSG”Exit_Sub:
Set cnn = Nothing
Exit Sub
Err_Handler:
Select Case Err.Number
Case 0
Resume Exit_Sub
Case Else
strMsg = “Error No ” & Err.Number & “: ” & Err.Description
Debug.Print strMsg
MsgBox strMsg, vbExclamation, “RESET AUTOINCREMENT FIELD ERROR”
Resume Exit_Sub
End SelectEnd Sub
Note that if the AutoNumber field was defined as a Primary Key, or involved in a relationship, was not able to alter field; got one of the error msg’s described in commented out block above. To be able to reset AutoNumber field defined as PK, it was necessary to first remove the PK index, then reset using DDL SQL Constraint clause like in this example:
ALTER TABLE Table1 ALTER COLUMN RecordID COUNTER (2000,5) CONSTRAINT PrimaryKey PRIMARY KEY;
If the AutoNumber field is also involved in relationships, it might be a bit convoluted to reset; you’d probably have to delete the relationships then re-create – not fun. I don’t know why you’d get an “Installable ISAM” error when trying to open ADO connection – that makes no sense!! The above code worked correctly on my system which is using AXP with Access 2000 file format for both FE & BE db’s.
HTH
-
WSMarkLiquorman
AskWoody Lounger -
WSMarkD
AskWoody LoungerJune 27, 2003 at 3:30 pm #689666PS – if ADO connection doesn’t work you can always resort to DAO as last resort, using Database object Execute method:
Public Sub daoResetAutoIncrementField(ByRef strDbName As String, _
ByRef strTbl As String, _
ByRef strFld As String, _
ByRef lngSeedVal As Long, _
ByRef lngIncrementVal As Long)
On Error GoTo Err_HandlerDim ws As DAO.Workspace
Dim db As DAO.Database
Dim strSQL As String
Dim strMsg As StringSet ws = DBEngine.Workspaces(0)
Set db = ws.OpenDatabase(strDbName)strSQL = “ALTER TABLE [” & strTbl & “] ALTER COLUMN [” & strFld & “] ” & _
“COUNTER (” & lngSeedVal & “,” & lngIncrementVal & “);”db.Execute strSQL
db.CloseSet ws = Nothing
Set db = NothingExample of use:
daoResetAutoIncrementField “C:Program FilesMicrosoft OfficeOffice10SamplesNorthwind.mdb”,”Table2″,”RecordID”,5000,5
This successfully reset AutoNumber field for Table2 (a linked table in current db) as shown in attached screen shot. Using DAO db Execute method seems to work same as ADO Connection Execute for executing a DDL SQL statement (you’ll get the same errors as well).
HTH
-
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Windows hosting exposes additional bugs
by
Susan Bradley
2 hours, 55 minutes ago -
No more rounded corners??
by
CWBillow
5 hours, 20 minutes ago -
Android 15 and IPV6
by
Win7and10
4 hours, 1 minute ago -
KB5058405 might fail to install with recovery error 0xc0000098 in ACPI.sys
by
Susan Bradley
15 hours, 16 minutes ago -
T-Mobile’s T-Life App has a “Screen Recording Tool” Turned on
by
Alex5723
17 hours, 58 minutes ago -
Windows 11 Insider Preview Build 26100.4202 (24H2) released to Release Preview
by
joep517
12 hours, 33 minutes ago -
Windows Update orchestration platform to update all software
by
Alex5723
1 day, 1 hour ago -
May preview updates
by
Susan Bradley
12 hours, 41 minutes ago -
Microsoft releases KB5061977 Windows 11 24H2, Server 2025 emergency out of band
by
Alex5723
4 hours, 16 minutes ago -
Just got this pop-up page while browsing
by
Alex5723
17 hours, 29 minutes ago -
KB5058379 / KB 5061768 Failures
by
crown
14 hours, 33 minutes ago -
Windows 10 23H2 Good to Update to ?
by
jkitc
3 hours, 53 minutes ago -
At last – installation of 24H2
by
Botswana12
1 day, 16 hours ago -
MS-DEFCON 4: As good as it gets
by
Susan Bradley
3 hours, 51 minutes ago -
RyTuneX optimize Windows 10/11 tool
by
Alex5723
2 days, 5 hours ago -
Can I just update from Win11 22H2 to 23H2?
by
Dave Easley
3 hours, 27 minutes ago -
Limited account permission error related to Windows Update
by
gtd12345
2 days, 18 hours ago -
Another test post
by
gtd12345
2 days, 18 hours ago -
Connect to someone else computer
by
wadeer
2 days, 13 hours ago -
Limit on User names?
by
CWBillow
2 days, 16 hours ago -
Choose the right apps for traveling
by
Peter Deegan
2 days, 6 hours ago -
BitLocker rears its head
by
Susan Bradley
1 day, 14 hours ago -
Who are you? (2025 edition)
by
Will Fastie
1 day, 12 hours ago -
AskWoody at the computer museum, round two
by
Will Fastie
2 days, 8 hours ago -
A smarter, simpler Firefox address bar
by
Alex5723
3 days, 4 hours ago -
Woody
by
Scott
3 days, 14 hours ago -
24H2 has suppressed my favoured spider
by
Davidhs
1 day, 13 hours ago -
GeForce RTX 5060 in certain motherboards could experience blank screens
by
Alex5723
4 days, 4 hours ago -
MS Office 365 Home on MAC
by
MickIver
3 days, 22 hours ago -
Google’s Veo3 video generator. Before you ask: yes, everything is AI here
by
Alex5723
4 days, 18 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.