
Problem with Filter and Requery, VB6, ADO 2.0, Access97, Win95/98
Hi All,
I hope you find the time to take a look at this problem. It's a pretty
long E-mail, but I tried to provide as much information as possible.
I can't seem to get this code working. In an application I have
written I use a form, on which I bind all textboxes to a Recordset.
This recordset never has more then 20 records, but a lot of fields
(about 180).
Whenever I exucute a requery, or a filter (depending on some
diffferent situtions) I have problems. The code looks like this (The
form contains more code, but I just copied the important parts):
Option Explicit
Dim RST As ADODB.Recordset
Private Sub Form_Load()
Dim mTXT as Textbox
'Open the recordset, containing all records from C5
Set RST = New ADODB.Recordset
'***************************************************************************************
'ConDB is a aAdoDb.Connection, Using Jet 3.51, connecting to an Access
97 Database
'When opening RST, I use adOpenDynamic, because it is a multi-user
environment, where different users might add
'records at the same time
'***************************************************************************************
RST.Open "Select * From ShiftStatusHCR1_C5", ConDB, adOpenDynamic,
adLockOptimistic
'***************************************************************************************
'The form contains a collection of textboxes, aalled TXT(i)
'Cycle through this collection, and bind them all to RST. The tag of
the Textbox contains the fieldname
'***************************************************************************************
For Each mTXT In Me.TXT
Set mTXT.DataSource = RST
mTXT.DataField = mTXT.Tag
Next
'Check if there is a record for this date/shift.
'If so, fill all textboxes, comboboxes etc.
Call PerformCheck
End Sub
Private Sub PerformCheck()
'***************************************************************************************
'Here I check wheter the recordset already contains a record for this
date/time.
'If so, I want this record to be shown, if not, I want to craete a new
record
'***************************************************************************************
On error resume next
RST.Filter=""
On Error Goto 0
'I use the on error because I call this sub from several
places in this form, The first time this would generate an error
because there is no filter applied yet
RST.Requery
'refresh the recordset, because values might be chaned, or new
records might have been added
'***************************************************************************************
'Here the problems occurs. I've got 2 :o)
'The first one applies to the RST.Filter. When RST.Filter returns one
or more records, there is no problem.
'When RST.Filter returns no records (no records can be found), the
program stops responding.
'It stops responding as an .EXE, as well as when I run it from the VB6
editor
'I checked the system monitor, and my Processor is running at the full
100% when not responding, so the computer is doing something....
'The only way to stop it is CTRL-ALT-DEL.....
'Question 1: Why does it stop responding when no records are found ?
The second problem:
'When using a OpenKeySet, RST.Recordcount returns the right amount of
records (which is one).
'With the OpenDynamic cursor I always get -1. Looking at the MSDN I
understand that this means that OpenDynamic doesn't support
.Recordcount
'Question2: Do I understand the MSDN correct; Doesn't OpenDynamic
support .Recordcount ? Do I forget somethng ? Is there a way to get
the .Recordcount from an OpenDynamic cursor ?
'By the way:
'dtDate is a Date/Time picker and mShift is an integer
'***************************************************************************************
rst.Filter = "[mdate]=#" & dtDate.Value & "# And " & _
"[mshift]=" & mShift
If rst.RecordCount = 0 Then
'The record doesn't exist yet; Create it.
RST.AddNew
RST.Fields("mDate").Value = dtDate.Value
RST.Fields("mShift").Value = mShift
RST.Update
End If
End Sub
Private Sub TXT_GotFocus(Index As Integer)
'***************************************************************************************
'Select the whole text when entering a textfield
'***************************************************************************************
TXT(Index).SelStart = 0
TXT(Index).SelLength = Len(TXT(Index).Text)
End Sub
Private Sub TXT_Validate(Index As Integer, Cancel As Boolean)
'***************************************************************************************
'When Validating the entered values, I look at the .Type of the
RST.Field. Depending on the type, I reformat the entered text
'FormatNumeric is a Constant, containing "0.0"
'***************************************************************************************
Select Case RST.Fields(TXT(Index).Tag).Type
Case adSmallInt
'In case of integer, Convert to integer
If IsNumeric(TXT(Index).Text) Then
TXT(Index).Text = CInt(TXT(Index).Text)
Else
TXT(Index).Text = "0"
End If
Case adSingle
'In case of a single, Convert to single
If IsNumeric(TXT(Index).Text) Then
TXT(Index).Text = Format(CSng(TXT(Index).Text),
FormatNumeric)
Else
TXT(Index).Text = "0"
End If
Case adVarChar
'In case of a string, determine the max length
TXT(Index).Text = Left(TXT(Index).Text,
RST.Fields(TXT(Index).Tag).DefinedSize)
End Select
End Sub
This is about it........... I've tried al kind of different things,
but nothing seems to work. I found a workaround (opening a second
recordset, filter this one, get the ID back and use RST.Find "[ID]=" &
RSTTemp.Fields("ID").value
I guess you understand that this doesn't improver the
preformance........ Beside this, it's a workaround, and I've got a
feeling I missed something in the original code.
A second workaround was to close the RST, and then reopen it again,
using a Where clause in stead of a filter. Problem here is that I need
to rebind all textboxes again after reopening it. Because there are so
many textboxes (about 180), this takes an awful long time.
Thanks for taking the time to read this. Hope you can help me.
Regards,
Gerard Verbruggen