Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 MS Access
 ado error

Author  Topic 

KnooKie
Aged Yak Warrior

623 Posts

Posted - 2005-06-02 : 08:44:01
Office 2002...using Access

i have the following code...


Public Function Ranks()
On Error GoTo ErrorHandler:

Const intMaxCols As Integer = 5 'number of spreadsheet columns required

Dim intBrokerMax As Integer, intCategoryMax As Integer
Dim strSQL As String
Dim intLoop As Integer
Dim cn As New ADODB.Connection
Dim rstCat As ADODB.Recordset 'category list
Dim rstBroker As ADODB.Recordset 'broker list

'cn.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;Data Source=H:\Development\Trading\Trading_AAR2005\764\764.mdb"
cn.ConnectionString = CurrentProject.Connection
cn.Open

'get broker list
strSQL = "SELECT fl_brokerCode, fs_brokerNameinUID FROM qryBrokerList"
Set rstBroker = cn.Execute(strSQL)

intBrokerMax = rstBroker.RecordCount 'total num of brokers to loop through

Do Until rstBroker.EOF
Debug.Print rstBroker!fl_brokercode
Debug.Print rstBroker!fs_brokerNameinUID
Loop

cn.Close

Exit Function
ErrorHandler:
MsgBox "Error in Ranks creation function " & Err.Number & " " & Err.Description
Debug.Print "Error in Ranks creation function " & Err.Number & " " & Err.Description
End Function



i get the following error on the cn.Open line....

Error in Ranks creation function -2147467259 The database has been placed in a state by user 'Admin' on machine '04637-DHP' that prevents it from being opened or locked.

the code is running in the database it is 'opening' if that means anything ?

no one else has the database open. It has linked tables in it from another Access database.

does anyone know why this is please ?

thanks
paul


====
Paul

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-06-02 : 09:11:23
No need to re-open the connection; it's already open. Just say:

dim cn as ADODB.Connection
set cn = CurrentProject.Connection

and then you can start using cn right away w/o issuing the Open() method.

- Jeff
Go to Top of Page

KnooKie
Aged Yak Warrior

623 Posts

Posted - 2005-06-02 : 11:41:41
Thanks Jeff, that solved my connection problem.

however i want to take a recordcount and if i leave out the .MoveLast .MoveFirst lines (which i though were just a DAO glitch thing) i get a recordcount of -1. If i include the .Move lines then i get the following message...

Error in Ranks creation function -2147217884 Rowset does not support fetching backward.

on the .MoveLast line

annoyingly i am obviously using the dodgy version of MDAC i have read about whose help files do not work.

how do i move through recordsets and get the recordCount ?

thanks
Paul


Public Function Ranks()
On Error GoTo ErrorHandler:

Const intMaxCols As Integer = 5 'number of spreadsheet columns required

Dim intBrokerMax As Integer, intCategoryMax As Integer
Dim strSQL As String
Dim intLoop As Integer
Dim cn As ADODB.Connection
Dim rstCat As ADODB.Recordset 'category list
Dim rstBroker As ADODB.Recordset 'broker list

Set cn = CurrentProject.Connection

'get broker list
strSQL = "SELECT fl_brokerCode, fs_brokerNameinUID FROM qryBrokerList"
Set rstBroker = cn.Execute(strSQL)
rstBroker.MoveLast
intBrokerMax = rstBroker.RecordCount 'total num of brokers to loop through
rstBroker.MoveFirst

Do Until rstBroker.EOF
Debug.Print rstBroker!fl_brokercode
Debug.Print rstBroker!fs_brokerNameinUID
Loop

cn.Close

Exit Function
ErrorHandler:
MsgBox "Error in Ranks creation function " & Err.Number & " " & Err.Description
Debug.Print "Error in Ranks creation function " & Err.Number & " " & Err.Description
End Function



====
Paul
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-06-02 : 11:50:22
I wouldn't -- I would make a separate call and do a SELECT COUNT(*). Or even better, don't try to calculate the count in the beginning -- loop through the RS and increment your count variable one by one as you go:


Set rstBroker = cn.Execute(strSQL)

intBrokerMax = 0

Do Until rstBroker.EOF
Debug.Print rstBroker!fl_brokercode
Debug.Print rstBroker!fs_brokerNameinUID
intBrokerMax = intBrokerMax + 1
Loop

' now intBrokerMax has the count of the rows in the RS.



Also, remember that you need to specify the proper options for an ADO recordset to ensure you can move forward or back. The default may be forward-only, I don't recall. Check the ADO documentation.

By the way -- loose the "int" prefix on your variable. Very "uncool" unless you are working in VBScript w/o variable declarations.

- Jeff
Go to Top of Page

KnooKie
Aged Yak Warrior

623 Posts

Posted - 2005-06-02 : 12:46:19
yes you were right ADO is forwardonly by default and i've moved up the cool-o-meter slightly by removing the int off variables. I kinda like the strSQL one though.

Everything working nicely now. Haven't used ADO in so long it's all coming back to me now.

Thanks for help.

Public Function Ranks()
On Error GoTo ErrorHandler:

Const MaxCols As Integer = 5 'number of spreadsheet columns required

Dim BrokerMax As Integer, CategoryMax As Integer
Dim strSQL As String
Dim cn As ADODB.Connection
Dim rstCat As New ADODB.Recordset 'category list
Dim rstBroker As New ADODB.Recordset 'broker list

Set cn = CurrentProject.Connection

'get broker list
strSQL = "SELECT fl_brokerCode, fs_brokerNameinUID FROM qryBrokerList"
With rstBroker
.Open strSQL, cn, adOpenStatic, adLockReadOnly
End With

BrokerMax = 0

Do Until rstBroker.EOF
Debug.Print rstBroker!fl_brokercode
Debug.Print rstBroker!fs_brokerNameinUID

'write to array here

BrokerMax = BrokerMax + 1
rstBroker.MoveNext
Loop

Debug.Print BrokerMax

cn.Close

Exit Function
ErrorHandler:
MsgBox "Error in Ranks creation function " & Err.Number & " " & Err.Description
Debug.Print "Error in Ranks creation function " & Err.Number & " " & Err.Description
End Function



====
Paul
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-06-02 : 13:07:26
You may want to clean your resources in your Exit Function Handler:

Set cn = Nothing
Set rstBroker = Nothing




Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

KnooKie
Aged Yak Warrior

623 Posts

Posted - 2005-06-02 : 13:10:35
yes good idea will do - thanks


====
Paul
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-06-02 : 13:25:13
quote:
Originally posted by jhermiz

You may want to clean your resources in your Exit Function Handler:

Set cn = Nothing
Set rstBroker = Nothing





This is a popular myth in VB, and a huge pet peeve of mine when I see it in code.

Unless cn and rstBroker are global or static variables, the above statement has no effect. As soon as the variables go out of scope (i.e., the function ends) the objects are no longer referenced and the memory will be recovered the same as if you explicitly assign those variables to Nothing.

The only cleanup I would add to the above is to explicitly close the rstBroker object.

I only browsed this so far, but Googling has produced what appears to be a decent article on this: http://www.keysound.com/html/ch_15_much_ado_about_nothing.htm

- Jeff
Go to Top of Page

KnooKie
Aged Yak Warrior

623 Posts

Posted - 2005-06-02 : 13:32:04
that's interesting didn't know that.

i'll have to go around impressing people before they all find out.


====
Paul
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-06-02 : 14:41:27
You'd be surprised the number of times a simple function using a connection / recordset object grows or changes based on the business. I've always ensured that whether local or not my variables are clean and are released from memory. Whether the function exits successfully or not, it is the extra assurance that your system doesn't hang.

Jeff not sure why it is a pet peeve if it is not causing any types of problems but only ensures that the memory is released.
I'll bet your bottom dollar that there are times that memory is not released even after the function exits.
Although the object was of the Excel object datatype without explicitly setting it to nothing that resource was hogging my PC's memory.

Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-06-02 : 14:53:57
>>Jeff not sure why it is a pet peeve if it is not causing any types of problems but only ensures that the memory is released.

That is the pet peeve -- people think the bold part is true and it is not. If you have well-structured code it is not necessary and has no effect. Just remember that setting object variables to Nothing does NOT explicitly destroy that object -- it simply decrements the reference count on that object by 1. (which is the same thing that happens when a variable goes out of scope)

- Jeff
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-06-02 : 15:09:58
quote:
Originally posted by jsmith8858

>>Jeff not sure why it is a pet peeve if it is not causing any types of problems but only ensures that the memory is released.

That is the pet peeve -- people think the bold part is true and it is not. If you have well-structured code it is not necessary and has no effect. Just remember that setting object variables to Nothing does NOT explicitly destroy that object -- it simply decrements the reference count on that object by 1. (which is the same thing that happens when a variable goes out of scope)

- Jeff



But if you are reusing that same variable in the same scope at a later time (which happens a lot, especially from the new comers to vb) then it is best to set the object to nothing if it is not being used.


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-06-02 : 15:21:07
>>But if you are reusing that same variable in the same scope at a later time (which happens a lot, especially from the new comers to vb) then it is best to set the object to nothing if it is not being used.

I am not sure what you are saying .... what do you mean by "reusing the same variable in the same scope at a later time" ?

- Jeff
Go to Top of Page

surfsurfer103
Starting Member

1 Post

Posted - 2006-05-02 : 18:26:02
Paul,
Error in Ranks creation function -2147467259 The database has been placed in a state by user 'Admin' on machine '04637-DHP' that prevents it from being opened or locked.

In my experience Access does this sometimes, its a sort of bug.

Its designed for some sort of security reason if more than one person is trying to update the database. Although in you case its a little puzzeling because I understood it applied only when updating some sort of structure of the database, not data. However I find if you are running a seperate access database(Programs) on some clients sharing some data on a server, this error dosnt occur if each database is opened in exclusive mode. Works most surely if "/excl" is added to the command icon when starting.

Cheers
Go to Top of Page
   

- Advertisement -