MJ_Wilkinson
Registered User.
- Local time
- Today, 23:15
- Joined
- Apr 26, 2017
- Messages
- 15
Hi,
I'm new to the forum and have introduced myself as such in the new members thread. I've inherited a database from someone and have been running the code on it fine for the past few months, occasionally tweaking wherever necessary.
However, now randomly, all of a sudden one section of code is throwing out the error 3048- "cannot open any more databases". It's so strange that it would be fine for months and now all of a sudden this error appears, I've changed no part of this sub.
All this sub is doing is updating tables M01, M02, M05, M08 & M12 from the data sat in Excel base tables, and assigning criteria such as 1 for Pass and 3 for Fail etc, based on performance against that measure in Tbl_Measures (e.g for M01- >0.9=1, <0.9=3).
My basic understanding is that this error is something to do with Access opening the tables and not closing them, but looking in the code I thought that they were being closed.
Anyway, I appreciate this is perhaps a long piece of code, and sorry if it's too long to be putting on these threads, however, any help you guys can offer to diagnose and fix this issue would be greatly appreciated. Looks like I could pick up some really useful info on here, keep up the good work all;
I'm new to the forum and have introduced myself as such in the new members thread. I've inherited a database from someone and have been running the code on it fine for the past few months, occasionally tweaking wherever necessary.
However, now randomly, all of a sudden one section of code is throwing out the error 3048- "cannot open any more databases". It's so strange that it would be fine for months and now all of a sudden this error appears, I've changed no part of this sub.
All this sub is doing is updating tables M01, M02, M05, M08 & M12 from the data sat in Excel base tables, and assigning criteria such as 1 for Pass and 3 for Fail etc, based on performance against that measure in Tbl_Measures (e.g for M01- >0.9=1, <0.9=3).
My basic understanding is that this error is something to do with Access opening the tables and not closing them, but looking in the code I thought that they were being closed.
Anyway, I appreciate this is perhaps a long piece of code, and sorry if it's too long to be putting on these threads, however, any help you guys can offer to diagnose and fix this issue would be greatly appreciated. Looks like I could pick up some really useful info on here, keep up the good work all;
Code:
Private Sub RunM01M02M05M08_REVISED()
'MEASURE LOOP
Dim db2 As Database
Dim LRS2 As DAO.Recordset
Dim loca2 As String
Dim valu2 As String
Set db2 = DBEngine(0)(0)
loca2 = "SELECT Measure From Tbl_Measure"
Set LRS2 = db2.OpenRecordset(loca2)
Do While Not LRS2.EOF
valu2 = LRS2("Measure")
LRS2.MoveNext
Dim TABLE As String
TABLE = DLookup("[Table]", "Tbl_Measure", "[Measure]='" & valu2 & "'")
Dim TARNUM As String
TARNUM = DLookup("[MeasureTarNum]", "Tbl_Measures", "[Measure]='" & valu2 & "'")
Dim MP As String
MP = DLookup("[MeasurePeriod]", "Tbl_Measures", "[Measure]='" & valu2 & "'")
Dim CP As String
CP = DLookup("[MeasureClosingPeriod]", "Tbl_Measures", "[Measure]='" & valu2 & "'")
If valu2 = "M02" Then
B1.BackColor = RGB(&H22, &HB1, &H4C)
Else
If valu2 = "M05" Then
B2.BackColor = RGB(&H22, &HB1, &H4C)
Else
If valu2 = "M08" Then
B5.BackColor = RGB(&H22, &HB1, &H4C)
Else
If valu2 = "M12" Then
B5.BackColor = RGB(&H22, &HB1, &H4C)
End If
End If
End If
End If
'BRAND LOOP
Dim DB As Database
Dim LRS As DAO.Recordset
Dim loca As String
Dim valu As String
Set DB = DBEngine(0)(0)
loca = "SELECT CIBrand From Tbl_Brand"
Set LRS = DB.OpenRecordset(loca)
Do While Not LRS.EOF
valu = LRS("CIBrand")
LRS.MoveNext
Dim LLINK As String
LLINK = DLookup("[LinkTable]", "Tbl_Links", "[CIBrand]='" & valu & "' And [Measure]='" & valu2 & "'")
'RETAILER LOOP
Dim db1 As Database
Dim LRS1 As DAO.Recordset
Dim loca1 As String
Dim valu1 As String
Set db1 = DBEngine(0)(0)
loca1 = "SELECT BCICode From Tbl_Ident Where [CIBrand]='" & valu & "'"
Set LRS1 = db1.OpenRecordset(loca1)
Do While Not LRS1.EOF
valu1 = LRS1("BCICode")
LRS1.MoveNext
Dim LCI As Variant
LCI = DLookup("[CICode]", "Tbl_Ident", "[BCICode]='" & valu1 & "'")
'*******************************EARN BACK******************************************
Dim LTAREB As Variant
LTAREB = DLookup("[Tar]", "Tbl_Earnback", "[BCICODE]='" & valu1 & "' And [Measure]='" & valu2 & "'")
If (IsNull(LTAREB)) Then
LTAREB = 0
End If
Dim LACTEB As Variant
LACTEB = DLookup("[Act]", "Tbl_Earnback", "[BCICODE]='" & valu1 & "' And [Measure]='" & valu2 & "'")
If (IsNull(LACTEB)) Then
LACTEB = 0
End If
'***********************************************************************************
Dim LTAR As Variant
LTAR = DLookup("[OBJ]", "" & LLINK & "", "[CICODE]='" & LCI & "'") + LTAREB
Dim LACT As Variant
LACT = DLookup("[ACT]", "" & LLINK & "", "[CICODE]='" & LCI & "'") + LACTEB
Dim LPERC As Variant '3 DECIMAL PLACES
LPERC = Round(DLookup("[VAL]", "" & LLINK & "", "[CICODE]='" & LCI & "'"), 3)
If LPERC >= TARNUM Then
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE " & TABLE & " SET [Tar] = " & LTAR & " Where [BCICode]='" & valu1 & "'"
DoCmd.RunSQL "UPDATE " & TABLE & " SET [Act] = " & LACT & " Where [BCICode]='" & valu1 & "'"
DoCmd.RunSQL "UPDATE " & TABLE & " SET [Perc] = " & LPERC & " Where [BCICode]='" & valu1 & "'"
DoCmd.RunSQL "UPDATE " & TABLE & " SET [PorF] = '1' Where [BCICode]='" & valu1 & "'"
DoCmd.SetWarnings True
Else
If LPERC < TARNUM And valu2 <> "M05" Then
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE " & TABLE & " SET [Tar] = " & LTAR & " Where [BCICode]='" & valu1 & "'"
DoCmd.RunSQL "UPDATE " & TABLE & " SET [Act] = " & LACT & " Where [BCICode]='" & valu1 & "'"
DoCmd.RunSQL "UPDATE " & TABLE & " SET [Perc] = " & LPERC & " Where [BCICode]='" & valu1 & "'"
DoCmd.RunSQL "UPDATE " & TABLE & " SET [PorF] = '3' Where [BCICode]='" & valu1 & "'"
DoCmd.SetWarnings True
Else
If LPERC < TARNUM And (valu2 = "M05" And [CP] = 0) Then
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE " & TABLE & " SET [Tar] = " & LTAR & " Where [BCICode]='" & valu1 & "'"
DoCmd.RunSQL "UPDATE " & TABLE & " SET [Act] = " & LACT & " Where [BCICode]='" & valu1 & "'"
DoCmd.RunSQL "UPDATE " & TABLE & " SET [Perc] = " & LPERC & " Where [BCICode]='" & valu1 & "'"
DoCmd.RunSQL "UPDATE " & TABLE & " SET [PorF] = '2' Where [BCICode]='" & valu1 & "'"
DoCmd.SetWarnings True
Else
If LPERC < TARNUM And (valu2 = "M05" And [CP] = -1) Then
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE " & TABLE & " SET [Tar] = " & LTAR & " Where [BCICode]='" & valu1 & "'"
DoCmd.RunSQL "UPDATE " & TABLE & " SET [Act] = " & LACT & " Where [BCICode]='" & valu1 & "'"
DoCmd.RunSQL "UPDATE " & TABLE & " SET [Perc] = " & LPERC & " Where [BCICode]='" & valu1 & "'"
DoCmd.RunSQL "UPDATE " & TABLE & " SET [PorF] = '3' Where [BCICode]='" & valu1 & "'"
DoCmd.SetWarnings True
Else
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE " & TABLE & " SET [PorF] = '0' Where [BCICode]='" & valu1 & "'"
DoCmd.SetWarnings True
End If
End If
End If
End If
Loop
LRS1.Close
Set LRS1 = Nothing
Set db1 = Nothing
Loop
LRS.Close
Set LRS = Nothing
Set DB = Nothing
Loop
LRS2.Close
Set LR2 = Nothing
Set db2 = Nothing
End Sub