Error message 3847 rewrite code from DAO to ADO

kate10123

Registered User.
Local time
Today, 04:37
Joined
Jul 31, 2008
Messages
185
Hi guys,

I am trying to pull data from a database to another database through an oracle view.

How can I change the code I am using for the transfer process to work with ADO?.

The reason for this is because we have moved from access 2003 to 2007!

Option Compare Database
Option Explicit
Public Const db_name As String = "TEST"
Public Const jup As String = "TEST"
Public Const pwd As String = "TEST"
Public Const uid As String = "TEST"
Public Const dbq As String = "TEST"
Public Const dsn_main As String = "{Oracle in DEFAULT_HOME}"
Public Const dsn_alt As String = "(Oracle in ORACLE_HOME)"
Dim dsn_use As String

Private Function Get_Local_Number(tbl As String) As Long
'Establish number of records in tbl

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(tbl, dbOpenTable)
Get_Local_Number = rst.RecordCount
rst.close
dbs.close
Set rst = Nothing
Set dbs = Nothing
Get_Local_Number_Exit:
Exit Function
Get_Local_Number_Err:
Select Case Err.Number
Case Else
DoCmd.SetWarnings True
MsgBox Err.Number & Chr(13) & Chr(10) & Err.Description
End Select
Resume Get_Local_Number_Exit

End Function

Private Sub Get_ODBC_Number(tbl As String, _
drv As Integer, _
num As Long, _
con As String)
On Error GoTo Get_ODBC_Number_Err
'Establish number of records in Oracle tbl via ODBC.

Dim wrkODBC As Workspace
Dim conODBC As Connection
Dim rstODBC As Recordset
dsn_use = dsn_main
Set wrkODBC = CreateWorkspace("ODBCWorkspace", _
uid, _
pwd, _
dbUseODBC)

Set conODBC = wrkODBC.OpenConnection("Connection1", drv, , _
"ODBC;DRIVER=" & dsn_use & ";DBQ=" & dbq & ";UID=" & uid & _
";PWD=" & pwd & ";DSN=" & dsn_use & ";")
Set rstODBC = conODBC.OpenRecordset(tbl, dbOpenDynamic)
con = conODBC.Connect
num = rstODBC.RecordCount
rstODBC.close
wrkODBC.Connections(0).close
wrkODBC.close
Set rstODBC = Nothing
Set conODBC = Nothing
Set wrkODBC = Nothing
Get_ODBC_Number_Exit:
Exit Sub
Get_ODBC_Number_Err:
Select Case Err.Number
Case 3146
num = -1
If dsn_use = dsn_main Then
dsn_use = dsn_alt
End If
Resume 0
DoCmd.SetWarnings True
Case Else
num = -1
DoCmd.SetWarnings True
MsgBox Err.Number & Chr(13) & Chr(10) & Err.Description, , db_name
End Select

Resume Get_ODBC_Number_Exit
End Sub
Sub Download(qryName1 As String, qryName2 As String, sysName As String)
On Error GoTo Download_Err
Dim odbc_num As Long
Dim odbc_con As String

DoCmd.SetWarnings False
Get_ODBC_Number "UOP_ASK_DATA_MVW", dbDriverNoPrompt, odbc_num, odbc_con
If odbc_num < 0 Then
Err.Raise vbObjectError + 513
End If
SysCmd acSysCmdSetStatus, "Searching for " & sysName & " data"
RunQueryDown qryName1, qryName2, odbc_con
Download_Exit:
DoCmd.SetWarnings True
Exit Sub
Download_Err:
Select Case Err.Number
Case 3146
If InStr(1, Error(Err.Number), "ORA-00942") > 0 Then
Resume Next
ElseIf InStr(1, Error(Err.Number), "ORA-00955") > 0 Then
Resume Next
End If
Case vbObjectError + 513
MsgBox "Invalid connection details. Download aborted.", _
vbOKOnly, _
db_name
Resume Download_Exit
Case Else
MsgBox Error$ & Chr(13) & Chr(10) & Err.Number, , db_name
Resume Download_Exit
End Select
End Sub

Private Sub RunQueryDown(qryName1 As String, _
qryName2 As String, _
qryCon As String)
Dim dbs As DAO.Database
Set dbs = CurrentDb
With dbs
.QueryDefs(qryName1).Connect = qryCon
.QueryDefs.Refresh
.QueryDefs(qryName1).close
DoCmd.OpenQuery qryName2, acViewNormal
.QueryDefs(qryName1).Connect = "ODBC;"
.QueryDefs.Refresh
.QueryDefs(qryName1).close
.close
End With
Set dbs = Nothing
End Sub
 
The reason for this is because we have moved from access 2003 to 2007!
That doesn't make sense. Access 2007's DEFAULT is DAO so why do you need to move to ADO?
 
Thanks for replying.

This code works fine in 2003 but the minute I try to run it in 2007 it brings up that particular error message.
 
What does it highlight and what is the exact message?
 
Attached is the database, for obvious reasons I have put test instead of the password and username so you can see the error message.

I open frm_JupiterSearch, enter a six digit number and hit 'SEARCH' this calls the connection and then this is when the error message appears, it is an ok type message so doesn't have the option to debug.
 

Attachments

I think you might have a corrupt file there. Try importing everything into a new, blank ACCDB file.
 
Just imported everything into a new database. Attached
 

Attachments

sorry that doesn't have the module in. Another attached.
 

Attachments

Just imported everything into a new database. Attached
Looks like you are missing a function or dll:

Jupiter_Transfer

is not there. Should there be a reference to this? What references do you have set in 2003 and do they match in 2007?
 
I see now - it is ODBC Direct and apparently you can't use that in 2007 (from the Google posts). So you are correct in your first assumption in that you need to go to ADO. I will try to help rewrite it but it may take a while as I just got slammed at work. If I don't get to it right away, perhaps someone else will.
 
Thanks for replying and taking the time to look at this for me. Still no luck my side, tried to look at the reference list but this didn't make a difference.
 
Indeed - ODBC direct is no longer supported by DAO with the ACCDB engine.
Even MS recommend using ADO for such work now (and they've not been making much ADO noise of late otherwise).

Ultimately you're just looking to replace the connection/recordset section of your code:
What is the target source - a SQL Server? Some other target?
 
I am trying to connect to an oracle view which interfaces with an oracle database.
 
Ah... If I'd looked a bit harder I'd have seen the slight clue of a constant
Public Const dsn_main As String = "{Oracle in DEFAULT_HOME}"
defined in your question. :-s
(Sorry - pushed for time at the moment).

Substantially removed from my element with this being an Oracle database, however the principles of building a connection string for a passthrough remain and should work. Perhaps you just need to play with the provider.
Connection Strings:
http://www.connectionstrings.com/?carrier=oracle
http://www.carlprothman.net/Default.aspx?tabid=90#ODBCDriverForOracleFromMicrosoft

As for the general concept of using ADO - it's well documented here and elsewhere.
The ADO OM is pretty narrow - but your equivalent section might be like...

Code:
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
    
cnn.Open "Provider=msdaora;Data Source=" & dbq & ";User Id=" & uid  & ";Password=" & pwd
With rst
    .Open "SELECT COUNT(*) FROM " & tbl, cnn, adOpenKeyset, adLockOptimistic
    num = .Fields(0)
    .Close
End With
cnn.Close
Set rst = Nothing
Set cnn = Nothing

Pure aircode. No Oracle server upon which to test it. Read the links though for more information.

Cheers.
 
You will also need to change your open method from what is shown in order for your record counts to work properly.

Code:
with RST
        .Open "SELECT COUNT(*) FROM " & tbl, cnn, adOpenKeyset, adLockOptimistic
    num = .Fields(0)
    .Close
End With

would need to be replaced with

Code:
with RST
     .Open "SELECT COUNT(*) FROM " & tbl, cnn, adOpenKeyset, adLockStatic
    num = .Fields(0)
    .Close
End With

Sorry, I too have no experience with Oracle.
 
>> .Open "SELECT COUNT(*) FROM " & tbl, cnn, adOpenKeyset, adLockStatic

Ummm...
There isn't even such a constant adLockStatic.
What were you aiming for the cursor type?
.Open "SELECT COUNT(*) FROM " & tbl, cnn, adOpenStatic
Still, there's no need for that.
The code I suggested should work just fine. (Unless Oracle behaves in some way I'm unfamiliar with).

I copied the code from a module lying around and edited it quickly. Though I spotted the only purpose being to aquire a recordcount and hence changed to an aggregate SQL statement at the very last moment before posting, I might have chosen the lock type adLockReadOnly (or omitted the parameter altogether - effectively the same thing as it's the default) and a Forward Only cursor type for a very tiny performance gain.

None the less... there's nothing failing about the code posted. :-s
 

Users who are viewing this thread

Back
Top Bottom