Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: Binding Fields


Message #1 by "George Oro" <george@c...> on Mon, 21 Oct 2002 18:16:59 +0400
Hi Guys,

I just started connecting my database using ADODB coding, at present, I just have 3 textbox fields and two cmdButton for test with
the following code:
QUESTION:
1) Is my connection OK? otherwise what is the best?
2) Is there any easiest way to bind my fields accordingly?

Any help and idea is highly appreciated.
Cheers,
George

MY CODE
----------
Option Compare Database
Option Explicit
Dim WithEvents rsContacts As ADODB.Recordset
----------

Private Sub Form_Open(Cancel As Integer)
    'Database Connection
    Dim cn As ADODB.Connection
    Dim strCn As String

    Set cn = New ADODB.Connection
    strCn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Database\MyVBapp\Communicator ComData.mdb;Persist Security Info=False"
    cn.Open strCn

    'Recordset Connection
    'Dim rsContacts As ADODB.Recordset
    Dim strRSContacts As String

    Set rsContacts = New ADODB.Recordset
    strRSContacts = "SELECT * FROM m01TblContacts"
    rsContacts.Open strRSContacts, cn, adOpenStatic, adLockOptimistic

    BindAllFields

End Sub
----------------
Private Sub BindAllFields()
    With Me
        .txtContactsID = rsContacts("m01ContactsID")
        .txtFirstName = rsContacts("m01FirstName")
        .txtLastName = rsContacts("m01LastName")
    End With
End Sub
---------------
Private Sub cmdNext_Click()
On Error GoTo Err_cmdNext_Click


    rsContacts.MoveNext
    BindAllFields

Exit_cmdNext_Click:
    Exit Sub

Err_cmdNext_Click:
    MsgBox Err.Description
    Resume Exit_cmdNext_Click

End Sub
----------------
Private Sub cmdPrev_Click()
On Error GoTo Err_cmdPrev_Click


    rsContacts.MovePrevious
    BindAllFields

Exit_cmdPrev_Click:
    Exit Sub

Err_cmdPrev_Click:
    MsgBox Err.Description
    Resume Exit_cmdPrev_Click

End Sub

Message #2 by "Bob Bedell" <bobbedell15@m...> on Mon, 21 Oct 2002 17:14:45 +0000
Hi George,

That's how its done. Why are you using WithEvents in you declaration?
You don't need it in this module. Only recommendation would be: in order
to prevent your cursor from falling off the beginning or end of your
recordset and producing weird error messages, have your navigation
buttons check for the recordsets BOF and EOF properties. The following
code also gives the user some halpful visual cues by enabling and
disabling the appropriate navigation buttons.

Private Sub cmdPrev_Click()

    rsContacts.MovePrevious
    txtContactsID.SetFocus
    rsContacts.MovePrevious
    If rsContacts.BOF Then
        cmdPrev.Enabled = False
    Else
        cmdPrev.Enabled = True
    End If
    cmdNext.Enabled = True
    rsContacts.MoveNext

    BindAllFields

End Sub

Private Sub cmdNext_Click()
    rsContacts.MoveNext
    txtContactsID.SetFocus
    rsContacts.MoveNext
    If rsContacts.EOF Then
        cmdNext.Enabled = False
    Else
        cmdNext.Enabled = True
    End If
    rsContacts.MovePrevious
    cmdPrev.Enabled = True

    BindAllFields
End Sub

Best,

Bob

>From: "George Oro" <george@c...>
>Reply-To: "Access" <access@p...>
>To: "Access" <access@p...>
>Subject: [access] Binding Fields
>Date: Mon, 21 Oct 2002 18:16:59 +0400
>
>Hi Guys,
>
>I just started connecting my database using ADODB coding, at present, I 
>just have 3 textbox fields and two cmdButton for test with
>the following code:
>QUESTION:
>1) Is my connection OK? otherwise what is the best?
>2) Is there any easiest way to bind my fields accordingly?
>
>Any help and idea is highly appreciated.
>Cheers,
>George
>
>MY CODE
>----------
>Option Compare Database
>Option Explicit
>Dim WithEvents rsContacts As ADODB.Recordset
>----------
>
>Private Sub Form_Open(Cancel As Integer)
>     'Database Connection
>     Dim cn As ADODB.Connection
>     Dim strCn As String
>
>     Set cn = New ADODB.Connection
>     strCn = "Provider=Microsoft.Jet.OLEDB.4.0;Data 
>Source=C:\Database\MyVBapp\Communicator ComData.mdb;Persist Security 
>Info=False"
>     cn.Open strCn
>
>     'Recordset Connection
>     'Dim rsContacts As ADODB.Recordset
>     Dim strRSContacts As String
>
>     Set rsContacts = New ADODB.Recordset
>     strRSContacts = "SELECT * FROM m01TblContacts"
>     rsContacts.Open strRSContacts, cn, adOpenStatic, adLockOptimistic
>
>     BindAllFields
>
>End Sub
>----------------
>Private Sub BindAllFields()
>     With Me
>         .txtContactsID = rsContacts("m01ContactsID")
>         .txtFirstName = rsContacts("m01FirstName")
>         .txtLastName = rsContacts("m01LastName")
>     End With
>End Sub
>---------------
>Private Sub cmdNext_Click()
>On Error GoTo Err_cmdNext_Click
>
>
>     rsContacts.MoveNext
>     BindAllFields
>
>Exit_cmdNext_Click:
>     Exit Sub
>
>Err_cmdNext_Click:
>     MsgBox Err.Description
>     Resume Exit_cmdNext_Click
>
>End Sub
>----------------
>Private Sub cmdPrev_Click()
>On Error GoTo Err_cmdPrev_Click
>
>
>     rsContacts.MovePrevious
>     BindAllFields
>
>Exit_cmdPrev_Click:
>     Exit Sub
>
>Err_cmdPrev_Click:
>     MsgBox Err.Description
>     Resume Exit_cmdPrev_Click
>
>End Sub
>
>


_________________________________________________________________
Choose an Internet access plan right for you -- try MSN! 
http://resourcecenter.msn.com/access/plans/default.asp


  Return to Index