Tum formlarınızda kullanabileceğiniz bir class module'u (mesela adı:=clsMain olsun ) uygulamanıza ekleyin.
Option Explicit
Private Conn As ADODB.Connection
Private DBName As String
Private PWD As String
Enum CM_RECORDSET_STATUS_RESULT_CONSTANTS
CMRSSR_TRUE = 1
CMRSSR_FALSE = 0
End Enum
Public Property Get CN() As ADODB.Connection
Set CN = Conn
End Property
Public Property Let DatabaseName(ByVal New_DatabaseName As String)
DBName = New_DatabaseName
End Property
Public Property Get DatabaseName() As String
DatabaseName = DBName
End Property
Public Property Let Password(ByVal New_Password As String)
PWD = New_Password
End Property
Public Property Get Password() As String
Password = PWD
End Property
Public Function InitConnection() As Boolean
On Local Error GoTo eTrap
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & DBName & ";" & _
"Jet OLEDB

InitConnection = True
Exit Function
eTrap:
InitConnection = False
Debug.Print Err.Description
End Function
Private Sub Class_Initialize()
Set Conn = New ADODB.Connection
End Sub
Public Function CheckRS(cmRecordset As ADODB.Recordset) As CM_RECORDSET_STATUS_RESULT_CONSTANTS
On Error GoTo eTrap
With cmRecordset
If (Not .BOF) And (Not .EOF) Then CheckRS = CMRSSR_TRUE Else CheckRS = CMRSSR_FALSE
End With
Exit Function
eTrap:
CheckRS = CMRSSR_FALSE
End Function
Public Sub SafeCloseRS(Recordset As ADODB.Recordset)
If (Recordset.State = adStateOpen) Then Recordset.Close
Set Recordset = Nothing
End Sub
Public Function OpenRS(RecordSource As String, _
Optional CursorType As CursorTypeEnum = adOpenKeyset, _
Optional LockType As LockTypeEnum = adLockReadOnly) As ADODB.Recordset
On Error GoTo errTrap
Dim nRs As New ADODB.Recordset
nRs.Open RecordSource, Conn, CursorType, LockType
Set OpenRS = nRs
Exit Function
errTrap:
Set OpenRS = Nothing
Debug.Print Err.Description
End Function
Uygulamanız a bir adet modul ekleyin, projenizi Main() fonksiyonu ile başlayacak şekilde ayarladıktan sonra;
Global myData As New clsMain
Private Sub main()
With cmData
.DatabaseName = App.Path & "/vt1.mdb"
.Password = "asdfgh"
If (.InitConnection) Then
Form1.Show
Else
MsgBox "Bağlantı hatası!", vbCritical, "Hata!"
End
End If
End With
End Sub
Artık herhangi bir form icinde ;
Private Sub cmdEkle_Click()
Dim strSQL As String
strSQL = "INSERT INTO (adı, soyadı, telefon) " & _
"VALUES (" & _
"'" & Replace(txtAdı,"'","`") & "', " & _
"'" & Replace(txtSoyadı,"'","`") & "', " & _
"'" & Replace(txtPhone,"'","`") & "')"
myData.CN.Execute strSQL
End Sub
ve ya;
Private Sub Form_Load()
Dim rs As New ADODB.Recordset
Set rs = cmData.OpenRS("SELECT adı FROM tblUyeler ORDER BY adı;")
If (Not myData.CheckRS(rs) = CMRSSR_TRUE) Then Exit Sub
With rs
While (Not .EOF)
cboUyeler.AddItem .Fields("adı")
.MoveNext
Wend
End With
myData.SafeCloseRS rs
End Sub
Şeklinde kullanabilirsiniz. Boylece acık recordset nesnesi yuzunden guncelleme ve access hataları oluşmayacaktır.
__________________