Código:
Public CNN As New ADODB.Connection
Public RST As New ADODB.Recordset
Public PRV As String, SQL1 As Variant
Function asg()
PRV = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
App.Path & "\Base.mdb" & ";Persist Security Info=False"
End Function
Function opDB()
If CNN.State = 0 Then
CNN.Open PRV
End If
End Function
Function openRS()
If CNN.State = 1 Then
RST.Open SQL1, CNN, adOpenStatic
End If
End Function
Function cerrar()
If CNN.State = 1 Then
CNN.Close
End If
End Function
Public RST As New ADODB.Recordset
Public PRV As String, SQL1 As Variant
Function asg()
PRV = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
App.Path & "\Base.mdb" & ";Persist Security Info=False"
End Function
Function opDB()
If CNN.State = 0 Then
CNN.Open PRV
End If
End Function
Function openRS()
If CNN.State = 1 Then
RST.Open SQL1, CNN, adOpenStatic
End If
End Function
Function cerrar()
If CNN.State = 1 Then
CNN.Close
End If
End Function
Otro modulo:
Código:
Public cn As ADODB.Connection
Public Function Conectar() As Boolean
Conectar = False
Dim strcon As String
strcon = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data source =" & App.Path & "\Base.mdb" ' Mi dirección personal de mi base de datos
On Error GoTo xd
Set cn = New ADODB.Connection
cn.Open strcon
Conectar = True
Exit Function
xd: MsgBox "Conexión fallida.", vbCritical, "CRITICAL ERROR FATAL"
End
End Function
Public Sub Desconectar()
On Error Resume Next
cn.Close
End Sub
Public Function Conectar() As Boolean
Conectar = False
Dim strcon As String
strcon = "Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data source =" & App.Path & "\Base.mdb" ' Mi dirección personal de mi base de datos
On Error GoTo xd
Set cn = New ADODB.Connection
cn.Open strcon
Conectar = True
Exit Function
xd: MsgBox "Conexión fallida.", vbCritical, "CRITICAL ERROR FATAL"
End
End Function
Public Sub Desconectar()
On Error Resume Next
cn.Close
End Sub
Formulario:
Código:
Dim rs As ADODB.Recordset
If Not Conectar() Then Exit Sub
Set rs = New ADODB.Recordset
rs.Open ("Articulos"), cn, adOpenKeyset, adLockOptimistic
rs.AddNew
'Base de datos / programa
rs!Codigo = txtcod
rs.Update
rs.Close
Set rs = Nothing
If Not Conectar() Then Exit Sub
Set rs = New ADODB.Recordset
rs.Open ("Articulos"), cn, adOpenKeyset, adLockOptimistic
rs.AddNew
'Base de datos / programa
rs!Codigo = txtcod
rs.Update
rs.Close
Set rs = Nothing