Speed up my VBA access Code

mfaqueiroz

Registered User.
Local time
Today, 13:37
Joined
Sep 30, 2015
Messages
125
Hi :)

I'm trying to use this code to accelerate my program....

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False



... but appears always this error:
method or data not found

Could you please help me?Do you have other tips to speed up my code?


I really appreciate your help!
 
What code are you actually trying to speed up ?
 
Hey Minty! Thanks for your reply. If you coulld help me would be great!!
there is my code:


Code:
Sub OffClassifications()
'This code will analyse the registers with off status and verify what happens 4 seconds before! Beyond the EventCode i can say if the off is ok or not.
Dim Ins As String
Dim Count1 As Integer
Dim Count2 As Integer
Dim Count3 As Integer
Dim Count4 As Integer
Dim CountON As Integer

Dim IDMachine As String
Dim Machine As String
Dim CodeMachine As String
Dim Date As Integer
Dim Tag As String
Dim id As Long
Dim Classe1 As String
Dim Classe2 As String
Dim Classe3 As String
Dim Classe4 As String
Dim Classe5 As String
Dim Classe6 As String

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False


Set dbs = DBEngine(0)(0)
Set Off = dbs.OpenRecordset("Tab1", DB_OPEN_DYNASET) 'Abre a Query de input, onde estao todos os dados de analise
Set ResultsTable = dbs.OpenRecordset("ResultsTable", DB_OPEN_DYNASET) '


Classe1 = Ok
Classe2 =  Caution
Classe3 = Maintenance
Classe5 = Not Ok
Classe6 =  Analyse


Off.MoveFirst   ---> I will see my first off

    Do While Not Off.EOF
    Count1 = 0   ' count the number of times that the EventCode =1 appears
    Count2 = 0
    Count3 = 0
    Count4 = 0
    Count5 = 0
    CountONs = 0
    
     Machine = Off.Fields(4).Value
     CodeMachine = Off.Fields(14).Value 
     IDMachine = Off.Fields(5).Value 
     id = Off.Fields(0).Value   'ID
     Date = Off.Fields(3).Value
     Date = Off.Fields(2).Value
     Tag = Off.Fields(8).Value
     
     
    Diference = DateAdd("s", -4, CDate(Format(Date, "dd-mm-yyyy hh:mm:ss")))
     DateComparision = Mid(Diferenca35, 7, 4) & "-" & Mid(Diferenca35, 4, 2) & "-" & Mid(Diferenca35, 1, 2) & " " & TimeValue(Diferenca35)
     Date1 = Mid(Off.Fields(2).Value, 7, 4) & "-" & Mid(Off.Fields(2).Value, 4, 2) & "-" & Mid(Off.Fields(2).Value, 1, 2) & " " & TimeValue(Off.Fields(2).Value)
     
    ' I will see  4 seconds before MY off how many EventCode=1,=2;=3,=4 happened
       Count1 = Nz(DCount("*", "TabMachine", " (((TabMachine.[Date])< #" & Date1 & "#) OR (((TabMachine.[ms]) <= " & Date & ") AND ((TabMachine.[Date]) = #" & Date1 & "#))) and  ((TabMachine.[Date])>= #" & DateComparision & "#) And TabMachine.[EventCode] = '1' AND TabMachine.[IDMachinenel] = '" & IDMachine & "' AND TabMachine.[Machine]= '" & Machine & "' "), 0)
       Count2 = Nz(DCount("*", "TabMachine", " (((TabMachine.[Date])< #" & Date1 & "#) OR (((TabMachine.[ms]) <= " & Date & ") AND ((TabMachine.[Date])= #" & Date1 & "#))) and  ((TabMachine.[Date])>= #" & DateComparision & "#) And TabMachine.[EventCode] = '2' AND TabMachine.[IDMachinenel] = '" & IDMachine & "' AND TabMachine.[Machinealação]= '" & Machine & "' "), 0)
       Count4 = Nz(DCount("*", "TabMachine", " (((TabMachine.[Date])< #" & Date1 & "#) OR (((TabMachine.[ms]) <= " & Date & ") AND ((TabMachine.[Date])= #" & Date1 & "#))) and  ((TabMachine.[Date])>= #" & DateComparision & "#) And TabMachine.[EventCode] = '4' AND TabMachine.[IDMachinenel] = '" & IDMachine & "' AND TabMachine.[Machinealação]= '" & Machine & "' "), 0)
       Count3 = Nz(DCount("*", "TabMachine", " (((TabMachine.[Date])< #" & Date1 & "#) OR (((TabMachine.[ms]) <= " & Date & ") AND ((TabMachine.[Date])= #" & Date1 & "#))) and  ((TabMachine.[Date])>= #" & DateComparision & "#) And TabMachine.[EventCode] = '3' AND TabMachine.[Machinealação]= '" & Machine & "' "), 0)
       
    
'I have 3 CodeMachines and the classification it's different for each one
 
           If CodeMachine = "12ZZ" Then
           
        
            
            If (Count1 <> 0 And Count2 <> 0 And Count3 <> 0) Or (Count1 = 0 And Count2 <> 0 And Count3 <> 0) Or (Count1 <> 0 And Count2 = 0 And Count3 <> 0) Then 
            ResultsTable.AddNew
            ResultsTable.Fields(1).Value = id
            ResultsTable.Fields(2).Value = Classe1
            ResultsTable.Fields(4).Value = IDMachine
            ResultsTable.Fields(5).Value = Machine
            ResultsTable.Fields(6).Value = Date
            ResultsTable.Fields(7).Value = CodeMachine
            ResultsTable.Fields(11).Value = Date
            ResultsTable.Fields(14).Value = Tag
            ResultsTable.Fields(16).Value = "Off"
            ResultsTable.Update
            
            ElseIf (Count1 <> 0 And Count2 = 0 And Count3 = 0) Then 
             ResultsTable.AddNew
            ResultsTable.Fields(1).Value = id
            ResultsTable.Fields(2).Value = Classe3
            ResultsTable.Fields(4).Value = IDMachine
            ResultsTable.Fields(5).Value = Machine
            ResultsTable.Fields(6).Value = Date
            ResultsTable.Fields(7).Value = CodeMachine
            ResultsTable.Fields(11).Value = Date
            ResultsTable.Fields(14).Value = Tag
            ResultsTable.Fields(16).Value = "Off"
            ResultsTable.Update
            
            ElseIf (Count1 <> 0 And Count2 <> 0 And Count3 = 0) Or (Count1 = 0 And Count2 <> 0 And Count3 = 0) Then  
            ResultsTable.AddNew
            ResultsTable.Fields(1).Value = id
            ResultsTable.Fields(2).Value = Classe6
            ResultsTable.Fields(4).Value = IDMachine
            ResultsTable.Fields(5).Value = Machine
            ResultsTable.Fields(6).Value = Date
            ResultsTable.Fields(7).Value = CodeMachine
            ResultsTable.Fields(11).Value = Date
            ResultsTable.Fields(14).Value = Tag
            ResultsTable.Fields(16).Value = "Off"
            ResultsTable.Update
            
            
            Else 
            ResultsTable.AddNew
            ResultsTable.Fields(1).Value = id
            ResultsTable.Fields(2).Value = Classe2
            ResultsTable.Fields(4).Value = IDMachine
            ResultsTable.Fields(5).Value = Machine
            ResultsTable.Fields(6).Value = Date
            ResultsTable.Fields(7).Value = CodeMachine
            ResultsTable.Fields(11).Value = Date
            ResultsTable.Fields(14).Value = Tag
            ResultsTable.Fields(16).Value = "Off"
            ResultsTable.Update
            End If
            
                ElseIf CodeMachine = "12PK" Then
                
                If (Count1 = 0 And Count2 <> 0) Or (Count1 <> 0 And Count2 <> 0) Then  
                ResultsTable.AddNew
                ResultsTable.Fields(1).Value = id
                ResultsTable.Fields(2).Value = Classe1
                ResultsTable.Fields(4).Value = IDMachine
                ResultsTable.Fields(5).Value = Machine
                ResultsTable.Fields(6).Value = Date
                ResultsTable.Fields(7).Value = CodeMachine
                ResultsTable.Fields(11).Value = Date
                ResultsTable.Fields(14).Value = Tag
                ResultsTable.Fields(16).Value = "Off"
                ResultsTable.Update
                
                ElseIf Count1 = 0 And Count2 = 0 Then 
                ResultsTable.AddNew
                ResultsTable.Fields(1).Value = id
                ResultsTable.Fields(2).Value = Classe2
                ResultsTable.Fields(4).Value = IDMachine
                ResultsTable.Fields(5).Value = Machine
                ResultsTable.Fields(6).Value = Date
                ResultsTable.Fields(7).Value = CodeMachine
                ResultsTable.Fields(11).Value = Date
                ResultsTable.Fields(14).Value = Tag
                ResultsTable.Fields(16).Value = "Off"
                ResultsTable.Update
            
         
                ElseIf Count1 <> 0 And Count2 = 0 Then 
                ResultsTable.AddNew
                ResultsTable.Fields(1).Value = id
                ResultsTable.Fields(2).Value = Classe3
                ResultsTable.Fields(4).Value = IDMachine
                ResultsTable.Fields(5).Value = Machine
                ResultsTable.Fields(6).Value = Date
                ResultsTable.Fields(7).Value = CodeMachine
                ResultsTable.Fields(11).Value = Date
                ResultsTable.Fields(14).Value = Tag
                ResultsTable.Fields(16).Value = "Off"
                ResultsTable.Update
        
                 End If
                
                     ElseIf CodeMachine = "PT12" Then
                
                       If (Count1 = 0 And Count3 <> 0) Or (Count3 <> 0 And Count2 <> 0) Then   
                       ResultsTable.AddNew
                       ResultsTable.Fields(1).Value = id
                       ResultsTable.Fields(2).Value = Classe1
                       ResultsTable.Fields(4).Value = IDMachine
                       ResultsTable.Fields(5).Value = Machine
                       ResultsTable.Fields(6).Value = Date
                       ResultsTable.Fields(7).Value = CodeMachine
                       ResultsTable.Fields(11).Value = Date
                       ResultsTable.Fields(14).Value = Tag
                       ResultsTable.Fields(16).Value = "Off"
                       ResultsTable.Update
                       
                       ElseIf Count1 <> 0 And Count3 <> 0 Then 
                       ResultsTable.AddNew
                       ResultsTable.Fields(1).Value = id
                       ResultsTable.Fields(2).Value = Classe2
                       ResultsTable.Fields(4).Value = IDMachine
                       ResultsTable.Fields(5).Value = Machine
                       ResultsTable.Fields(6).Value = Date
                       ResultsTable.Fields(7).Value = CodeMachine
                       ResultsTable.Fields(11).Value = Date
                       ResultsTable.Fields(14).Value = Tag
                       ResultsTable.Fields(16).Value = "Off"
                       ResultsTable.Update
                   
                
                       ElseIf Count1 <> 0 And Count3 = 0 Then 
                       ResultsTable.AddNew
                       ResultsTable.Fields(1).Value = id
                       ResultsTable.Fields(2).Value = Classe3
                       ResultsTable.Fields(4).Value = IDMachine
                       ResultsTable.Fields(5).Value = Machine
                       ResultsTable.Fields(6).Value = Date
                       ResultsTable.Fields(7).Value = CodeMachine
                       ResultsTable.Fields(11).Value = Date 'CodeMachinenel
                       ResultsTable.Fields(14).Value = Tag
                       ResultsTable.Fields(16).Value = "Off"
                       ResultsTable.Update
        
                      
                       Else 'Desconhecido
                       ResultsTable.AddNew
                       ResultsTable.Fields(1).Value = id
                       ResultsTable.Fields(2).Value = Classe2
                       ResultsTable.Fields(4).Value = IDMachine
                       ResultsTable.Fields(5).Value = Machine
                       ResultsTable.Fields(6).Value = Date
                       ResultsTable.Fields(7).Value = CodeMachine
                       ResultsTable.Fields(11).Value = Date
                       ResultsTable.Fields(14).Value = Tag
                       ResultsTable.Fields(16).Value = "Off"
                       ResultsTable.Update
                        
                        
                        End If
                        
            
                End If
          
           Off.MoveNext
           Loop

            
 
           End Sub
 
Last edited by a moderator:
How about telling readers, in plain English, WHAT you are trying to do?
What is this code suppose to do?
Does it actually run?
 
Last edited:
  • Like
Reactions: Rx_
Ok :)!

I want to speed up my VBA codes, i've read that I can use in an generic way this code:

Sub xxx()

Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False

my code

Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True

End Sub

I've tried to use that but appears this error:
method or data not found

1-This code works for every kind off codes to speed up?

2-In my particular situation,
-I have one table with a lot of machine registers, I've the status (could be off,on, alarm, in course, programmed....) the date ( dd-mm-yyyy hh:mm:ss) and the Eventcode (could be 1,2,3).
-There are 3 kind of machines that are differentiated by CodeMachine for each one there is a decision tree where i analyse the EventCode 4 seconds before the register with status off:
-If i have 2 and 1 is ok...
- If i....(see code in my above post)

I hope that I am clear in this.
:)
Thank you for your time and help.
 
I can see A LOT wrong with your code. Namely variable declaration.
Add
Code:
Option Explicit
to the top of your module, then debug to see the variables you have not explicitly declared.

Secondly, the most likely cause of slowness is the DCount functions. You can use the following to pull a recordset that groups up the counts and should be faster:
Code:
    ' I will see 4 seconds before MY off how many EventCode=1,=2;=3,=4 happened
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("TRANSFORM Count(ID) SELECT 'Response' FROM TabMachine" & _
        " WHERE ((([Date])< #" & Date1 & "#) OR ((([ms]) <= " & Date & ") AND (([Date]) = #" & Date1 & "#))) and (([Date])>= #" & DateComparision & "#) And [IDMachinenel] = '" & IDMachine & "' AND [Machine]= '" & Machine & "'" & _
        " GROUP BY 'Response' PIVOT EventCode IN (1,2,3,4)", dbOpenDynaset)
    Count1 = rs![1]
    Count2 = rs![2]
    Count3 = rs![3]
    Count4 = rs![4]

Thirdly, The section of code that writes to your table is WAY overwritten. It looks like the only thing that changes based on your criteria is the Classe#.
That said, you could use this to clean that up:
Code:
'I have 3 CodeMachines and the classification it's different for each one
    ResultsTable.AddNew
    ResultsTable.Fields(1).Value = id
    If CodeMachine = "12ZZ" Then
        If (Count1 <> 0 And Count2 <> 0 And Count3 <> 0) Or (Count1 = 0 And Count2 <> 0 And Count3 <> 0) Or (Count1 <> 0 And Count2 = 0 And Count3 <> 0) Then
            ResultsTable.Fields(2).Value = Classe1
        ElseIf (Count1 <> 0 And Count2 = 0 And Count3 = 0) Then
            ResultsTable.Fields(2).Value = Classe3
        ElseIf (Count1 <> 0 And Count2 <> 0 And Count3 = 0) Or (Count1 = 0 And Count2 <> 0 And Count3 = 0) Then
            ResultsTable.Fields(2).Value = Classe6
        Else
            ResultsTable.Fields(2).Value = Classe2
        End If
    ElseIf CodeMachine = "12PK" Then
        If (Count1 = 0 And Count2 <> 0) Or (Count1 <> 0 And Count2 <> 0) Then
            ResultsTable.Fields(2).Value = Classe1
        ElseIf Count1 = 0 And Count2 = 0 Then
            ResultsTable.Fields(2).Value = Classe2
        ElseIf Count1 <> 0 And Count2 = 0 Then
            ResultsTable.Fields(2).Value = Classe3
        End If
    ElseIf CodeMachine = "PT12" Then
        If (Count1 = 0 And Count3 <> 0) Or (Count3 <> 0 And Count2 <> 0) Then
            ResultsTable.Fields(2).Value = Classe1
        ElseIf Count1 <> 0 And Count3 <> 0 Then
            ResultsTable.Fields(2).Value = Classe2
        ElseIf Count1 <> 0 And Count3 = 0 Then
            ResultsTable.Fields(2).Value = Classe3
        Else 'Desconhecido
            ResultsTable.Fields(2).Value = Classe2
        End If
    End If
    ResultsTable.Fields(4).Value = IDMachine
    ResultsTable.Fields(5).Value = Machine
    ResultsTable.Fields(6).Value = Date
    ResultsTable.Fields(7).Value = CodeMachine
    ResultsTable.Fields(11).Value = Date
    ResultsTable.Fields(14).Value = Tag
    ResultsTable.Fields(16).Value = "Off"
    ResultsTable.Update
    
    Off.MoveNext
 
  • Like
Reactions: Rx_
I am grateful for your help,I learned a lot from it! :) I will apply this right now!
 
i've also this sub that is really slow, do you have any suggestion to improve my code?

I have one table with entrys with two status: Alamr and Man(from Manual).
For each status=Alarm there is an Man pair. So I want to discover the sets Alarm-Man. Sometimes I have repeated Alarms or Man status that might be deleted.
So I made an routine that puts one "1" when the alarm has a pair Man, and when there are repeated and it puts one "2"..


Ex:
Alarm 1
Man 1
Man 2
Man 2
Alarm 1
Man 1
Alarm 1
Man 1

Should be:
Alamr 1
Man
Alarm 1
Man 1
Alarm 1


The code:

Code:
Sub Check()

Dim Machine As String
Dim Desc As String
Dim MachineCode As String
Dim Status As string
Dim id as Long
Dim TimeMs as long

Set dbs = DBEngine(0)(0)
Set TabMachine = dbs.OpenRecordset("TabMachine", DB_OPEN_DYNASET)
Set AlarmManConta = dbs.OpenRecordset("SELECT TabMachine.ID, TabMachine.[My date], TabMachine.[Date], TabMachine.[ms], TabMachine.[Machine], TabMachine.[MachineCode], TabMachine.[Description], TabMachine.[Status], TabMachine.[Tag], TabMachine.[TimeMs] , TabMachine.[Check] FROM TabMachine WHERE  (TabMachine.[Check] is Null) and TabMachine.[Tag] like '*KKK' and ((TabMachine.[Status])='Alarm' Or (TabMachine.[Status])='MAN') ORDER BY TabMachine.[TimeMs];", DB_OPEN_DYNASET)


Do While Not AlarmManConta.EOF
Set AlarmMan = dbs.OpenRecordset("SELECT TabMachine.ID, TabMachine.[My date], TabMachine.[Date], TabMachine.[ms], TabMachine.[Machine], TabMachine.[MachineCode], TabMachine.[Description], TabMachine.[Status], TabMachine.[Tag], TabMachine.[TimeMs] , TabMachine.[Check] FROM TabMachine WHERE  TabMachine.[Tag] like '*KKK' and ((TabMachine.[Status])='Alarm' Or (TabMachine.[Status])='MAN') and (TabMachine.[Check] is Null) ORDER BY TabMachine.[TimeMs];", DB_OPEN_DYNASET)


If AlarmMan.RecordCount = 0 Then
Exit Sub
End If
    
    
AlarmMan.MoveFirst
    Machine = AlarmMan.Fields(4).Value
    MachineCode = AlarmMan.Fields(5).Value
    Desc = AlarmMan.Fields(6).Value
    Status = AlarmMan.Fields(7).Value
    TimeMs = AlarmMan.Fields(9).Value
    id = AlarmMan.Fields(0).Value

    If Status = "MAN" Or Status = "Alarm" Then

        Set AlarmMan2 = dbs.OpenRecordset("SELECT TabMachine.ID, TabMachine.[My date], TabMachine.[Date], TabMachine.[ms], TabMachine.[Machine], TabMachine.[MachineCode], TabMachine.[Description], TabMachine.[Status], TabMachine.[Tag], TabMachine.[TimeMs], TabMachine.[Check] FROM TabMachine WHERE  TabMachine.[TimeMs] < " & TimeMs & " AND  TabMachine.[Description]= '" & Desc & "' and TabMachine.[Machine]= '" & Machine & "' and TabMachine.[MachineCode]= '" & MachineCode & "' and TabMachine.[Tag] like '*KKK' and ((TabMachine.[Status])='Alarm' Or (TabMachine.[Status])='MAN') and ((TabMachine.[Check] is Null) or TabMachine.[Check]= '0') ORDER BY TabMachine.[TimeMs];", DB_OPEN_DYNASET)
        If AlarmMan2.RecordCount <> 0 Then
        AlarmMan2.MoveLast
        id2 = AlarmMan2.Fields(0).Value
            If AlarmMan2.Fields(0) <> id Then
            
                If Status <> AlarmMan2.Fields(7).Value Then ' Foi Check e tem par, escrevo 1
                TabMachine.FindFirst "[ID] Like " & id & ""
                TabMachine.Edit
                TabMachine.Fields(11) = "1"
                TabMachine.Update
                
                TabMachine.FindFirst "[ID] Like " & id2 & ""
                TabMachine.Edit
                TabMachine.Fields(11) = "1"
                TabMachine.Update
            
    
                ElseIf Status = AlarmMan2.Fields(7) Then ' KKKa repetido, devo apagar
                TabMachine.FindFirst "[ID] Like " & id2 & ""
                TabMachine.Edit
                TabMachine.Fields(11) = "2"
                TabMachine.Update
                
                TabMachine.FindFirst "[ID] Like " & id & ""
                TabMachine.Edit
                TabMachine.Fields(11) = "0"
                TabMachine.Update
                End If
                End If
            
            Else
            TabMachine.FindFirst "[ID] Like " & id & ""
            TabMachine.Edit
            TabMachine.Fields(11) = "0"
            TabMachine.Update
            End If
            
       
           AlarmMan.MoveNext
     
     
            End If
            Loop
            
            DoCmd.RunSQL "DELETE * from TabMachine where TabMachine.[Check] ='2'"
            
            End Sub

[note: I asked in other post for help with an similar issue, that was told me to use SQL(the code is above) that worked really good for few registers but isn't working so well with more registers- i'm working with arround 50.000 registers of Alarm/Man]


DELETE *
FROM AlarmMan
WHERE ((((SELECT TOP 1 Dupe.Status
   FROM AlarmMan AS Dupe
   WHERE Dupe.TimeMS >AlarmMan.TimeMS
     AND Dupe.Machine = AlarmMan.Machine
     AND Dupe.Area = AlarmMan.Area
     AND Dupe.MachineCode = AlarmMan.MachineCode

   ORDER BY Dupe.TimeMS ASC,Dupe.ID))=[AlarmMan..[Status]));

I appreciate all your willingness to help :)!
 
Last edited by a moderator:
TJPoorman :) I've tried to use this code, but appears always this error "run-time error nr 94 invalid use of null" and appears one pop up saying " item not found in this collection"
Do you have ideia what could be wrong?

Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("TRANSFORM Count(ID) SELECT 'Response' FROM TabMachine" & _
" WHERE ((([Date])< #" & Date1 & "#) OR ((([ms]) <= " & Date & ") AND (([Date]) = #" & Date1 & "#))) and (([Date])>= #" & DateComparision & "#) And [IDMachinenel] = '" & IDMachine & "' AND [Machine]= '" & Machine & "'" & _
" GROUP BY 'Response' PIVOT EventCode IN (1,2,3,4)", dbOpenDynaset)
Count1 = rs![1]
Count2 = rs![2]
Count3 = rs![3]
Count4 = rs![4]
 
Last edited:
Declare the rs sql as a string first then debug.print the sql string statement to show you what it's trying to use.
 
Thank you Minty! :)
I did that and appears "Run-time error '13' Type mismatch" ....maybe I didn't understood

Dim rs As String
Set rs = CurrentDb.OpenRecordset("TRANSFORM Count(ID) SELECT 'Response' FROM TabMachine" & _
" WHERE ((([Date])< #" & Date1 & "#) OR ((([ms]) <= " & Date & ") AND (([Date]) = #" & Date1 & "#))) and (([Date])>= #" & DateComparision & "#) And [IDMachinenel] = '" & IDMachine & "' AND [Machine]= '" & Machine & "'" & _
" GROUP BY 'Response' PIVOT EventCode IN (1,2,3,4)", dbOpenDynaset)
debug.print rs
Count1 = rs![1]
Count2 = rs![2]
Count3 = rs![3]
Count4 = rs![4]
 
No quite what I had in mind...
Code:
Dim strSql as string
Set strSql = "TRANSFORM Count(ID) SELECT 'Response' FROM TabMachine" & _
" WHERE ((([Date])< #" & Date1 & "#) OR ((([ms]) <= " & Date & ") AND (([Date]) = #" & Date1 & "#))) and (([Date])>= #" & DateComparision & "#) And [IDMachinenel] = '" & IDMachine & "' AND [Machine]= '" & Machine & "'" & _
" GROUP BY 'Response' PIVOT EventCode IN (1,2,3,4)"
debug.print strSql

Set rs = CurrentDb.OpenRecordset(strSql, dbOpensDynaset)

This will set up the sql string and print it out to the immediate window before trying to open the recordset with the same sql string
 
Ahh :) ok!!
I've done that

In my debug print window i can see:
TRANSFORM Count(ID) SELECT 'Response' FROM TabMachine WHERE ((([Date])< #2014-01-05 10:41:44#) OR ((([ms]) <= 110) AND (([Date]) = #2014-01-05 10:41:44#))) and (([Date])>= #2014-01-05 10:41:40#) And [IDMachinenel] = 'KPB11' AND [MACHINE]= 'Markus12' GROUP BY 'Response' PIVOT [EventCode] IN (1,2,3,4)

and when i write set rs=CurrentDb.OpenRecordset(strSql, dbOpensDynaset)
appears this error: Run-time errro '3001' invalid argument
 
Oaky I would say the 'Response' bit is not correct and should be [Resposnse]

I know you have a lot of code but I would also really change the name of your Date field to something other than [Date] - it makes the whole thing virtually impossible to read through. and if you forget anywhere to enclose it in brackets you'll get all sorts of strange issues.
 
Thanks Minty! :) I changed the date field !
I also change 'response' to [response] but the error 3001 remains :(
 
No 'Response' is correct. Do you have a field called [ID] or is your primary key called something else?
 
Just as an observation, you have a LOT of this construct: object.value = ... or something = object.value

For Access, the default property IS .value so you could reduce the amount of typing (and correspondingly increase the readability) by removing .VALUE for objects that actually have a .VALUE property.

Although I can't point to a specific way to do this, I suspect that you could DRASTICALLY improve the speed of the code by not having that recordset open and doing a huge IF ladder. On the other hand, if the recordset is relatively small, the difference between looping through a recordset and usinq selective SQL UPDATE queries would be small. How large is your list of machines? (I didn't notice it in passing through the post because it is so complex.)

What I am talking about is, for large recordsets, it might pay you to find a way to do two or three or four UPDATE queries.

Code:
UPDATE table SET field1=value1 WHERE x=1 AND y=2 AND z=3 .... ;
UPDATE table SET field1=value2 WHERE x=0 AND y>2 AND z<3 .... ;
UPDATE table SET field1=value3 WHERE x<>0 AND y=0 and z>3 .... ;

If the table in question is short, this WILL NOT make a difference - but if you are talking large tables, UPDATE queries are noticeably faster than recordset loops. How large is large? Well, if you are worried about speeding up your code, that becomes a significant question but sadly, I can't answer that for you. It will always be a matter of what you can tolerate vs. how much effort you are ready to apply to the thing that is at the margins of your tolerance level. Certainly if the loop takes an annoying number of seconds, this might be better done with SQL. If the loop is a matter of a couple of seconds, maybe it is better to live with it.
 
dbOpensDynaset

There's your problem. It should be dbOpenDynaset.
Again this is a very good example of why variable declaration is important.
When you attempt to compile the code it will throw a flag on these problems.
 
The Doc_Mac!! thank you this was really usefull :D!!
TjPoorman thanks, was well noticed :) i've correct , now appears this error on Count1=rs![1] -> run time error 94 invalid use of null
 

Users who are viewing this thread

Back
Top Bottom