-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMADO.BAS
406 lines (300 loc) · 11.7 KB
/
MADO.BAS
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
Attribute VB_Name = "mAdo"
Option Explicit
'****************************
'creado por Luis Núñez Ibarra
'
'Módulo con funciones utiles de ADO
'
'URL
'http://proyectovb.topcities.com
'
'EMAIL
'proyectovb@hotmail.com
'****************************
Public DBConnection() As New ADODB.Connection
Public DBRecordset As New ADODB.Recordset
Public DBCommand As New ADODB.Command
'Abre una conexion segun un origen de datos ODBC
'definido en el panel de control ODBC
'
'True = Exito al conectar
'False = Error al conectar
Public Function AbrirConexion(ByVal Conexion As String, _
Optional ByVal Usuario As String = "", _
Optional Password As String = "") As Boolean
On Error GoTo ErrorAbrirConexion
Dim ret As Boolean
ret = True
DBConnection.ConnectionTimeout = 0 'no hay limite de espera de tiempo
If Usuario <> "" Then
DBConnection.Open Conexion, Usuario, Password
Else
DBConnection.Open Conexion
End If
GoTo SalirAbrirConexion
ErrorAbrirConexion:
ret = False
MsgBox "AbrirConexion : " & Err & " " & Error$, vbCritical
Resume SalirAbrirConexion
SalirAbrirConexion:
AbrirConexion = ret
Err = 0
End Function
'Abre una base de datos access
'Regresa :
'True = Exito al conectar
'False = Error al conectar
'
Public Function AbrirBaseDatosAccess(ByVal BaseAccess As String, _
Optional ByVal Access2000 As Boolean = False, _
Optional ByVal Usuario As String = "", _
Optional ByVal Password As String = "") As Boolean
On Error GoTo ErrorAbrirBaseDatosAccess
Dim ret As Boolean
Dim Conexion As String
ret = True
If Not Access2000 Then 'access 97
Conexion = "Provider=Microsoft.Jet.OLEDB.3.51;" _
& "Persist Security Info=False;Data Source=" & BaseAccess
Else 'access 2000
Conexion = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Persist Security Info=False;Data Source=" & BaseAccess
End If
DBConnection.ConnectionTimeout = 0 'no hay limite de espera de tiempo
If Usuario <> "" Then
DBConnection.Open Conexion, Usuario, Password
Else
DBConnection.Open Conexion
End If
GoTo SalirAbrirBaseDatosAccess
ErrorAbrirBaseDatosAccess:
ret = False
MsgBox "AbrirBaseDatosAccess : " & Err & " " & Error$, vbCritical
Resume SalirAbrirBaseDatosAccess
SalirAbrirBaseDatosAccess:
AbrirBaseDatosAccess = ret
Err = 0
End Function
'abre un adodb.recordset
'parametros :
' adors = un recordset a abrir
' sqlstmt = la cade sql con el select realizado
' modoabrir =
Public Function AbrirRecordset(ByVal AdoRs As ADODB.Recordset, ByVal SqlStmt As String, _
ModoAbrir As eOpenRecordset) As Boolean
On Error GoTo ErrorAbrirRecordset
Dim ret As Boolean
ret = True
If ModoAbrir = OPEN_SERVIDOR_TURBO Then
'Abre el cursor en modo solo avance ->
'no se puede realizar MoveFirst/MoveNext/MovePrevius/MoveLast/Move/Recordcount
'<es la forma mas rapida de abrir un cursor>.
'útil para cuando queremos por ejemplo :
'llenar una grilla, combo, lista, etc
DBRecordset.CursorLocation = adUseServer
DBRecordset.Open SqlStmt, DBConnection, adOpenForwardOnly, adLockReadOnly, adCmdText
ElseIf ModoAbrir = OPEN_CLIENTE_TURBO Then
'Abre el cursor en modo solo avance ->
'no se puede realizar MoveFirst/MoveNext/MovePrevius/MoveLast/Move/Recordcount
'este modo es un poco mas lento. pero solo cambia donde abrimos el cursor
'tambien es util para cuando queremos por ejemplo :
'llenar una grilla, combo, lista, etc
DBRecordset.CursorLocation = adUseClient
DBRecordset.Open SqlStmt, DBConnection, adOpenForwardOnly, adLockReadOnly, adCmdText
ElseIf ModoAbrir = OPEN_CLIENTE_BROWSER Then
'Abre el cursor en modo browser
'podemos realizar MoveFirst/MoveNext/MovePrevius/MoveLast/Move/Recordcount
'este modo es lento. pero solo cambia donde abrimos el cursor
DBRecordset.CursorLocation = adUseClient
DBRecordset.Open SqlStmt, DBConnection, adOpenDynamic, adLockReadOnly, adCmdText
Else
ret = False 'modo incorrecto
End If
GoTo SalirAbrirRecordset
ErrorAbrirRecordset:
ret = False
MsgBox "AbrirRecordset : " & Err & " " & Error$, vbCritical
Resume SalirAbrirRecordset
SalirAbrirRecordset:
AbrirRecordset = ret
Err = 0
End Function
'agrega parametros delete
Private Sub AgregaParametrosDelete()
Dim SqlStmt As String
SqlStmt = "DELETE "
SqlStmt = SqlStmt & "FROM AUTHORS WHERE "
SqlStmt = SqlStmt & "AU_ID = ? "
With DBCommand
.Parameters.Append .CreateParameter("AU_ID", adInteger, adParamInput, 5, 0)
.CommandText = SqlStmt
.ActiveConnection = DBConnection
.Prepared = True 'prepara nuestro cursor
End With
End Sub
'prepara el comando para ejecutar un insert
'
Public Sub AgregaParametrosInsert()
Dim SqlStmt As String
SqlStmt = "INSERT INTO "
SqlStmt = SqlStmt & "AUTHORS VALUES (? , ? , ? )"
With DBCommand
.Parameters.Append .CreateParameter("AU_ID", adInteger, adParamInput, 4, 0)
.Parameters.Append .CreateParameter("AUTHOR", adChar, adParamInput, 50, 0)
.Parameters.Append .CreateParameter("YEAR_BORN", adInteger, adParamInput, 2, 0)
.CommandText = SqlStmt
.ActiveConnection = DBConnection
.Prepared = True 'prepara nuestro cursor
End With
End Sub
'agrega los parametros para realizar un select a la tabla authors en biblio.mdb
Private Sub AgregaParametrosSelectAuthors()
Dim SqlStmt As String
SqlStmt = "SELECT "
SqlStmt = SqlStmt & "* FROM AUTHORS WHERE "
SqlStmt = SqlStmt & "AU_ID = ? "
With DBCommand
.Parameters.Append .CreateParameter("AU_ID", adInteger, adParamInput, 5, 0)
.CommandText = SqlStmt
.ActiveConnection = DBConnection
.Prepared = True 'prepara nuestro cursor
End With
End Sub
'configura los parametros para ejecutar un update en tabla authors
Private Sub AgregaParametrosUpdate()
Dim SqlStmt As String
SqlStmt = "UPDATE "
SqlStmt = SqlStmt & " AUTHORS "
SqlStmt = SqlStmt & " SET "
SqlStmt = SqlStmt & " AUTHOR = ? , "
SqlStmt = SqlStmt & " YEAR_BORN = ? "
SqlStmt = SqlStmt & " WHERE "
SqlStmt = SqlStmt & " AU_ID = ? "
With DBCommand
.Parameters.Append .CreateParameter("AUTHOR", adChar, adParamInput, 50, "")
.Parameters.Append .CreateParameter("YEAR_BORN", adInteger, adParamInput, 4, 0)
.Parameters.Append .CreateParameter("AU_ID", adInteger, adParamInput, 4, 0)
.CommandText = SqlStmt
.ActiveConnection = DBConnection
.Prepared = True 'prepara nuestro cursor
End With
End Sub
'ejecuta el delete en tabla authors
Private Sub EjecutaDeleteAuthors()
Dim k As Integer
Dim Arr_Authors() As eAuthors
ReDim Preserve Arr_Authors(3)
Arr_Authors(1).AU_ID = 1
Arr_Authors(2).AU_ID = 2
Arr_Authors(3).AU_ID = 3
'RAPIDISIMO >:-> !
For k = 1 To UBound(Arr_Authors)
With DBCommand
.Parameters("AU_ID") = Arr_Authors(k).AU_ID
Set DBRecordset = Nothing
Set DBRecordset = .Execute
End With
Next k
'LENTO >:-< !!!
For k = 1 To UBound(Arr_Authors)
SqlStmt = "DELETE FROM AUTHORS "
SqlStmt = SqlStmt & " WHERE "
SqlStmt = SqlStmt & " AU_ID = " & Arr_Authors(k).AU_ID
DBConnection.Execute SqlStmt
Next k
End Sub
'ejecuta insert en authors
'
Private Sub EjecutaInsertAuthors()
Dim k As Integer
Dim Arr_Authors() As eAuthors
ReDim Preserve Arr_Authors(3)
Arr_Authors(1).AU_ID = 1
Arr_Authors(1).AUTHOR = "MIGUEL DE CERVANTES"
Arr_Authors(1).YEAR_BORN = 1860
Arr_Authors(2).AU_ID = 2
Arr_Authors(2).AUTHOR = "STEPHEN KING"
Arr_Authors(2).YEAR_BORN = 1940
Arr_Authors(3).AU_ID = 3
Arr_Authors(3).AUTHOR = "ISABEL ALLENDE"
Arr_Authors(3).YEAR_BORN = 1953
'RAPIDISIMO >:-> !
For k = 1 To UBound(Arr_Authors)
With DBCommand
.Parameters("AU_ID") = Arr_Authors(k).AU_ID
.Parameters("AUTHOR") = Arr_Authors(k).AUTHOR
.Parameters("YEAR_BORN") = Arr_Authors(k).YEAR_BORN
Set DBRecordset = Nothing
Set DBRecordset = .Execute
End With
Next k
'LENTO >:-< !!!
For k = 1 To UBound(Arr_Authors)
SqlStmt = "INSERT INTO AUTHORS VALUES "
SqlStmt = SqlStmt & Arr_Authors(k).AU_ID & " , "
SqlStmt = SqlStmt & "'" & Arr_Authors(k).AUTHOR & "' , "
SqlStmt = SqlStmt & Arr_Authors(k).YEAR_BORN & ")"
DBConnection.Execute SqlStmt
Next k
End Sub
'ejecuta el select de consulta en tabla authors
Private Sub EjecutaSelectAuthors()
'se configura el comando pero el ejecute debe establecerse
'a un recordset
'lo ideal es preparar el o los comandos globales una vez en el programa
'y limpiarlos al finalizarlo.
'
'si el comando va a ser usado en un formulario preparar parametros
'al inicio y luego limpiarlos en el unload
With DBCommand
.Parameters("AU_ID") = 1000
Set DBRecordset = Nothing
Set DBRecordset = .Execute
End With
With DBRecordset
Do While Not .EOF
'.........
.MoveNext
Loop
End With
DBRecordset.Close
End Sub
'ejecuta el update en tabla authors
Private Sub EjecutaUpdateAuthors()
Dim k As Integer
Dim Arr_Authors() As eAuthors
ReDim Preserve Arr_Authors(3)
Arr_Authors(1).AU_ID = 1
Arr_Authors(1).AUTHOR = "MIGUEL DE CERVANTES Y SAAVEDRA"
Arr_Authors(1).YEAR_BORN = 1860
Arr_Authors(2).AU_ID = 2
Arr_Authors(2).AUTHOR = "STEPHEN KING PEREZ"
Arr_Authors(2).YEAR_BORN = 1940
Arr_Authors(3).AU_ID = 3
Arr_Authors(3).AUTHOR = "ISABEL ALLENDE GOCE"
Arr_Authors(3).YEAR_BORN = 1953
'RAPIDISIMO >:-> !
For k = 1 To UBound(Arr_Authors)
With DBCommand
.Parameters("AU_ID") = Arr_Authors(k).AU_ID
.Parameters("AUTHOR") = Arr_Authors(k).AUTHOR
.Parameters("YEAR_BORN") = Arr_Authors(k).YEAR_BORN
Set DBRecordset = Nothing
Set DBRecordset = .Execute
End With
Next k
'LENTO >:-< !!!
For k = 1 To UBound(Arr_Authors)
SqlStmt = "UPDATE AUTHORS SET "
SqlStmt = SqlStmt & " AUTHOR = '" & Arr_Authors(k).AUTHOR & "' , "
SqlStmt = SqlStmt & " YEAR_BORN = " & Arr_Authors(k).YEAR_BORN
SqlStmt = SqlStmt & " WHERE "
SqlStmt = SqlStmt & " AU_ID = " & Arr_Authors(k).AU_ID
DBConnection.Execute SqlStmt
Next k
End Sub
'limpiar comando de memoria
Private Sub LimpiarComando()
DBCommand.Prepared = False
Set DBCommand = Nothing
End Sub