Visual Basic - Error Al buscar Campos iguales

Life is soft - evento anual de software empresarial
 
Vista:
Imágen de perfil de jos

Error Al buscar Campos iguales

Publicado por jos (10 intervenciones) el 11/07/2015 14:53:49
El error es el siguiente construi un codigo que permite buscar si un campo el "clave" esta en la base de datos, si existe pues el programa debe mostrar un mensaje de error, y ya, pero si no existe que continue con las siguientes condiciones, hasta guardar el nuevo registro.
los frmmaxl son mensajes de error
los verr son el numero del error
el campo clave es txtcd.text o varcon, probe con ambos y siempre me sale el siguiente error
Siempre, pero siempre me dice que existe el registro aun cuando en realidad no existe.
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
MMControl1.Command = "Open"
MMControl1.Command = "Play"
varcon = txtcd.Text
If (txtcd.Text <> "") Then
    If (txtcd.Text <> Adodc1.Recordset.Fields("cedest").Value) Then
        If (txtnb1.Text <> "") Then
            If (txtap1.Text <> "") Then
                If (cbomat.Text <> "") Then
                        If (txtc.Text <> "") Then
                            If (txtcc.Text <> "") Then
                                If (txtc.Text = txtcc.Text) Then
                                    If conn >= 1 Then
                                        Adodc8.Refresh
                                        Adodc8.Recordset.Find "codasi='" & "a2" & "'"
                                        If Adodc8.Recordset.EOF = False Then
                                            Adodc8.Recordset.AddNew
                                            Adodc8.Recordset.Fields("cedest").Value = txtcd.Text
                                            Adodc8.Recordset.Fields("nom1").Value = txtnb1.Text
                                            Adodc8.Recordset.Fields("ape1").Value = txtap1.Text
                                            Adodc8.Recordset.Fields("codasi").Value = a2
                                            Adodc8.Recordset.Fields("codsec").Value = a
                                            Adodc8.Recordset.Update
                                        Else
 
                                        End If
                                    End If
                                    If conn >= 2 Then
                                        Adodc8.Refresh
                                        Adodc8.Recordset.Find "codasi='" & "b2" & "'"
                                        If Adodc8.Recordset.EOF = False Then
                                            Adodc8.Recordset.AddNew
                                            Adodc8.Recordset.Fields("cedest").Value = txtcd.Text
                                            Adodc8.Recordset.Fields("nom1").Value = txtnb1.Text
                                            Adodc8.Recordset.Fields("ape1").Value = txtap1.Text
                                            Adodc8.Recordset.Fields("codasi").Value = b2
                                            Adodc8.Recordset.Fields("codsec").Value = b
                                            Adodc8.Recordset.Update
                                        Else
 
                                        End If
                                    End If
                                    If conn >= 3 Then
                                        Adodc8.Refresh
                                        Adodc8.Recordset.Find "codasi='" & "c2" & "'"
                                        If Adodc8.Recordset.EOF = False Then
                                            Adodc8.Recordset.AddNew
                                            Adodc8.Recordset.Fields("cedest").Value = txtcd.Text
                                            Adodc8.Recordset.Fields("nom1").Value = txtnb1.Text
                                            Adodc8.Recordset.Fields("ape1").Value = txtap1.Text
                                            Adodc8.Recordset.Fields("codasi").Value = c2
                                            Adodc8.Recordset.Fields("codsec").Value = c
                                            Adodc8.Recordset.Update
                                        Else
 
                                        End If
                                    End If
                                    If conn >= 4 Then
                                        Adodc8.Refresh
                                        Adodc8.Recordset.Find "codasi='" & "d2" & "'"
                                        If Adodc8.Recordset.EOF = False Then
                                            Adodc8.Recordset.AddNew
                                            Adodc8.Recordset.Fields("cedest").Value = txtcd.Text
                                            Adodc8.Recordset.Fields("nom1").Value = txtnb1.Text
                                            Adodc8.Recordset.Fields("ape1").Value = txtap1.Text
                                            Adodc8.Recordset.Fields("codasi").Value = d2
                                            Adodc8.Recordset.Fields("codsec").Value = d
                                            Adodc8.Recordset.Update
                                        Else
 
                                        End If
                                    End If
                                    If conn >= 5 Then
                                        Adodc8.Refresh
                                        Adodc8.Recordset.Find "codasi='" & "e2" & "'"
                                        If Adodc8.Recordset.EOF = False Then
                                            Adodc8.Recordset.AddNew
                                            Adodc8.Recordset.Fields("cedest").Value = txtcd.Text
                                            Adodc8.Recordset.Fields("nom1").Value = txtnb1.Text
                                            Adodc8.Recordset.Fields("ape1").Value = txtap1.Text
                                            Adodc8.Recordset.Fields("codasi").Value = e2
                                            Adodc8.Recordset.Fields("codsec").Value = e
                                            Adodc8.Recordset.Update
                                        Else
 
                                        End If
                                    End If
                                    If conn >= 6 Then
                                        Adodc8.Refresh
                                        Adodc8.Recordset.Find "codasi='" & "f2" & "'"
                                        If Adodc8.Recordset.EOF = False Then
                                            Adodc8.Recordset.AddNew
                                            Adodc8.Recordset.Fields("cedest").Value = txtcd.Text
                                            Adodc8.Recordset.Fields("nom1").Value = txtnb1.Text
                                            Adodc8.Recordset.Fields("ape1").Value = txtap1.Text
                                            Adodc8.Recordset.Fields("codasi").Value = f2
                                            Adodc8.Recordset.Fields("codsec").Value = f
                                            Adodc8.Recordset.Update
                                        Else
 
                                        End If
                                    End If
                                    If conn >= 7 Then
                                        Adodc8.Refresh
                                        Adodc8.Recordset.Find "codasi='" & "g2" & "'"
                                        If Adodc8.Recordset.EOF = False Then
                                            Adodc8.Recordset.AddNew
                                            Adodc8.Recordset.Fields("cedest").Value = txtcd.Text
                                            Adodc8.Recordset.Fields("nom1").Value = txtnb1.Text
                                            Adodc8.Recordset.Fields("ape1").Value = txtap1.Text
                                            Adodc8.Recordset.Fields("codasi").Value = g2
                                            Adodc8.Recordset.Fields("codsec").Value = g
                                            Adodc8.Recordset.Update
                                        Else
 
                                        End If
                                    End If
                                    If conn >= 8 Then
                                        Adodc8.Refresh
                                        Adodc8.Recordset.Find "codasi='" & "h2" & "'"
                                        If Adodc8.Recordset.EOF = False Then
                                            Adodc8.Recordset.AddNew
                                            Adodc8.Recordset.Fields("cedest").Value = txtcd.Text
                                            Adodc8.Recordset.Fields("nom1").Value = txtnb1.Text
                                            Adodc8.Recordset.Fields("ape1").Value = txtap1.Text
                                            Adodc8.Recordset.Fields("codasi").Value = h2
                                            Adodc8.Recordset.Fields("codsec").Value = h
                                            Adodc8.Recordset.Update
                                        Else
 
                                        End If
                                    End If
                                    If conn >= 9 Then
                                        Adodc8.Refresh
                                        Adodc8.Recordset.Find "codasi='" & "i2" & "'"
                                        If Adodc8.Recordset.EOF = False Then
                                            Adodc8.Recordset.AddNew
                                            Adodc8.Recordset.Fields("cedest").Value = txtcd.Text
                                            Adodc8.Recordset.Fields("nom1").Value = txtnb1.Text
                                            Adodc8.Recordset.Fields("ape1").Value = txtap1.Text
                                            Adodc8.Recordset.Fields("codasi").Value = i2
                                            Adodc8.Recordset.Fields("codsec").Value = i
                                            Adodc8.Recordset.Update
                                        Else
 
                                        End If
                                    End If
                                    If conn >= 10 Then
                                        Adodc8.Refresh
                                        Adodc8.Recordset.Find "codasi='" & "j2" & "'"
                                        If Adodc8.Recordset.EOF = False Then
                                            Adodc8.Recordset.AddNew
                                            Adodc8.Recordset.Fields("cedest").Value = txtcd.Text
                                            Adodc8.Recordset.Fields("nom1").Value = txtnb1.Text
                                            Adodc8.Recordset.Fields("ape1").Value = txtap1.Text
                                            Adodc8.Recordset.Fields("codasi").Value = j2
                                            Adodc8.Recordset.Fields("codsec").Value = j
                                            Adodc8.Recordset.Update
                                        Else
 
                                        End If
                                    End If
                                    vin = 6
                                    Adodc1.Recordset.Update
                                    Adodc1.Refresh
                                    Adodc1.Recordset.MoveLast
                                    frmmenuad.Refresh
                                    imgadda.Enabled = True
                                    txtcd.Enabled = False
                                    txtnb1.Enabled = False
                                    txtnb2.Enabled = False
                                    txtap1.Enabled = False
                                    txtap2.Enabled = False
                                    cbomat.Enabled = False
                                    lblbut.Enabled = False
                                    txtc.Enabled = False
                                    txtcc.Enabled = False
                                    txtcc.Text = ""
                                    frmact.Show
                                    imgga.Enabled = False
                                Else
                                    verr = 8
                                    frmmaxl.Show
                                End If
                            Else
                                verr = 15
                                frmmaxl.Show
                            End If
                        Else
                            verr = 14
                            frmmaxl.Show
                        End If
                Else
                    verr = 12
                    frmmaxl.Show
                End If
            Else
                verr = 11
                frmmaxl.Show
            End If
        Else
            verr = 10
            frmmaxl.Show
        End If
    Else
        verr = 7
        frmmaxl.Show
        'Adodc1.Refresh
        'Adodc4.Refresh
        'txtcd.Enabled = False
        'txtnb1.Enabled = False
        'txtnb2.Enabled = False
        'txtap1.Enabled = False
        'txtap2.Enabled = False
        'cbomat.Enabled = False
        'lblbut.Enabled = False
        'txtc.Enabled = False
        'txtcc.Enabled = False
        'imgadda.Enabled = True
        'imgga.Enabled = False
    End If
Else
    verr = 9
    frmmaxl.Show
End If
aqui Agrego un registro
ejemplo1_zpsm0ivaqpi
aqui lleno los campos del registro
ejemplo2_zpskrgfajcz
aqui preciono el boton guardar que es el del codigo que esta mas arriba del post.
y me dice que el registro existe, aun cuando eso no es cierto.
ejemplo3_zpshsk3xwii
gracias por leer, espero solucionar el problema pronto, o que ustedes me digan la solucion :p.
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 Wilfredo Patricio Castillo
Val: 8
Ha aumentado su posición en 9 puestos en Visual Basic (en relación al último mes)
Gráfica de Visual Basic

Error Al buscar Campos iguales

Publicado por Wilfredo Patricio Castillo (260 intervenciones) el 11/07/2015 19:39:30
Creo que te complicado la vida demasiado para algo tan básico.

Si lo que necesitas saber es si un campo clave está en tu tabla puedes hacerlo así:

Dim Conn as new ADODB.Connection
Dim RstBuscar as New ADODB.Recordset

'Abres la conexion

RstBuscar.Open "Select CampoClave From TuTabla Where CampoClave=TuCodigoDeTuFormulario",Conn,AdLocReadOnly,AdLocOptimistic

IfRstBuscar.RecordCount>0 then
msgbox"Código ya existe"
exit sub
Else
'aqui sigues haciendo lo que necesites
end if



Espero te ayude y te de una luz.

Saludos cordiales
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar
Imágen de perfil de jos

Error Al buscar Campos iguales

Publicado por jos (10 intervenciones) el 11/07/2015 19:41:00
gracias lo voy a probar.
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
Imágen de perfil de jos

Error Al buscar Campos iguales

Publicado por jos (10 intervenciones) el 11/07/2015 23:18:13
termine arreglando de una manera muy peculiar, hize un adodc nuevo para hacer la valizacion y ya, no es lo q me dijiste q hiciera pero me diste la idea, asi q gracias.
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