Hello,
I am new to programming VBA and was hoping someone could help me debug an error i received when trying to implement Ken Snells code for reporting users logged into the DB Lock file.
I added the reference to the Microsoft ActiveX 2.1 library and seem to be getting the
error 3265 Item not found in this collection when I hit the following line:
strNewDataSource = dbs.TableDefs(strDummyTableName).Connect
I have a feeling it is some fundamental that i'm missing but here is the code:
Public Function WhoIsInTheDatabaseLockFile() As String
' OUTPUTS A LIST OF USERS IN THE DATABASE:
' 1. COMPUTER NAME ("COMPUTER NAME")
' 2. LOGON NAME ("LOGIN_NAME")
' 3. WHETHER USER IS STILL CONNECTED TO THE DB (USER ID
' REMAINS IN .LDB FILE UNTIL LAST USER EXITS OR
' UNTIL THE SLOT IS CLAIMED BY ANOTHER USER)
' ("CONNECTED")
' 4. WHETHER USER'S CONNECTION TERMINATED UNDER NORMAL
' CIRCUMSTANCES ("SUSPECT_STATE")
' *** ADAPTED FROM MICROSOFT KNOWLEDGE BASE ARTICLE 285822
Dim cn As New ADODB.Connection
Dim dbs As DAO.Database
Dim xlngLoop As Long
Dim rs As New ADODB.Recordset
Dim strNewDataSource As String, strCNString As String, xTT As String
Dim strCurrConnectString As String, xstrUserArray As String, strPipeDelimiterChar As String
Const strDummyTableName As String = "tbl__DummyTable_KeepRecordsetOpen"
Const strDatabaseString As String = "DATABASE="
Const strDataSourceText As String = "Data Source="
On Error GoTo Err_Msg
xstrUserArray = ""
strCurrConnectString = CurrentProject.Connection
strCNString = Mid(strCurrConnectString, InStr(strCurrConnectString, _
strDataSourceText) + Len(strDataSourceText))
strCNString = Left(strCNString, InStr(strCNString, ";") - 1)
Set dbs = CurrentDb
strNewDataSource = dbs.TableDefs(strDummyTableName).Connect
strNewDataSource = Mid(strNewDataSource, InStr(strNewDataSource, _
strDatabaseString) + Len(strDatabaseString))
Debug.Print "File containing the data tables: " & strNewDataSource
cn.ConnectionString = Replace(strCurrConnectString, strCNString, _
strNewDataSource, 1, 1, vbTextCompare)
cn.Open
' The user roster is exposed as a provider-specific schema rowset
' in the Jet 4.0 OLE DB provider. You have to use a GUID to
' reference the schema, as provider-specific schemas are not
' listed in ADO's type library for schema rowsets
Set rs = cn.OpenSchema(adSchemaProviderSpecific, _
, "{947bb102-5d43-11d1-bdbf-00c04fb92675}")
'Output the list of all users in the designated database
Debug.Print rs.Fields(0).name, "", rs.Fields(1).name, _
"", rs.Fields(2).name, rs.Fields(3).name
While Not rs.EOF
Debug.Print rs.Fields(0), rs.Fields(1), rs.Fields(2), rs.Fields(3)
For xlngLoop = 0 To 3
xTT = Trim(Nz(rs.Fields(xlngLoop), ""))
If Len(xTT) > 1 Then
If Right(xTT, 1) = Chr(0) Then xTT = Left(xTT, Len(xTT) - 1)
End If
xstrUserArray = xstrUserArray & xTT & strPipeDelimiterChar
Next xlngLoop
rs.MoveNext
Wend
If Len(xstrUserArray) > 0 Then xstrUserArray = Left(xstrUserArray, _
Len(xstrUserArray) - 1)
WhoIsInTheDatabaseLockFile = xstrUserArray
Exit_Function:
On Error Resume Next
rs.close
Set rs = Nothing
cn.close
Set cn = Nothing
dbs.close
Set dbs = Nothing
Exit Function
Err_Msg:
Debug.Print "Error occurred. Error number " & Err.Number & ": " & Err.Description
Resume Exit_Function
End Function
I am new to programming VBA and was hoping someone could help me debug an error i received when trying to implement Ken Snells code for reporting users logged into the DB Lock file.
I added the reference to the Microsoft ActiveX 2.1 library and seem to be getting the
error 3265 Item not found in this collection when I hit the following line:
strNewDataSource = dbs.TableDefs(strDummyTableName).Connect
I have a feeling it is some fundamental that i'm missing but here is the code:
Public Function WhoIsInTheDatabaseLockFile() As String
' OUTPUTS A LIST OF USERS IN THE DATABASE:
' 1. COMPUTER NAME ("COMPUTER NAME")
' 2. LOGON NAME ("LOGIN_NAME")
' 3. WHETHER USER IS STILL CONNECTED TO THE DB (USER ID
' REMAINS IN .LDB FILE UNTIL LAST USER EXITS OR
' UNTIL THE SLOT IS CLAIMED BY ANOTHER USER)
' ("CONNECTED")
' 4. WHETHER USER'S CONNECTION TERMINATED UNDER NORMAL
' CIRCUMSTANCES ("SUSPECT_STATE")
' *** ADAPTED FROM MICROSOFT KNOWLEDGE BASE ARTICLE 285822
Dim cn As New ADODB.Connection
Dim dbs As DAO.Database
Dim xlngLoop As Long
Dim rs As New ADODB.Recordset
Dim strNewDataSource As String, strCNString As String, xTT As String
Dim strCurrConnectString As String, xstrUserArray As String, strPipeDelimiterChar As String
Const strDummyTableName As String = "tbl__DummyTable_KeepRecordsetOpen"
Const strDatabaseString As String = "DATABASE="
Const strDataSourceText As String = "Data Source="
On Error GoTo Err_Msg
xstrUserArray = ""
strCurrConnectString = CurrentProject.Connection
strCNString = Mid(strCurrConnectString, InStr(strCurrConnectString, _
strDataSourceText) + Len(strDataSourceText))
strCNString = Left(strCNString, InStr(strCNString, ";") - 1)
Set dbs = CurrentDb
strNewDataSource = dbs.TableDefs(strDummyTableName).Connect
strNewDataSource = Mid(strNewDataSource, InStr(strNewDataSource, _
strDatabaseString) + Len(strDatabaseString))
Debug.Print "File containing the data tables: " & strNewDataSource
cn.ConnectionString = Replace(strCurrConnectString, strCNString, _
strNewDataSource, 1, 1, vbTextCompare)
cn.Open
' The user roster is exposed as a provider-specific schema rowset
' in the Jet 4.0 OLE DB provider. You have to use a GUID to
' reference the schema, as provider-specific schemas are not
' listed in ADO's type library for schema rowsets
Set rs = cn.OpenSchema(adSchemaProviderSpecific, _
, "{947bb102-5d43-11d1-bdbf-00c04fb92675}")
'Output the list of all users in the designated database
Debug.Print rs.Fields(0).name, "", rs.Fields(1).name, _
"", rs.Fields(2).name, rs.Fields(3).name
While Not rs.EOF
Debug.Print rs.Fields(0), rs.Fields(1), rs.Fields(2), rs.Fields(3)
For xlngLoop = 0 To 3
xTT = Trim(Nz(rs.Fields(xlngLoop), ""))
If Len(xTT) > 1 Then
If Right(xTT, 1) = Chr(0) Then xTT = Left(xTT, Len(xTT) - 1)
End If
xstrUserArray = xstrUserArray & xTT & strPipeDelimiterChar
Next xlngLoop
rs.MoveNext
Wend
If Len(xstrUserArray) > 0 Then xstrUserArray = Left(xstrUserArray, _
Len(xstrUserArray) - 1)
WhoIsInTheDatabaseLockFile = xstrUserArray
Exit_Function:
On Error Resume Next
rs.close
Set rs = Nothing
cn.close
Set cn = Nothing
dbs.close
Set dbs = Nothing
Exit Function
Err_Msg:
Debug.Print "Error occurred. Error number " & Err.Number & ": " & Err.Description
Resume Exit_Function
End Function