Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeat function..

Status
Not open for further replies.

Nightsoft

Programmer
Aug 23, 2001
50
0
0
DK
Hi.

I'm trying to make a form with a listbox, which has to be populatede, repeatedly...
I've tryied with a delay function I found, but, all it does, is to hang my app.. This is my code so far.. :

[tt]Public Sub Delay(lngSeconds As Long)
Dim lngStart As Long
lngStart = Timer

Do While Timer <= intStart + lngSeconds
DoEvents
Loop
End Sub
[/tt]

And then in a function called ListSager i end it with
Sub ListSager()
' code to puopulate listbox, - IT works.

Delay(5)
ListSager
End Sub

I don't get any error msg.. It just hangs VB6 and i have to kill it. :-(

Anyone who can see what I'm doing wrong? Or links to something that works? :)

Anyway, I'll keep on trying..
Cheers

Machine code Rocks:)
 
Why does it need to populate repeatedly?
How do you populate it?

I was standing in the park, wondering why frisbees got bigger as they came closer... then it hit me!
 
Hi,

Your delay function is working ok. I ran my own little test of it using a label on a form which is updated every X seconds. The only problem i had was that when I closed the form it would re-open again when the function ListSager was called again after completion of the delay routine.

If this is the same problem that you have then the following code shows a work around using a variable that has it's value changed in the Form_Unload()section of the code.

Code:
Option Explicit
Public CallAgain As Boolean  ' Variable to help with the closing down of the form

Private Sub Form_Load()
Form1.Show
CallAgain = True ' Need to call the routine again
ListSager

End Sub
Public Sub Delay(lngSeconds As Long)
  Dim lngStart As Long
  Dim intstart As Integer
  lngStart = Timer

  Do While Timer <= lngStart + lngSeconds
    DoEvents
    If CallAgain = False Then
        Exit Do ' Form has been closed so exit the delay loop
    End If
  Loop
End Sub

Sub ListSager()
Static i As Integer
Label1.Caption = "Counter value is: " & i
i = i + 1
Delay (5)
If CallAgain = True Then    ' Only call sub again if form not been closed
  ListSager
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
CallAgain = False
End Sub

This now allows the form to close gracefully and stay closed.

Another less graceful (and not too highly recommended) way to acheive this is instead of using the variable, have an End statement in your Form_Unload() code
i.e.
Code:
Private Sub Form_Unload(Cancel As Integer)
End
End Sub

HTH,

Dave
 
tb :
It needs to be continuosly populated, because, it's a monitoring tool, which looks in a db, if any new posts has come, If any then repopulate listbox.

I'm populating it using, in test, access with ADO. Will be going on to MSSQL when the system is ready.
All the way through, it's a system which moniters Backup, Mail, Safety, and various other things, on remote computers..
WHen something happens, the server will send an email to me, and an Event sink in Exchange, will fill the data in the database. All in all.

Digsy :
Thnx for the code, at first when i inserted it, the program wouldn't show.. It didn't hang this time, but nothing showed.. So i tried with a Me.Show in Form_Load, Then it showed...
But now I have to push the close button 7 times hehe..

This is the full code :
[tt]
Public CallAgain As Boolean

Public Sub Delay(lngSeconds As Long)
Dim lngStart As Long
Dim intstart As Integer
lngStart = Timer

Do While Timer <= lngStart + lngSeconds
DoEvents
If CallAgain = False Then
Exit Do
End If
Loop
End Sub

Sub ListSager()
Dim i
Dim LBHS As New CLBHScroll
Dim MyConn As New ADODB.Connection
Dim RSSag As New ADODB.Recordset
Dim RSAntal As New ADODB.Recordset
Dim ConnString As String

ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\DB\CS-SupportDB.mdb;User Id=admin;Password=;"

MyConn.ConnectionString = ConnString
MyConn.Open
' #### See if any new posts in db '
SQLFindAntalSagerQuery = "SELECT COUNT(*) As SagsAntal FROM SagsTB WHERE Afviklet = false"
Set RSAntal = MyConn.Execute(SQLFindAntalSagerQuery)

If RSAntal("SagsAntal") > lblSagsAntal.Caption Then
SQLFindSagsInfoQuery = "SELECT Sag.SagsId, Sag.Dato, Sup.SupportType, MedArb.Navne, Sag.ProblemBeskrivelse FROM SagsTB Sag, SupportTypeTB Sup, MedarbejderTB MedArb WHERE Sag.SupportId = Sup.SupportId AND Sag.MedarbejderId = MedArb.MedarbejderId AND Sag.Afviklet = false"
Set RSSag = MyConn.Execute(SQLFindSagsInfoQuery)

' #### LBHS is a listbox interface, which puts on scroll bars if nessecary '
LBHS.Init lstSagsList

Do While Not (RSSag.BOF Or RSSag.EOF)
LBHS.AddItemTab RSSag("SagsId") & _
vbTab & RSSag("Dato") & _
vbTab & RSSag("SupportType") & _
vbTab & RSSag("Navne") & _
vbTab & RSSag("ProblemBeskrivelse")
RSSag.MoveNext
Loop
lblSagsAntal.Caption = RSAntal("SagsAntal")
End If

MyConn.Close
Set MyConn = Nothing

Delay (5)
If CallAgain = True Then
ListSager
End If
End Sub

Private Sub Form_Load()
ReDim TabStops(3) As Long

' #### Make tabsstops in listbox '
TabStops(0) = 40
TabStops(1) = 125
TabStops(2) = 185
TabStops(3) = 240

Call SendMessage(lstSagsList.hwnd, LB_SETTABSTOPS, 4&, TabStops(0))
Me.Show
CallAgain = True
ListSager
End Sub

Private Sub Form_Unload(Cancel As Integer)
CallAgain = False
End Sub

Private Sub Form_Hide()
CallAgain = False
End Sub
[/tt]

I will try to find out why it's 7 times, i have to click..?
But if anyone can see it please let me know.. Thnx by the way, for the quick response. :)

Machine code Rocks:)
 
Just another question ... sorry I keep on asking instead of helping :)

Why do you loop through the recordset and use the .additem method? and not binding it to the recordset through the .DataSource property?

Which IMHO would prove to be faster ...




I was standing in the park, wondering why frisbees got bigger as they came closer... then it hit me!
 
Hi again.. I tried to put some msgbox's to see what CallAgain was. I put one in my main, and in my ListSager.
Only thing I kan see, is that when i press close, it shows the form again, making CallAgain true again.. Why. It's 7 times.. I can't see yes... But ill keep on debugging.


Machine code Rocks:)
 
tb: I didn't knew that I could do that.. Ill look into that some time.. :) Anything that makes it faster or better is usefull thnx.
But then again.. Maybe there will be some problems, because, Im using a Class Module, which has the AddItem. It's not, the listbox directly I'm populating, but The LBHS Class's AddItem.

The LBHS.AddItem, will, make the listbox scroll if nessecary.


Machine code Rocks:)
 
Is it always 7 times or does the number change?

Have you got more than 1 form running?

Also just for 'fun' try putting the End statement into Form_Unload() aswell.

Private Sub Form_Unload(Cancel As Integer)
CallAgain = False
End
End Sub


Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top