0

I'm having some trouble with a display object that I use to trigger a sub. The results of the display object are either true or false, and I use the _Change method. The code is quite simple.

Private Sub clamshellLblRequest_Change()
    If Not tagDisplay Is Nothing Then
        GoTo execute
    Else
        Set tagDisplay = LoadedDisplays
        GoTo execute
    End If

execute:
        If clamshellLblRequest.Value = 1 Then
            LogDiagnosticsMessage "Requesting clamshell label information"
            Call labels.clamshell
        End If
End Sub

When I first start the application, I get a "type mismatch" error (13) specific to this value. I have several other display objects that I use the same way with the same datatype but don't seem to have this problem. What else could be causing this?

Update: I have a module I use standard timers with that include the following.

Public Sub tenthSec()
    'Create a program delay, DateTime Timer resolution in MSWindows is 0.01.  Needed for tag updates.
    t = Timer
    While Timer - t < 0.1
    Wend
End Sub

When I execute call timers.tenthSec just before evaluating the value of the object, it doesn't seem to throw the type mismatch.

...    
execute:
        Call timers.tenthSec
        If clamshellLblRequest.Value = 1 Then
            LogDiagnosticsMessage "Requesting clamshell label information"
            Call labels.clamshell
        End If
End Sub

I wouldn't call this a solution, perhaps a band-aid. Any thoughts?

Community
  • 1
  • 1
addohm
  • 2,248
  • 3
  • 14
  • 40
  • Where do you define your variables? Type mismatch comes from there and we cannot see it. – M-- Apr 27 '17 at 15:13
  • `clamshellLblRequest.Value` is the object value. It's not defined from within the VBA itself. It is boolean. – addohm Apr 27 '17 at 15:14
  • Instead of writing a `Sub` for wait, do this: `Application.Wait(Now + #0:00:01#)` This adds one second wait to your code. – M-- Apr 27 '17 at 15:17
  • I don't understand the part `If clamshellLblRequest.Value = 1 Then` because if `Value` is of type `Boolean` then the condition [will never be true](http://stackoverflow.com/questions/3621037/casting-a-boolean-to-an-integer-returns-1-for-true). Are you sure `Value` is `Boolean`? – Daniel Dušek Apr 27 '17 at 16:11
  • The software this code ties into is proprietary. It will not accept `1` as `True`. It threw me for a loop for a LONG time because I couldn't figure out why my code was so broken in random places. Basically, when I interact with the external software, I need to use `1` and `0` instead of `True`and `False`. – addohm Apr 27 '17 at 17:51

1 Answers1

0

Agree with @Masoud about the wait. You could also use DoEvents inside of a loop, which allows other things to keep calculating, etc. Also, you shouldn't need the execute: and goto with the code you have, you should be able to just do something like this (note the change of Not Is Nothing to Is Nothing):

Private Sub clamshellLblRequest_Change()
    If tagDisplay Is Nothing Then
        Set tagDisplay = LoadedDisplays
    End If

    Application.Wait(Now + #0:00:01#)
    ' or
    For i = 1 to 1000
        DoEvents
    Next i

    If clamshellLblRequest.Value = 1 Then
         LogDiagnosticsMessage "Requesting clamshell label information"
         Call labels.clamshell
    End If
End Sub
Hauffa
  • 11
  • 2
  • That seems logical. I keep repeatedly used tasks (not necessarily repeatedly used within one piece of software, but frequently used across many pieces of software) in modules so I can easily dump them in and access them. – addohm Apr 27 '17 at 17:53