Krish212124
Krish212124

Reputation: 65

MS Access form to edit specific record from a form by providing input through text box

Can someone please help me on this find specific record Edit through MS access Forms

I have Frmfind form where I have one filed "ticket#" is a input text box another filed was button "find"

When I enter ticket# which is primary key for my table. I need get the the specific ticket# record should be opened in FormEdit mode using VBA code...

So I have another form "frmEdit" of specific record which has to be called from frmfind -> specific input..

note: Ticket# is column in my table whcih it is primary to have the ticket#.

Code:

Option Compare Database

Private Sub find_Click()

If IsNull(Me.Text79) Or Me.Text79 = "" Then
            MsgBox "You must enter a Ticket #", vbOKOnly, "Required Data"
            Me.Text79.SetFocus
        Exit Sub
    End If

If [Ticket#] = Me.Text79.Value Then


MsgBox "Record found"

DoCmd.Close
DoCmd.OpenForm "frmEdit"

Else


MsgBox "not matching record"
Me.Text79.SetFocus

End If

End Sub

Private Sub Form_Open(cancel As Integer)
'On open set focus to text box
Me.Text79.SetFocus
End Sub

Upvotes: 1

Views: 5445

Answers (1)

Albert D. Kallal
Albert D. Kallal

Reputation: 49329

You can use this code:

Private Sub Command112_Click()

   Me.txtSeach.SetFocus
   On Error Resume Next
   DoCmd.OpenForm "frmEdit", , , "TicketNumber = '" & Nz(Me.text79, "") & "'"

End Sub

Note in above if TicketNumber is in fact a number column and not text, then remove the single quotes and use this:

   DoCmd.OpenForm "aa", , , "TicketNumber = " & Nz(Me.text79, "")

Then for your message, just place that code in the forms on-open event that has a cancel:

eg:

Private Sub Form_Open(Cancel As Integer)

   If IsNull(Me!TicketNumberID) Then
      MsgBox "Please enter a valid Ticket #", vbOKOnly, "Required Data"
      Cancel = True
   End If

End Sub

The above assumes your search column is ticket number. You can also use dlookup(), or even dcount(). I think above is less code, but:

Dim strWhere         As String

strWhere = "TicketNumber = " & Me.text79

If DCount("*", "tblBookings", strWhere) > 0 Then
   code for Record found goes here
Else
   code reocrd not found code here
End If

So either way should suffice here.

Upvotes: 1

Related Questions