Joym123
asked on
Using FindFirst Method with a Data Control Recordset not moving to found record
Hi,
I have a strange problem I hope somebody can help me with.
I am using VB6 (SP5) with Access XP.
I say this is a strange problem because everything works fine if run from within VB , but when I compile it to an exe the part of the functionality does not work.
I have a form (frmAddFields) with a control array of textboxes and command buttons bound to a data Control and another form called frmFind that I am using to find an item in the first form and then move to that record (in frmAddFields). There is also a class called DataClass that manages the data control. ( I hope that I am explaining this well enough, please ask if you need clarification)
As mentioned this works fine when run from within VB6, I click on the find button select the value I am looking for and I get a message box saying the value was retrieved and am at that record in frmAddFields.
When I run the exe though, I get the message box saying the record was retrieved but the field displayed in frmAddFields is always the first record in the table.
The code below is the portion to handle cmdFind
Case cmdFind 'find a specified record
Dim iReturn As Integer
Dim iVal As Integer
gFindString = ""
With frmFind
.addCaption = mvarFindCaption
.recordSource = mvarFindRecordSource 'used to fill listbox of values in frmFind
.Show vbModal
End With
If Len(gFindString) > 0 Then
Dim sResult As String
With mvardataCtl.Recordset
sResult = BuildCriteria()
If Len(sResult) > 0 Then
.FindFirst sResult
If .NoMatch Then
iReturn = MsgBox(" ResItem " & gFindString & _
" was not found.", vbCritical)
Else
iReturn = MsgBox(" ResItem " & gFindString & _
" was retrieved.", vbInformation)
End If
End If
End With
End If
call updateButtons
The updateButtons function just enables or disables buttons and locks the textFields in the frmAddFields.
Any help would be greatly appreciated , I just can't seem to work it out. I am new to using data controls and classes in VB.
Thanks in advance
Joy
I have a strange problem I hope somebody can help me with.
I am using VB6 (SP5) with Access XP.
I say this is a strange problem because everything works fine if run from within VB , but when I compile it to an exe the part of the functionality does not work.
I have a form (frmAddFields) with a control array of textboxes and command buttons bound to a data Control and another form called frmFind that I am using to find an item in the first form and then move to that record (in frmAddFields). There is also a class called DataClass that manages the data control. ( I hope that I am explaining this well enough, please ask if you need clarification)
As mentioned this works fine when run from within VB6, I click on the find button select the value I am looking for and I get a message box saying the value was retrieved and am at that record in frmAddFields.
When I run the exe though, I get the message box saying the record was retrieved but the field displayed in frmAddFields is always the first record in the table.
The code below is the portion to handle cmdFind
Case cmdFind 'find a specified record
Dim iReturn As Integer
Dim iVal As Integer
gFindString = ""
With frmFind
.addCaption = mvarFindCaption
.recordSource = mvarFindRecordSource 'used to fill listbox of values in frmFind
.Show vbModal
End With
If Len(gFindString) > 0 Then
Dim sResult As String
With mvardataCtl.Recordset
sResult = BuildCriteria()
If Len(sResult) > 0 Then
.FindFirst sResult
If .NoMatch Then
iReturn = MsgBox(" ResItem " & gFindString & _
" was not found.", vbCritical)
Else
iReturn = MsgBox(" ResItem " & gFindString & _
" was retrieved.", vbInformation)
End If
End If
End With
End If
call updateButtons
The updateButtons function just enables or disables buttons and locks the textFields in the frmAddFields.
Any help would be greatly appreciated , I just can't seem to work it out. I am new to using data controls and classes in VB.
Thanks in advance
Joy
ASKER
hi anv,
BuildCriteria just returns the string for findFirst to search for. It checks the field type and returns a string appropriate for that field type.
This all works fine if run from visual studio. But when I make an exe I only get the message box saying the string was retrieved without actually displaying the found record in the text Fields.
I have pretty much given up on finding a solution and am in the process of changing the code to do all this without a data control, just using a recordset.
But if anyone knows what the problem is I would still like to know!
This is the function buildCriteria
Private Function BuildCriteria() As String
Dim sCriteria As String, sMessage As String
Dim fField As dao.Field
Dim iIndx As Integer
Set fField = mvardataCtl.Recordset.fiel ds(mvarFin dMatchFiel d)
Select Case fField.Type
Case dbInteger, dbLong, dbCurrency, dbSingle, dbDouble
BuildCriteria = "" & mvarFindMatchField & " = " & gFindString
Case dbDate
BuildCriteria = "" & mvarFindMatchField & " = #" & gFindString & "#"
Case dbText
BuildCriteria = "" & mvarFindMatchField & " = '" & gFindString & "'"
Case Else
sMessage = "Sorry, you can't use the find feature on fields"
sMessage = sMessage & " of type : " & fField.Type
iIndx = MsgBox(sMessage, vbCritical, App.EXEName)
End Select
End Function
Cheers
Joy
BuildCriteria just returns the string for findFirst to search for. It checks the field type and returns a string appropriate for that field type.
This all works fine if run from visual studio. But when I make an exe I only get the message box saying the string was retrieved without actually displaying the found record in the text Fields.
I have pretty much given up on finding a solution and am in the process of changing the code to do all this without a data control, just using a recordset.
But if anyone knows what the problem is I would still like to know!
This is the function buildCriteria
Private Function BuildCriteria() As String
Dim sCriteria As String, sMessage As String
Dim fField As dao.Field
Dim iIndx As Integer
Set fField = mvardataCtl.Recordset.fiel
Select Case fField.Type
Case dbInteger, dbLong, dbCurrency, dbSingle, dbDouble
BuildCriteria = "" & mvarFindMatchField & " = " & gFindString
Case dbDate
BuildCriteria = "" & mvarFindMatchField & " = #" & gFindString & "#"
Case dbText
BuildCriteria = "" & mvarFindMatchField & " = '" & gFindString & "'"
Case Else
sMessage = "Sorry, you can't use the find feature on fields"
sMessage = sMessage & " of type : " & fField.Type
iIndx = MsgBox(sMessage, vbCritical, App.EXEName)
End Select
End Function
Cheers
Joy
ASKER
Hi,
I have solved my problem. It had nothing to do with the data Control or any of the posted code.
In case anyone else makes such a stupid mistake! The problem was I had code for setting the data control in the form activate. So when the form where the record was supposed to be shown from the find form became active again the activate event was fired and the data control pointed to the first record again.
I have solved my problem. It had nothing to do with the data Control or any of the posted code.
In case anyone else makes such a stupid mistake! The problem was I had code for setting the data control in the form activate. So when the form where the record was supposed to be shown from the find form became active again the activate event was fired and the data control pointed to the first record again.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sResult = BuildCriteria()
If Len(sResult) > 0 Then
.FindFirst sResult
If .NoMatch Then
iReturn = MsgBox(" ResItem " & gFindString & _
" was not found.", vbCritical)
Else
iReturn = MsgBox(" ResItem " & gFindString & _
" was retrieved.", vbInformation)
End If
End If
what does BuildCriteria do??