Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

form reloads on it's own

Posted on 2003-12-02
16
Medium Priority
?
428 Views
Last Modified: 2010-05-01
i have a form that has a timer control.  in the form_unload procedure i make the timer.enabled=false.  sometimes the form reloads immediately after it's form_unload completes (happens intermittenylty, maybe once out 10, always randomly).  i have a feeling that even though the timer is disabled, a function that is called in the timer has not finished and is referencing a control's event after the form has unloaded, therefore the form reloads to facilitate the event.

is this plausible? and if so how can one prevent this from happening?
0
Comment
Question by:howardsd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 3
  • +2
16 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9861057
Do you have doevents in your code?
0
 

Author Comment

by:howardsd
ID: 9861106
no
0
 
LVL 2

Expert Comment

by:devoted2christ
ID: 9861199
Is disabling the timer the only thing in your form_unload procedure? And when the form reloads, is the timer enabled or disabled?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:EDDYKT
ID: 9861222
shows us the queryunload routine
0
 

Author Comment

by:howardsd
ID: 9861350
here's the unload routine.  in the form_load routine the timer gets enabled.

Private Sub Form_Unload(Cancel As Integer)

On Error GoTo err
  Timer1.Enabled = False

  If dbvalueconnection.state = adStateOpen Then
    dbvalueconnection.Close
  End If

  closeRecordsets rsvalueread, rsvalueidread
 
  Screen.MousePointer = vbDefault
 
  Set dbvalueconnection = Nothing
  Set rsvalueread = Nothing
  Set rsvalueidread = Nothing

  Exit Sub
 
err:
  If err <> 0 Then
    MsgBox err.Source & "-->" & err.Description, vbOKOnly, "Form unload failure"
  End If
End Sub
0
 
LVL 2

Expert Comment

by:devoted2christ
ID: 9861524
Try putting a Sleep in there (after the disabling) to delay the rest of the events...
0
 

Author Comment

by:howardsd
ID: 9861831
with a Sleep(2000) i get the same result.  i close the form, its waits 2 seconds, closes, then the form pops right back up.  still occuring randomly, no real pattern for when it does/doesn't happen).
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9862299
I think you need to show us what is in the Timer routine.

Idle_Mind
0
 

Author Comment

by:howardsd
ID: 9862323
Private Sub Timer1_Timer()
Dim d As String

  d = String(200, 0)
  l = SCardComand(0, "Card,Info,Status", 0, nil, 0, d, 200)
  d = Left(d, InStr(d, Chr(0)) - 1)
 
  If l = 0 Then
    If d = "active" Then
      cardStatus.Caption = "card in reader"
      If iSnglRead = False Then
        ' if no problems during read and loyalty id the active screen make form able
        If fillForm = True Then
          cmdAdd.Enabled = True
          cmdSub.Enabled = True
          cmdWriteCustCrd.Enabled = True
          cmdWriteRetailerCrd.Enabled = True
        End If
        iSnglClr = False
        iSnglRead = True
      End If
    ElseIf d = "wait" Then
      cardStatus.Caption = "insert card"
      If iSnglClr = False Then
        Call clearControlsValue
        lblCardHolder.Caption = ""
        cmdAdd.Enabled = False
        cmdSub.Enabled = False
        cmdWriteCustCrd.Enabled = False
        cmdWriteRetailerCrd.Enabled = False
        iSnglClr = True
        iSnglRead = False
      End If
    Else
      cardStatus.Caption = d
      cmdAdd.Enabled = False
      cmdSub.Enabled = False
    End If
  End If
End Sub
0
 

Author Comment

by:howardsd
ID: 9862351
the fillForm function reads a smart card and fills the form with the contents of the card
0
 
LVL 2

Expert Comment

by:ramyhh
ID: 9862391
add in the unload:

set form1 = nothing ' where form1 is the name of the form
0
 
LVL 2

Expert Comment

by:devoted2christ
ID: 9862440
Where is the form being controlled from? Is it the main form of the program, or is there another form or procedure that opens it? If the second is true, then is there any call being made to the form by the form that called it?
0
 

Author Comment

by:howardsd
ID: 9862499
this is the main form, but it is loaded by a splash form that flashes the user when the app is started. here is the form_load procedure for the splash form

valueSplash is the splash form (duh)
loyalty is the main form

Private Sub Form_Load()
  valueSplash.Show 0    
  Refresh  
  Sleep 2500
  valueSplash.Hide
  Unload valueSplash
  Load loyalty
End Sub
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 150 total points
ID: 9862590
If there is code executing in a loop or timer sub, it will prevent a form from unloading.    I have also seen cases where refreshing a control on the form will make it appear.

You could try adding a global boolean variable "unloading" and then set it to true in your form_unload sub.
Then in your timer code, check it before attempting to update your form controls.

Idle_Mind

Give this a whirl and see if it makes a difference.

' Code Follows...
Private unloading As Boolean

Private Sub Form_Load()
    unloading = False
End Sub

Private Sub Form_Unload(Cancel As Integer)

On Error GoTo err
  unloading = True ' <----- added line
  Timer1.Enabled = False

  If dbvalueconnection.state = adStateOpen Then
    dbvalueconnection.Close
  End If

  closeRecordsets rsvalueread, rsvalueidread
 
  Screen.MousePointer = vbDefault
 
  Set dbvalueconnection = Nothing
  Set rsvalueread = Nothing
  Set rsvalueidread = Nothing

  Exit Sub
 
err:
  If err <> 0 Then
    MsgBox err.Source & "-->" & err.Description, vbOKOnly, "Form unload failure"
  End If
End Sub

Private Sub Timer1_Timer()
Dim d As String

  d = String(200, 0)
  l = SCardComand(0, "Card,Info,Status", 0, nil, 0, d, 200)
  d = Left(d, InStr(d, Chr(0)) - 1)
 
  If l = 0 Then
    If d = "active" And Not unloading Then ' <--- if we are not unloading....
      cardStatus.Caption = "card in reader"
      If iSnglRead = False And Not unloading Then ' <--- if we are not unloading....
        ' if no problems during read and loyalty id the active screen make form able
        If fillForm = True And Not unloading Then ' <--- if we are not unloading....
          cmdAdd.Enabled = True
          cmdSub.Enabled = True
          cmdWriteCustCrd.Enabled = True
          cmdWriteRetailerCrd.Enabled = True
        End If
        iSnglClr = False
        iSnglRead = True
      End If
    ElseIf d = "wait" And Not unloading Then ' <--- if we are not unloading....
      cardStatus.Caption = "insert card"
      If iSnglClr = False And Not unloading Then ' <--- if we are not unloading....
        Call clearControlsValue
        lblCardHolder.Caption = ""
        cmdAdd.Enabled = False
        cmdSub.Enabled = False
        cmdWriteCustCrd.Enabled = False
        cmdWriteRetailerCrd.Enabled = False
        iSnglClr = True
        iSnglRead = False
      End If
    ElseIf  Not unloading then ' <--- if we are not unloading....
      cardStatus.Caption = d
      cmdAdd.Enabled = False
      cmdSub.Enabled = False
    End If
  End If
End Sub
0
 

Author Comment

by:howardsd
ID: 9862712
that seemed to have done it.  i tested 30 times without the occurence whereas before it happened at least 1 in 10

thnx!!!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9862749
No Problem.

Glad I could help.

Idle_Mind
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question