How to Get a Button to Open a Form with Filled-In Information

accessnoobhelp

New member
Local time
Today, 18:11
Joined
Jan 9, 2018
Messages
7
Hello, this is my first post so I hope I don't break any rules. I do not have a tech background, but I am using an Access database and I need help. I bought a database that was already set up with access to the full source code. It is for keeping client records at a small nonprofit.
There is first a form for a list of all persons, which lists their names and a "person ID" that is assigned (via autocount) when I create a new client.
When I create a new client, it opens up a blank "Person" form, which is for the "Person" table. There is no issue with this form.
However, when I am looking at the "persons" form, I can press "Detail" beside the person ID. It should bring up the same form for a new client, except that it pulls the filled-in form for this person. They are connected with the person ID.
I moved some things around in both of those forms, and deleted some unnecessary fields, but didn't mess with any code at all. I very stupidly did not save a backup, and due to the grief this issue is causing me, will never make that mistake again.
However, after editing these forms, the "detail" button is broken (for lack of a better term). Instead of pulling up the "person" form with the specific person ID and their filled-in form, it brings up a blank form.
This is the VBA code for the cmdDetail button:
Private Sub cmdDetail_Click()
On Error GoTo Err_cmdDetail_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Person"

stLinkCriteria = "[Person ID]=" & Me![Person ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmdDetail_Click:
Exit Sub

Err_cmdDetail_Click:
MsgBox Err.Description
Resume Exit_cmdDetail_Click

End Sub
---
Is there something wrong with this code? I have tried to create a new button and it does the same thing. I admittedly have little idea what I'm doing, but I follow directions well and can answer more questions that help someone give me a direction to go in. Thanks in advance for any help.
 
The code looks fine. It assumes the ID field has a numeric data type; does it? You may also need to force a save, if the new record hasn't been committed yet:

If Me.Dirty Then Me.Dirty = False
 
I'm not sure if this is how I find that out, but I looked at the Person table in design view, and Person ID's data type is AutoNumber. Is that okay, or does that need to be changed?
These records have been saved. I used the database for a couple of months before making any changes to the forms that messed up the detail button, so I have quite a few saved records. (also I accidentally posted this as a reply to my thread, my bad)
 
Autonumber is fine, it's a numeric type (Long Integer). Can you attach the db here?
 
Immediately before opening another form or report, you should save the current record if it is dirty.

Code:
If Me.Dirty then
    DoCmd.RunCommand acCmdSaveRecord
End If

If your form is still not opening to the correct record, check the form properties and make sure that the "Data Entry" property on the Data tab is set to No. When this property is set to Yes, the form always opens to the "new" record and will be empty.
 
I can't do that, as it has confidential data on it. I can provide screen shots of other parts of the database if you'd like.
 
I'm not sure what you mean by "if it is dirty." I have all my current records saved. I do not actually enter in any data prior to pressing "detail" so I don't know that there is anything to save in that form.
I did check the data entry property, and it is set to No.
 
The If statement checks to see if the form is dirty. If it is, the data is saved. If no updates were done prior to this point that have not already been saved, nothing happens. If the form is updateable, this is a safety precaution that doesn't cost you anything and could save you from working with incorrect data.

Since there is nothing obvious wrong with your code, we may not be able to solve the problem without the actual database. See if you can extract just the relevant pieces with test data.
 
So I went to make a test database, and the file size is too large to upload. Any tips for how to make it smaller to be able to post?
 
Generally if you do a compact/repair within Access and then zip using Windows, it's small enough to attach.
 
Oh, for some reason I thought he meant the "Persons" form, where the detail button is located. I changed the data entry to no on the person form and it worked! I can access my data when I press detail again. Thank you! Sorry my misunderstanding made for more problems. But thank you again!!
 
No problem, it can certainly get confusing.
 

Users who are viewing this thread

Back
Top Bottom