Access - Busqueda erronea

 
Vista:

Busqueda erronea

Publicado por jes (11 intervenciones) el 05/10/2004 08:41:14
En que falla este código si hace una busqueda entre 2 números (100-2004 y 300-2004) y el reusltado es 169 y no el correcto (200).
GRacias.
Private Sub Comando35_Click()
Dim BASE As Database
Dim REC, REC1 As Recordset
Dim NIINICIAL, nfinal As String
Dim t, Tinicio, tfinal As Long

Set BASE = CurrentDb
Set REC = BASE.OpenRecordset("datos")
Set REC1 = BASE.OpenRecordset("TABLAPARAINFORME")

RII.SetFocus
NIINICIAL = RII.Text
RFF.SetFocus
nfinal = RFF.Text
Tinicio = 0
tfinal = 0
t = 0

While Not REC1.EOF
REC1.Delete
REC1.MoveNext
Wend

While Not REC.EOF

If REC.Fields(1).Value = NIINICIAL Then
Tinicio = REC.Fields(0).Value

End If

If REC.Fields(1).Value = nfinal Then
tfinal = REC.Fields(0).Value

End If

REC.MoveNext
Wend

REC.MoveFirst

While REC.Fields(0).Value < Tinicio
REC.MoveNext
Wend

While tfinal > REC.Fields(0).Value

t = t + 1

REC1.AddNew
REC1.Fields(0).Value = REC.Fields(0).Value
REC1.Fields(1).Value = REC.Fields(1).Value
REC1.Fields(2).Value = REC.Fields(2).Value
REC1.Fields(3).Value = REC.Fields(3).Value
REC1.Fields(4).Value = REC.Fields(4).Value
REC1.Fields(5).Value = REC.Fields(5).Value
REC1.Update
REC.MoveNext

Wend
REC.Close
REC1.Close
END SUB
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Alejandro

Busqueda erronea

Publicado por Alejandro (4142 intervenciones) el 12/05/2023 00:39:02
El problema en tu código está en la lógica de los bucles `While` al buscar los valores de `Tinicio` y `tfinal`. El problema se produce porque estás moviendo el cursor del registro antes de comprobar si el valor coincide con el número buscado.

Aquí tienes una versión corregida del código:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Private Sub Comando35_Click()
    Dim BASE As Database
    Dim REC, REC1 As Recordset
    Dim NIINICIAL, nfinal As String
    Dim t, Tinicio, tfinal As Long
 
    Set BASE = CurrentDb
    Set REC = BASE.OpenRecordset("datos")
    Set REC1 = BASE.OpenRecordset("TABLAPARAINFORME")
 
    RII.SetFocus
    NIINICIAL = RII.Text
    RFF.SetFocus
    nfinal = RFF.Text
    Tinicio = 0
    tfinal = 0
    t = 0
 
    REC.MoveFirst
    While Not REC.EOF
        If REC.Fields(1).Value = NIINICIAL Then
            Tinicio = REC.Fields(0).Value
        End If
 
        If REC.Fields(1).Value = nfinal Then
            tfinal = REC.Fields(0).Value
        End If
 
        If Tinicio <> 0 And tfinal <> 0 Then
            Exit While
        End If
 
        REC.MoveNext
    Wend
 
    REC.MoveFirst
    While Not REC.EOF
        If REC.Fields(0).Value >= Tinicio And REC.Fields(0).Value <= tfinal Then
            REC1.AddNew
            REC1.Fields(0).Value = REC.Fields(0).Value
            REC1.Fields(1).Value = REC.Fields(1).Value
            REC1.Fields(2).Value = REC.Fields(2).Value
            REC1.Fields(3).Value = REC.Fields(3).Value
            REC1.Fields(4).Value = REC.Fields(4).Value
            REC1.Fields(5).Value = REC.Fields(5).Value
            REC1.Update
        End If
 
        REC.MoveNext
    Wend
 
    REC.Close
    REC1.Close
End Sub

En esta versión corregida, se mueve el cursor del registro al inicio del bucle antes de verificar si el valor coincide con `NIINICIAL` y `nfinal`. Además, se agrega una condición de salida en caso de que se encuentren ambos valores para evitar que el bucle continúe innecesariamente.

Espero que esto resuelva tu problema.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar