I’ve got to tell you, Benny, I’ve gotten a headache from trying to follow all the ins and outs of what you’ve already tried in your database! So I’m going to start off with some very basic advice:
Keep It Simple! When tackling a relatively big job like this, take a pen and apiece of paper, and write down a very simple outline of the problem. In this case you need:
- SwitchBoard Form
- New Record --> Form1
- View All Records --> Form2
- Edit Records --> Form3
Next, and this is the important part, take the problems on one at a time! Don’t go harrying off in three or four different directions, working first on one form, then another, then a third, never getting any of them to work properly, and becoming mired down more and more as you go along. This particular problem isn’t, believe it or not, all that complicated!
To begin with, you don’t need four forms, you only need two! A SwitchBoard form and a form to display your records. Stop and think about it; what you’ve called Form1, Form2 and Form3 are really the same form! They all hold the same data! You simply want the form to handle that data in different ways, depending on circumstances, so that’s what you should do, have one form but make it operate in different ways, depending on your needs. I’ll give you most of what you’ll need to accomplish this task, only holding off, for now, on the question of limiting who has the ability to edit/delete records. After you get everything else up and working, you can tackle that.
While it’s true that you can control whether records can be added or edited or deleted or even viewed, thru the Properties box for a given form, you can also do this by the manner in which you open the form, and that’s what I’ll demonstrate here.
The SwitchBoard form is where you’ll navigate,
not between three almost identical forms, but rather
between the three instances of the same form. Let’s start with this form, frmSwitchBoard:
You’ll need three command buttons to open the data form in the appropriate mode,
cmdNewRecord,
cmdViewAllRecords and
cmdEditRecords.Here’s the code for these buttons.
This button will open the data form so that new records can be entered. You can add one, or more than one, new record, but you can't navigate to an existing record! Opened in this mode (
acFormAdd) the Access nav buttons will only move to a new record. After
frmNewResource is opened
frmSwitchBoard is closed.
- Private Sub cmdNewRecord_Click()
-
On Error GoTo Err_cmdNewRecord_Click
-
-
Dim stDocName As String
-
-
stDocName = "frmNewResource"
-
DoCmd.OpenForm stDocName, , , , acFormAdd
-
DoCmd.Close acForm, "frmSwitchBoard"
-
-
Exit_cmdNewRecord_Click:
-
Exit Sub
-
-
Err_cmdNewRecord_Click:
-
MsgBox Err.Description
-
Resume Exit_cmdNewRecord_Click
-
-
End Sub
This button will open the form in
ReadOnly mode. You can view all records but you can't add/edit/delete records; Access disables these options when the form is opened using
acFormReadOnly.
- Private Sub cmdViewAllRecords_Click()
-
On Error GoTo Err_cmdViewAllRecords_Click
-
-
Dim stDocName As String
-
Dim stLinkCriteria As String
-
-
stDocName = "frmNewResource"
-
DoCmd.OpenForm stDocName, , , , acFormReadOnly
-
DoCmd.Close acForm, "frmSwitchBoard"
-
-
Exit_cmdViewAllRecords_Click:
-
Exit Sub
-
-
Err_cmdViewAllRecords_Click:
-
MsgBox Err.Description
-
Resume Exit_cmdViewAllRecords_Click
-
-
End Sub
This final button allows the records to be edited or deleted. The
OpenArgs parameter ("
Edit Only") is used in this OpenForm command. When accessed in the
Form_Load event of
frmNewResource, it tells Access not to allow new records to be added and allows the combobox
cboResourceID to be visible. This dropdown box will be used to go to a given record for editing/deletion, and will only be visible when the form is opened in edit mode..
- Private Sub cmdEditRecords_Click()
-
On Error GoTo Err_cmdEditRecords_Click
-
-
Dim stDocName As String
-
Dim stLinkCriteria As String
-
-
stDocName = "frmNewResource"
-
DoCmd.OpenForm stDocName, , , , , , "Edit Only"
-
DoCmd.Close acForm, "frmSwitchBoard"
-
-
Exit_cmdEditRecords_Click:
-
Exit Sub
-
-
Err_cmdEditRecords_Click:
-
MsgBox Err.Description
-
Resume Exit_cmdEditRecords_Click
-
-
End Sub
-
Now, for your data form,
frmNewResource:
First, if you haven't already done so, create your data form
frmNewResource based on your table/query, including all the fields you want displayed.
Leave all of the form's properties at their Default settings! Don't mess around with the
AllowAdditons, AllowDeletions, AllowEdits and
Data Entry; the mode used to open the form will take care of these things. Then simply:
Add a combo box to your form. The Combobox Wizard will pop up
Select "
Find a record based on the value I selected in my combobox."
From the table/query the form is based on, click on the field you're searching by (
your ID field) to move it to the right side.
Hit Next.
Size the column appropriately.
Hit Next.
Name the combobox
cboResourceID.
Hit Finish.
Now you can drop the combobox down and scroll down to the item to search by, or you can start to enter the item, and the combobox will "autofill" as you type. Hit <
Enter> and the record will be retrieved.
This code, thru the use of
OpenArgs, tells Access to not allow new records to be added when the form is opened for editing/deleting of records. It also allows the combobox we made, which will be used to retrieve a given record, to be visible.
- Private Sub Form_Load()
-
If Len(Nz(Me.OpenArgs, "")) > 0 And Me.OpenArgs = "Edit Only" Then
-
Me.cboResourceID.Visible = True
-
Me.AllowAdditions = False
-
Else
-
Me.cboResourceID.Visible = False
-
End If
-
End Sub
-
Give this a try now, keeping a few things in mind. First, try to get the code working as it's given here. If you have problems, post back here telling us about it. Don't go changing this, changing that willy nilly and then posting back when it doesn't work; this will just end up confusing you and the people here who are trying to help you. This is always a good policy when trying to implement code someone's posted. The exception, of course, would be when you're simply replacing generic control names with your own actual names.
Next, create your
frmSwitchBoard with its buttons and code and your
frmNewResource with it's combobox.
Finally, test things one at a time. Make sure your data form is behaving properly. Make sure the combobox is working correctly, retrieving records as it should. And finally, test out each form opening mode to make sure that they're behaving properly.
I've spent a fair amount of time hacking this problem, and it's all working on my end, but sometimes things do get copied/pasted incorrectly going from Access to here on the forum. So if you have any problems, post back here and be patient. I'll be checking back fairly frequently tomorrow (well, today actually; it's about 0400 hours here) and I'm sure other people will be looking in as well.
Good luck!
Linq
;0)>