Solved Code wont run correctly unless stepping through with breakpoints? (1 Viewer)

Sam Summers

Registered User.
Local time
Today, 09:16
Joined
Sep 17, 2001
Messages
939
Hi Guys,

This code works as long as i set breakpoints and step through it but if i just open the form and try it does not initialise variable 'a'

I must be missing something because it works perfectly otherwise?

Many thanks in advance.

Code:
Private Sub Form_Load()

Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim e As Integer
Dim f As Integer
Dim g As Integer
Dim answer As Integer
Dim answer2 As Integer
Dim answer3 As Integer
Dim answer4 As Integer
Dim answer5 As Integer
Dim MyVar, MyVar2, MyCheck

    MyVar = Me.TotalHours.Value
    MyVar2 = Me.ServiceInterval.Value

    If MyCheck = IsNull(MyVar) Or MyVar = "" Then

    Else
    End If
    
    a = Me.TotalHours.Value
    b = Me.ServiceInterval.Value
    d = Me.Component_FlagInterval.Value
    f = Me.WarningAt.Value
    If IsNull(MyVar2) Or MyVar2 = "" Then
    Exit Sub
    Else
    If Me.TotalHours.Value < MyVar2 Then
    answer2 = b - Me.TotalHours.Value
 '   answer2 = b - a
    Me.DueIn.Value = answer2
    Else
    answer = a / b
    Me.Divider.Value = answer
    e = Me.ServiceHour.Value
    f = Me.WarningAt.Value
    g = Me.WarningHour.Value
    
    If a > e Then
    answer3 = b + e
    answer4 = answer3 - a
    Me.DueIn.Value = answer4
    Else
    
 End If
End If
End If

End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:16
Joined
May 21, 2018
Messages
8,463
For a start, the if checks make no sense to me.

This does nothing
Code:
 If MyCheck = IsNull(MyVar) Or MyVar = "" Then
    Else
    End If

and this is in the wrong place and no exit sub is needed
Code:
 If IsNull(MyVar2) Or MyVar2 = "" Then
    Exit Sub
    Else
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:16
Joined
May 21, 2018
Messages
8,463
Also your variables make no sense. You have two variables for Me.TotalHours.Value, and still do not use either of them

a = Me.TotalHours.Value
MyVar = Me.TotalHours.Value
If Me.TotalHours.Value < MyVar2 Then

The whole thing is kind of a mess. I would probably start over and write something that is clear an understandable.
 

Sam Summers

Registered User.
Local time
Today, 09:16
Joined
Sep 17, 2001
Messages
939
Also your variables make no sense. You have two variables for Me.TotalHours.Value, and still do not use either of them

a = Me.TotalHours.Value
MyVar = Me.TotalHours.Value
If Me.TotalHours.Value < MyVar2 Then

The whole thing is kind of a mess. I would probably start over and write something that is clear an understandable.
Well if i knew how to do that i would?
Its taken me over a week to get my head round what i have now and as i said it works perfectly when i step through it but not otherwise and i dont understand why?
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:16
Joined
Sep 21, 2011
Messages
14,050
You also need to indent your code, otherwise it is harder to debug?

Code:
Private Sub Form_Load()

    Dim a As Integer
    Dim b As Integer
    Dim c As Integer
    Dim d As Integer
    Dim e As Integer
    Dim f As Integer
    Dim g As Integer
    Dim answer As Integer
    Dim answer2 As Integer
    Dim answer3 As Integer
    Dim answer4 As Integer
    Dim answer5 As Integer
    Dim MyVar, MyVar2, MyCheck

    MyVar = Me.TotalHours.Value
    MyVar2 = Me.ServiceInterval.Value

    If MyCheck = IsNull(MyVar) Or MyVar = "" Then

    Else
    End If

    a = Me.TotalHours.Value
    b = Me.ServiceInterval.Value
    d = Me.Component_FlagInterval.Value
    f = Me.WarningAt.Value
    If IsNull(MyVar2) Or MyVar2 = "" Then
        Exit Sub
    Else
        If Me.TotalHours.Value < MyVar2 Then
            answer2 = b - Me.TotalHours.Value
            '   answer2 = b - a
            Me.DueIn.Value = answer2
        Else
            answer = a / b
            Me.Divider.Value = answer
            e = Me.ServiceHour.Value
            f = Me.WarningAt.Value
            g = Me.WarningHour.Value

            If a > e Then
                answer3 = b + e
                answer4 = answer3 - a
                Me.DueIn.Value = answer4
            Else

            End If
        End If
    End If

End Sub

Dim MyVar, MyVar2, MyCheck
These will be variants?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:16
Joined
Feb 28, 2001
Messages
27,001
I have actually seen something like what you are describing where single-stepping or breakpointing works but a straight-out run doesn't. Treat the DB as having been corrupted somehow.

First, decompile your code, then compact and repair your DB and then compile your code. (NOTE: In the referenced article below, which is a gathering of several tips, "decompile" is not the first entry. Scroll down a bit an you'll see it.) Always, always, ALWAYS make a backup copy before you start work like this since it involves multiple places where things could go wrong.


If that doesn't work, create a brand new empty database and then use the External Data option to import everything from the old file to the new one. DO NOT try to EXPORT from old to new. Do it as an import from the old file.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:16
Joined
Oct 29, 2018
Messages
21,358
Hi. Just curious, you said the problem was "it does not initialise variable 'a'," is that all? All the other variables get initialised?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:16
Joined
May 21, 2018
Messages
8,463
I think it is something like this, from what I can see.
Code:
Private Sub Form_Load()

if not (me.totalhours & "") = "" and Not (me.ServiceInterval & "") = "" then
    if me.TotalHours < me.ServiceInterval then
      me.dueinvalue = me.ServiceInterval - me.TotalHours
   else
      me.divider = me.TotalHours / me.ServiceInterval  
      if me.TotalHours > Me.ServiceHour then
         me.dueInValue = me.ServiceInterval + Me.ServiceHour - me.TotalHours
      end if
   end if
end if
end sub
 

Sam Summers

Registered User.
Local time
Today, 09:16
Joined
Sep 17, 2001
Messages
939
Amazing guys! Let me just check it all works and i will get straight back to you!
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:16
Joined
May 21, 2018
Messages
8,463
However, I think this can be shortened more, if I understand correctly

All equipment has a service interval, a last service (servicehour), and total hours
If it has never been serviced would the servicehour be null or zero?
If that is the case then this is even simpler. Also would the divider always be calculated regardless if it exceeded the service interval.

Code:
Private Sub Form_Load()


if not (me.totalhours & "") = "" and Not (me.ServiceInterval & "") = "" then
      me.divider = me.TotalHours / me.ServiceInterval 
      me.dueInValue = me.ServiceInterval + nz(Me.ServiceHour,0) - me.TotalHours
end if
end sub

if the
TotalHours = 75
ServiceInterval = 100
ServiceHours = Null or 0

Then Due in is: 100 + 0 - 75 = 25

if
TotalHours = 125
ServiceInterval = 100
ServiceHours = 100
Then Due in is: 100 + 100 - 125 = 75
 

Sam Summers

Registered User.
Local time
Today, 09:16
Joined
Sep 17, 2001
Messages
939
Right at the moment using MajP's earlier code i am getting a "Type mismatch" error on line 60

Code:
10 If Not (Me.TotalHours & "") = "" And Not (Me.ServiceInterval & "") = "" Then
20   If Me.TotalHours < Me.ServiceInterval Then
30      Me.DueIn.Value = Me.ServiceInterval - Me.TotalHours
40   Else
50      Me.Divider = Me.TotalHours / Me.ServiceInterval
60      If Me.TotalHours > Me.ServiceHour Then
70       Me.DueIn.Value = Me.ServiceInterval + Me.ServiceHour - Me.TotalHours
80      End If
90   End If
100 End If
 

Sam Summers

Registered User.
Local time
Today, 09:16
Joined
Sep 17, 2001
Messages
939
However, I think this can be shortened more, if I understand correctly

All equipment has a service interval, a last service (servicehour), and total hours
If it has never been serviced would the servicehour be null or zero?
If that is the case then this is even simpler. Also would the divider always be calculated regardless if it exceeded the service interval.

Code:
Private Sub Form_Load()


if not (me.totalhours & "") = "" and Not (me.ServiceInterval & "") = "" then
      me.divider = me.TotalHours / me.ServiceInterval
      me.dueInValue = me.ServiceInterval + nz(Me.ServiceHour,0) - me.TotalHours
end if
end sub

if the
TotalHours = 75
ServiceInterval = 100
ServiceHours = Null or 0

Then Due in is: 100 + 0 - 75 = 25

if
TotalHours = 125
ServiceInterval = 100
ServiceHours = 100
Then Due in is: 100 + 100 - 125 = 75
I'll try this just now
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:16
Joined
May 21, 2018
Messages
8,463
That would hint to me that Service hour is not a numeric. You will need to check that, because
100 > "SomeString"
will return a type mismatch.
 

Sam Summers

Registered User.
Local time
Today, 09:16
Joined
Sep 17, 2001
Messages
939
However, I think this can be shortened more, if I understand correctly

All equipment has a service interval, a last service (servicehour), and total hours
If it has never been serviced would the servicehour be null or zero?
If that is the case then this is even simpler. Also would the divider always be calculated regardless if it exceeded the service interval.

Code:
Private Sub Form_Load()


if not (me.totalhours & "") = "" and Not (me.ServiceInterval & "") = "" then
      me.divider = me.TotalHours / me.ServiceInterval
      me.dueInValue = me.ServiceInterval + nz(Me.ServiceHour,0) - me.TotalHours
end if
end sub

if the
TotalHours = 75
ServiceInterval = 100
ServiceHours = Null or 0

Then Due in is: 100 + 0 - 75 = 25

if
TotalHours = 125
ServiceInterval = 100
ServiceHours = 100
Then Due in is: 100 + 100 - 125 = 75
Yes it just threw an error again at nz(Me.ServiceHour,0)
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:16
Joined
May 21, 2018
Messages
8,463
You have that field likely bound to a text field. Look in the table and change to numeric.
 

Sam Summers

Registered User.
Local time
Today, 09:16
Joined
Sep 17, 2001
Messages
939
Hi. Just curious, you said the problem was "it does not initialise variable 'a'," is that all? All the other variables get initialised?
Yes all the others seem to work but i may have found the mismatch
 

Sam Summers

Registered User.
Local time
Today, 09:16
Joined
Sep 17, 2001
Messages
939
So after finding the mismatch, this is working best now but still only stepping through breakpoints?

I think i have to do what Doc Man said and get back to you. Fantastic help everyone!!

Code:
10 If Not (Me.TotalHours & "") = "" And Not (Me.ServiceInterval & "") = "" Then
20   If Me.TotalHours < Me.ServiceInterval Then
30      Me.DueIn.Value = Me.ServiceInterval - Me.TotalHours
40   Else
50      Me.Divider = Me.TotalHours / Me.ServiceInterval
60      If Me.TotalHours > Me.ServiceHour Then
70       Me.DueIn.Value = Me.ServiceInterval + Me.ServiceHour - Me.TotalHours
80      End If
90   End If
100 End If
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:16
Joined
May 21, 2018
Messages
8,463
This is a fine point but an empty field in some cases means the same as 0 in other cases it means you have not entered or does not exist. By that, if you are tracking hours on all equipment then you could default totalhours to zero. However for service interval an empty field means you have not recorded it yet or it does not have a specified interval. Also a control bound to a numeric field cannot have an empty string in it so no need to check for that. Then this gets even cleaner.

Code:
Private Sub Form_Load()
dim TotalHrs as long
dim ServInt as long

TotalHrs = nz(me.totalHours,0)
ServInt = nz(me.serviceInterval,0)

if not ServInt = 0 then
      me.divider = TotalHrs / ServInt 
      me.dueInValue = ServInt + nz(Me.ServiceHour,0) - TotalHrs
end sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:16
Joined
May 21, 2018
Messages
8,463
You have the code in the form's load event. So it runs only on the first record. I think you should be in the form's On Current event to run on each record. But more likely you need to call this in the after update of TotalHrs, ServInt, and ServiceHours. So make a general procedure so you can call "CalculateDueIn" from each of these events.
Code:
Private Sub CalculateDueIn()
dim TotalHrs as long
dim ServInt as long

TotalHrs = nz(me.totalHours,0)
ServInt = nz(me.serviceInterval,0)

if not ServInt = 0 then
      me.divider = TotalHrs / ServInt
      me.dueInValue = ServInt + nz(Me.ServiceHour,0) - TotalHrs
end if
end sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:16
Joined
Oct 29, 2018
Messages
21,358
Yes all the others seem to work but i may have found the mismatch
In that case, I have another idea you could try, but I won't muddy the waters any further. Let us know how you get along with your current path. Cheers!
 

Users who are viewing this thread

Top Bottom