Monday, July 25, 2011

Display records information based on selected item from ComboBox in Access

Option Compare Database

Private Sub Form_Current()
    If Me.NewRecord Then
        Me.NameofTable = ""
    Else
        Me.NameofTable.SetFocus
        Me.NameofTable.Requery
        Me.NameofTable = Me.NameofTable.ItemData(Forms(0).CurrentRecord - 1)
    End If
End Sub

Private Sub NameofTable_Click()
    ' at this point, cbx text = cbx value
    If Me.NewRecord Or Me.NameofTable.ListIndex = -1 Then
         Me.nameoftabledum = Trim(Me.NameofTable)
         DoCmd.RunCommand acCmdSaveRecord
     Else
         If Me.NameofTable.ListIndex <> Forms(0).CurrentRecord - 1 Then
             DoCmd.GoToRecord , , acGoTo, Me.NameofTable.ListIndex + 1
         End If
    End If
End Sub

Private Sub NameofTable_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 46 Then
        If Me.NameofTable.SelLength < Len(Me.NameofTable.Value) Then
            ' delete part of the text
        Else   ' delete record
            Dim m As Integer
            m = Forms(0).CurrentRecord
            DoCmd.RunCommand acCmdDeleteRecord
            Me.NameofTable.Requery
            Forms(0).Requery
            If m > 1 Then m = m - 1
            DoCmd.GoToRecord , , acGoTo, m
         End If
    End If
End Sub


No comments: