Share via

Access 2013 Command Button to Find a Record

Anonymous
2014-05-08T13:56:25+00:00

Hello,

I have an access table, tbl_Trainer, which holds data for trainers. There are two fields, trainerID and trainerName, which trainerID is the primary key.

I have created a form, named frm_Trainer to enter data to tbl_Trainer, it has the two fields from the table: txt_TrainerID and txt_Name. Entry fields are text boxes.

In the form, I have placed a command button to check if the trainerID that is entered by the user in the text box txt_trainerID already exists in the table tbl_Trainer. 

In the command button click procedure I have created a "Result" parameter and built as follows:

result = DLookup ([trainerD], Tables![tbl_Trainer], "[frm_Trainer]![txt_trainerID]=" & Tables![tbl_Trainer]![trainerID])

With this my intention is that if the result is null, Access will display the message that the ID is available, otherwise it will inform the user that the ID is already in the table.

However, the result function is not working and returning an "Object required" message. I could not figure out what is wrong. I need help with this.

To make the search and return information, I do not insist on using a procedure, if I can do this with a macro, that is fine [and much better, frankly]. If the procedure is the long way to go, can you please direct me to a source/example which can help me with this issue (find/replace/inform user)?

Thanks,

Tolga

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2014-05-09T09:24:44+00:00

Hi

if the TrainerId is a text (not numeric) change your formula with:

result = DLookup ("trainerID", "tbl_Trainer", "TrainerID = '" & ME.txt_trainerID & "'")

Mimmo

Was this answer helpful?

0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-05-09T11:20:21+00:00

    Mimmo, this works like a charm! Thank you very much!

    Scott, thank you very much for pointing me to the right direction.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-05-09T08:12:19+00:00

    Hi Scott, Mimmo, 

    Thank you very much for your replies.

    Mimmo, the equation returns 

    Run-time error '3075'

    Syntax error (missing operator) in query expression 'trainerID='.

    Scott,

    1. I'm sorry for my misuse of words, frankly I am not an Access guy and really did not think about this. I have created the form with the wizard. It should be a bound text box, so that txt_TrainerID is bound to trainerID field in the tbl_Trainer table.
    2. See above :)
    3. I have dimmed result as variant. 
    4. TrainerID is assigned manually. That's why I want to check if the manually assigned ID is already present. 

    Thanks,

    Tolga

    Was this answer helpful?

    0 comments No comments
  3. ScottGem 68,830 Reputation points Volunteer Moderator
    2014-05-08T14:54:49+00:00

    First, you don't have fields on a form, you have controls that may or may not be bound to fields in a table. This is a subtle but important distinction.

    Second, as Mimmo says you need to reverse the comparsion in your DLookup.

    Third, Did you define Result as a data type.  If the TrainingID doesn't exist, then the DLookup will return a Null so Result has to be DIMmed as a Variant to accept Nulls. 

    Finally, are Trainer's assigned IDs through another process? If not, why not just use ans autonumber?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2014-05-08T14:30:14+00:00

    Hi,

    try to change:

    result = DLookup ("trainerID", "tbl_Trainer", "TrainerID = " & ME.txt_trainerID)

    Bye Mimmo

    Was this answer helpful?

    0 comments No comments