问题
Im working on a key inventory database. Something to manage who signs out room keys and when they bring them back in. I've created the tables I plan on using and have started working on the forms. However, I am having trouble getting the forms/subforms to work as desired.
I've created buttons to add/edit/delete/etc the records in the subform which are a result of the tables. I've coded the buttons based on an old project I did in my DB class (someone else did the forms then, I did queries). I can't seem to get them working. So far, I've only coded the buttons for "mainKeys" form so far.
I've noticed the main form shows 1 of 14 records whereas the forms from my old project only showed all records in the subforms.
Here's the Access file: http://jumpshare.com/b/W7AKih
Here's my old project (this is what im trying to copy): http://jumpshare.com/b/r7Y6O1
Here's the code for the buttons:
Option Compare Database
Private Sub cmdAdd_Click()
If Me.keyID.Tag & "" = "" Then
CurrentDb.Execute "INSERT INTO KEYS(KEY_ID, ROOM, DRAWER)" & _
" VALUES(" & Me.keyID & ",'" & Me.roomID & "'," & Me.drawerID & ")"
subKey.Form.Requery
Else
CurrentDb.Execute "UPDATE KEYS " & _
" SET KEY_ID=" & Me.keyID & _
", ROOM='" & Me.roomID & "'" & _
", DRAWER='" & Me.drawerID & "'" & _
" WHERE KEY_ID=" & Me.keyID.Tag
End If
cmdReset_Click
subKey.Form.Requery
End Sub
Private Sub cmdBack_Click()
End Sub
Private Sub cmdDelete_Click()
If Not (Me.subKey.Form.Recordset.EOF And Me.subKey.Form.Recordset.BOF) Then
If MsgBox("Confirm Deletion?", vbYesNo) = vbYes Then
CurrentDb.Execute "DELETE FROM KEYS" & _
" WHERE KEY_ID=" & Me.subKey.Form.Recordset.Fields("KEY_ID")
Me.subKey.Form.Requery
End If
End If
End Sub
Private Sub cmdEdit_Click()
If Not (Me.subKey.Form.Recordset.EOF And Me.subKey.Form.Recordset.BOF) Then
With Me.subKey.Form.Recordset
Me.keyID = .Fields("KEY_ID")
Me.roomID = .Fields("ROOM")
Me.drawerID = .Fields("DRAWER")
Me.keyID.Tag = .Fields("KEY_ID")
Me.cmdAdd.Caption = "Update Record"
Me.cmdEdit.Enabled = False
End With
End If
End Sub
Private Sub cmdExit_Click()
DoCmd.Close
End Sub
Private Sub cmdReset_Click()
Me.keyID = ""
Me.roomID = ""
Me.drawerID = ""
Me.keyID.SetFocus
Me.cmdEdit.Enabled = True
Me.cmdAdd.Caption = "ADD KEY"
Me.keyID.Tag = ""
End Sub
From what I can see the Reset & Exit buttons work just fine, the add/delete/update records do not. Also, this is a small project I am working on for an office in my school. If anyone has any recommendations from what they see in the file that would be great.
回答1:
The reason nothing is happening is that the event line in your database is set to [Embedded Macro] rather than [Event Procedure], so the code you have posted is never run. It is always wise to set a breakpoint when you have a problem to see if the code is reached.
After that, to my mind, you are going to far too much trouble in one way and far too little in another. Instead of writing all that insert code when Access will handle an addition to the subform very nicely, write a lot more checking code to make sure the data is in good shape.
来源:https://stackoverflow.com/questions/14253306/access-buttons-vba-problems-and-subform-issues