Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2014
    Posts
    17

    Unanswered: VBA Code - Opening a form and finding a specific record.

    Hello,

    I have the following VBA code which opens up my form and allows me to search for a specfic surname.

    Private Sub Command4_Click()

    DoCmd.OpenForm "frm_Candidate", , , "surname = me.lbl_surname"

    End Sub


    However when the input box appears to search for the surname it says "me.lbl_surname".

    How do i change this to 'Enter surname' or something similiar. The actual code works and does what it wants, I just want it to look a bit cleaner.

    Thanks for your help in advance!

  2. #2
    Join Date
    Mar 2014
    Posts
    17
    Also what is the code to cancel the command as at the moment if i press cancel it goes to the VBA code to debug?

  3. #3
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    It should be:
    Code:
    DoCmd.OpenForm "frm_Candidate", , , "surname = '" & Me.lbl_surname & "'"
    Provided that lbl_surname is the name of a control on the current form that contains the surname to be searched.
    Have a nice day!

  4. #4
    Join Date
    Mar 2014
    Posts
    17
    Thats great thanks! Works perfectly one more thing though please, When I enter in a surname and then go back to the form it is still there? Is there a way to delete it once it has been searched for?

    Hope that makes sense!

  5. #5
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    If lbl_surname is a label, you can use:
    Code:
    DoCmd.OpenForm "frm_Candidate", , , "surname = '" & Me.lbl_surname & "'"
    Me.lbl_surname.Caption = ""
    If its a textbox, use:
    Code:
    Me.lbl_surname.Value = ""
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •