Hi Dirk, Your are correct, I was trying to keep the code simple. The
variable is a collection and not a container.
Here's the actual code that I have in my program:
Public Function read_DBTable(ByVal tblName As String, _
ByRef PKeys As Collection, _
ByRef FKeys As Collection, _
ByRef RecipeNames As Collection, _
ByRef numOfRows As Integer) As errType
On Error GoTo ErrorHandler
Dim conn As ADODB.Connection
Dim rs As ADODB.recordSet
Dim tmpSQLstr As String
Dim strConn As String
Debug.Print ""
Debug.Print "clsDBManager[read_DBTable]: Entry Point "
read_DBTable = noError
' open the connection using the tmpSQLstr parameter
tmpSQLstr = "SELECT "
tmpSQLstr = tmpSQLstr + tblName & ".* "
tmpSQLstr = tmpSQLstr + " FROM "
tmpSQLstr = tmpSQLstr + tblName
tmpSQLstr = tmpSQLstr + " WHERE "
tmpSQLstr = tmpSQLstr + tblName
tmpSQLstr = tmpSQLstr + ".ForeignKey = 2 "
tmpSQLstr = tmpSQLstr + "ORDER BY "
tmpSQLstr = tmpSQLstr + tblName
tmpSQLstr = tmpSQLstr + ".Name;"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & CurrentProject.Path & _
"\" & parm.dbName & ";"
' open the connection us
Set conn = New ADODB.Connection
conn.Open strConn
' open database table
Set rs = New ADODB.recordSet
rs.Open tmpSQLstr, conn, adOpenKeyset, adLockOptimistic
Debug.Print " --- Opening database """ & parm.dbName & """ and table """
& tblName & """"
If rs.BOF And rs.EOF Then
MsgBox "clsDBManager[read_DBTable]: ERROR, did not find any records in
database table """ & tblName & """"
Debug.Print "clsDBManager[read_DBTable]: ERROR, did not find any records
in database table """ & tblName & """"
read_DBTable = errOccured
Exit Function
End If
numOfRows = 1
Do Until rs.EOF
' we don't want to store blank data in the collection; therefore, if we
' encounter a blank, we need to skip the record
If IsNull(rs.Fields.Item(2)) Then
read_DBTable = errOccured
Debug.Print "clsDBManager[read_DBTable]: Error in table, check table
values [" & _
rs.Fields.Item(0) & ":" & _
rs.Fields.Item(1) & ":" & _
rs.Fields.Item(2) & """]"
Else
PKeys.Add (rs.Fields.Item(0).value), CStr(numOfRows)
FKeys.Add (rs.Fields.Item(1).value), CStr(numOfRows)
RecipeNames.Add (rs.Fields.Item(2).value), CStr(numOfRows)
Debug.Print " --- Saving table data[" & numOfRows; "]: """ & _
rs.Fields.Item(0).value & ":" & _
rs.Fields.Item(1).value & ":" & _
rs.Fields.Item(2).value & """"
numOfRows = numOfRows + 1
End If
' move to the next row in the table
rs.MoveNext
Loop
' Add EOF Marker to last record in collection
PKeys.Add 0, CStr(numOfRows)
FKeys.Add 0, CStr(numOfRows)
RecipeNames.Add "EOF", CStr(numOfRows)
' set the number of rows to include all the table data except the "EOF" record
' the "EOF" marker is there for safty reasons
numOfRows = numOfRows - 1
' clean up
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
Debug.Print "clsDBManager[read_DBTable]: Exit Point "
Debug.Print ""
Exit Function
ErrorHandler:
read_DBTable = errOccured
' clean up
If Not rs Is Nothing Then
If rs.State = adStateOpen Then rs.Close
End If
Set rs = Nothing
If Not conn Is Nothing Then
If conn.State = adStateOpen Then conn.Close
End If
Set conn = Nothing
If Err <> 0 Then
MsgBox "clsDBManager[read_DBTable]: Error, in trying to open database"
Debug.Print "clsDBManager[read_DBTable]: Error, in trying to open
database"
' MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
End Function
Dirk Goldgar said:
Eddie's Bakery and Cafe' said:
Hi, Dirk, This is the subroutine where I seem to be spending all my
time. I also get a run-time error when I switch between "Design
View" and "Form View", the error is:
Run-Time Error .. "The database has been placed in a state by user
'Admin' on machine . that prevents if from being opened or
locked."
I am opening a table and putting its data into collection objects
that are passed back to the caller. In addition to the "Err.Source"
error, the db open error is occurring at line number 26 (conn.Open
strConn). Thanks for all your help
1) Public Sub Read_DBTable (ByVal tblName as String, _
2) ByVal dbName as String, _
3) ByRef myContainer as Container)
4)
4.1) On Error GoTo ErrorHandler
4.2)
5) Dim conn As ADODB.Connection
6) Dim rs As ADODB.recordSet
7) Dim tmpSQLstr As String
8) Dim strConn As String
9) Dim NumOfRows as Integer
10)
11) tmpSQLstr = "SELECT "
12) tmpSQLstr = tmpSQLstr + tblName & ".* "
13) tmpSQLstr = tmpSQLstr + " FROM "
14) tmpSQLstr = tmpSQLstr + tblName
15) tmpSQLstr = tmpSQLstr + " WHERE "
16) tmpSQLstr = tmpSQLstr + tblName
17) tmpSQLstr = tmpSQLstr + ".ForeignKey = 2 "
18) tmpSQLstr = tmpSQLstr + "ORDER BY "
19) tmpSQLstr = tmpSQLstr + tblName
20) tmpSQLstr = tmpSQLstr + ".Name;"
21)
22) strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
23) "Data Source=" & CurrentProject.Path & "\" & dbName &
";" 24)
25) Set conn = New ADODB.Connection
26) conn.Open strConn
27)
28) Set rs = New ADODB.recordSet
29) rs.Open tmpSQLstr, conn, adOpenKeyset, adLockOptimistic
30)
31) If rs.BOF And rs.EOF Then
32) Exit Sub ' No Records were found
33) End If
34)
35) NumOfRows = 1
36)
37) Do Until rs.EOF
38) myContainer.Add (rs.Fields.Item(0).value), CStr(numOfRows)
39) NumOfRows = numOfRows + 1
40) rs.MoveNext
41) Loop
42)
43) rs.Close
44) conn.Close
45)
46) Set rs = Nothing
47) Set conn = Nothing
48) Exit Sub
49)
50) ErrorHandler:
51 )
52 ) If Not rs Is Nothing Then
54) If rs.State = adStateOpen Then rs.Close
55) End If
56)
57) Set rs = Nothing
58)
59) If Not conn Is Nothing Then
60) If conn.State = adStateOpen Then conn.Close
61) End If
62)
63) Set conn = Nothing
64)
65) If Err <> 0 Then
66) MsgBox "clsDBManager[read_DBTable]: Error, in trying to open
database"
67) Debug.Print "clsDBManager[read_DBTable]: Error, in trying to
open database"
68) MsgBox Err.Source & "-->" & Err.Description, , "Error"
69) End If
70) End Function
That code has some oddities that give me compile errors before even
getting to the problem you're posting about. You can't have copied and
pasted it; what did you change in transcription? Line 32 gives me an
error because of the ` character you've used instead of a ' character,
but more importantly, you speak in your post of a collection object, but
you Dim myContainer as Container. The only Container object I know
about is the DAO Container object, and that has no Add method and
wouldn't be what you wanted to use.
If I change "Container" to "Collection", and fix that ` character, the
code compiles for me without error. So I think you'd better copy and
paste your actual code if I'm to have any chance at debugging it.
PS: Dirk, if you don't already known, I am changing careers to become
a baker. Since you have been such a tremendous help, I would like to
send you some goodies from the bakery where I am training. If you
give me you mailing address I will be glad to send you some goodies.
That's very thoughtful of you, and I'll take you up on the offer --
after we've solved this problem of yours.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)