Adding Command Button To Form

uneek78

Registered User.
Local time
Today, 11:15
Joined
Mar 26, 2009
Messages
68
I am using Access 2000. Evidently adding a Command Button isn't as cut and dry as clicking on it, selecting a location on your form, and going through the wizard and choosing what you want. I have added a Save Record & Add Record to my form. Evidently some VB coding is required. Which, I don't see why the option is there to add a button through the wizard w/ preset names (ex - Save Record, Add Record, Next Record, etc..) if they don't automatically work when you use them. Can anybody help me!?! I am familar with access, but have no idea about this VB coding stuff. So if you have the time and decide to answer my question I would need to know step-by-step how to incorporate anything that requires code to make this work. I don't understand ANY VB AT ALL!!!! Yeah, I'm pitiful. The 1st step is admittance. The 2nd step is YOU helping me. I'm about to lose my mind..........:eek:
 
When you select the wizard created CommandButton and go to properties and then the Event tab, what do you see next to the Click event? Press the "..." button next to the Click event and tell us what you see.
 
Attached is the message I get when I click the New Record button. It also appears to have that exact same code in there already.
 

Attachments

  • access.JPG
    access.JPG
    19.7 KB · Views: 111
You only want the code that the "..." takes you to so delete the other procedure. Then try your button again.
 
This is the code on the "Add Record" button:

Private Sub Add_Record_Click()
On Error GoTo Err_Add_Record_Click


DoCmd.GoToRecord , , acNewRec

Exit_Add_Record_Click:
Exit Sub

Err_Add_Record_Click:
MsgBox Err.Description
Resume Exit_Add_Record_Click

End Sub


What do I do with it!?!
 
This should only be listed once in the code module for the form. Is it there more than once? If it is then delete or comment out the other procedures and try the button again. It should work as posted.
 
If I right-click on the button and click "..." and copy the whole coding I see from top to bottom, this is what is there:

Option Compare Database

Private Sub Browse_ALL_Click()
On Error GoTo Err_Browse_ALL_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Critical Test / Result"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Browse_ALL_Click:
Exit Sub

Err_Browse_ALL_Click:
MsgBox Err.Description
Resume Exit_Browse_ALL_Click

End Sub
Private Sub Save_Record_Click()
On Error GoTo Err_Save_Record_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_Save_Record_Click:
Exit Sub

Err_Save_Record_Click:
MsgBox Err.Description
Resume Exit_Save_Record_Click

End Sub
Private Sub Save_Record_Click()
On Error GoTo Err_Save_Record_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_Save_Record_Click:
Exit Sub

Err_Save_Record_Click:
MsgBox Err.Description
Resume Exit_Save_Record_Click

End Sub
Private Sub Add_Record_Click()
On Error GoTo Err_Add_Record_Click


DoCmd.GoToRecord , , acNewRec

Exit_Add_Record_Click:
Exit Sub

Err_Add_Record_Click:
MsgBox Err.Description
Resume Exit_Add_Record_Click

End Sub

Private Sub Form_Open(Cancel As Integer)

End Sub
Private Sub Save_Record_Click()
On Error GoTo Err_Save_Record_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_Save_Record_Click:
Exit Sub

Err_Save_Record_Click:
MsgBox Err.Description
Resume Exit_Save_Record_Click

End Sub
Private Sub New_Record_Click()
On Error GoTo Err_New_Record_Click


DoCmd.GoToRecord , , acNewRec

Exit_New_Record_Click:
Exit Sub

Err_New_Record_Click:
MsgBox Err.Description
Resume Exit_New_Record_Click

End Sub
Private Sub Command43_Click()
On Error GoTo Err_Command43_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_Command43_Click:
Exit Sub

Err_Command43_Click:
MsgBox Err.Description
Resume Exit_Command43_Click

End Sub
Private Sub Command44_Click()
On Error GoTo Err_Command44_Click


DoCmd.GoToRecord , , acNewRec

Exit_Command44_Click:
Exit Sub

Err_Command44_Click:
MsgBox Err.Description
Resume Exit_Command44_Click

End Sub
Private Sub Save_Record_Click()
On Error GoTo Err_Save_Record_Click


DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_Save_Record_Click:
Exit Sub

Err_Save_Record_Click:
MsgBox Err.Description
Resume Exit_Save_Record_Click

End Sub
Private Sub Add_Record_Click()
On Error GoTo Err_Add_Record_Click


DoCmd.GoToRecord , , acNewRec

Exit_Add_Record_Click:
Exit Sub

Err_Add_Record_Click:
MsgBox Err.Description
Resume Exit_Add_Record_Click

End Sub
 
I guess I would like to know why do I go through the wizard for adding the command button and the button not work. You would think it would put the code in there for you being that the options for what kind of button you want are already pre-written in the wizard. Just seems kind of silly to me that you would create the button using their process and it not work automatically. I dunno............memoirs of a non-coder.
 
the thing is

why do you need to add a save record button to your form, or an add record button

access isnt like excel where you can undo actions - a record is automatically saved in certain circumstances

an addrecord button is actually quite an unusual thing to need- i can rarely recall ever having a button for that purpose.

all you generally need to do is include navigation buttons on your form (looks like vcr controls at the bottom) - just click the * and it takes you to the new record page - which is all the command button is doing anyway
 
Because the form will be for a host of users that need big buttons and such to help clarify what they're doing. Clicking Save is a lot more reassuring then hoping it was saved.
 
How can I take those navigation buttons off anyway? I want my own command buttons, not those. This form is for employees that are as far from technical as you get and they get too confused when they have a lot of buttons and options. Believe me, I'm the one who has to educate them and then repeat it 5 times for them to understand.
 
You have several duplicates. Comment them out or delete them.
Code:
Option Compare Database
Option Explicit

Private Sub Browse_ALL_Click()
   On Error GoTo Err_Browse_ALL_Click

   Dim stDocName As String
   Dim stLinkCriteria As String

   stDocName = "Critical Test / Result"
   DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Browse_ALL_Click:
   Exit Sub

Err_Browse_ALL_Click:
   MsgBox Err.Description
   Resume Exit_Browse_ALL_Click

End Sub

Private Sub Save_Record_Click()
   On Error GoTo Err_Save_Record_Click


   DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_Save_Record_Click:
   Exit Sub

Err_Save_Record_Click:
   MsgBox Err.Description
   Resume Exit_Save_Record_Click

End Sub

[COLOR="SeaGreen"]'Private Sub Save_Record_Click()
'   On Error GoTo Err_Save_Record_Click
'
'
'   DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
'
'Exit_Save_Record_Click:
'   Exit Sub
'
'Err_Save_Record_Click:
'   MsgBox Err.Description
'   Resume Exit_Save_Record_Click
'
'End Sub[/COLOR]
Private Sub Add_Record_Click()
   On Error GoTo Err_Add_Record_Click


   DoCmd.GoToRecord , , acNewRec

Exit_Add_Record_Click:
   Exit Sub

Err_Add_Record_Click:
   MsgBox Err.Description
   Resume Exit_Add_Record_Click

End Sub

Private Sub Form_Open(Cancel As Integer)

End Sub

[COLOR="SeaGreen"]'Private Sub Save_Record_Click()
'   On Error GoTo Err_Save_Record_Click
'
'
'   DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
'
'Exit_Save_Record_Click:
'   Exit Sub
'
'Err_Save_Record_Click:
'   MsgBox Err.Description
'   Resume Exit_Save_Record_Click
'
'End Sub[/COLOR]
Private Sub New_Record_Click()
   On Error GoTo Err_New_Record_Click


   DoCmd.GoToRecord , , acNewRec

Exit_New_Record_Click:
   Exit Sub

Err_New_Record_Click:
   MsgBox Err.Description
   Resume Exit_New_Record_Click

End Sub

Private Sub Command43_Click()
   On Error GoTo Err_Command43_Click


   DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Exit_Command43_Click:
   Exit Sub

Err_Command43_Click:
   MsgBox Err.Description
   Resume Exit_Command43_Click

End Sub

Private Sub Command44_Click()
   On Error GoTo Err_Command44_Click


   DoCmd.GoToRecord , , acNewRec

Exit_Command44_Click:
   Exit Sub

Err_Command44_Click:
   MsgBox Err.Description
   Resume Exit_Command44_Click

End Sub

[COLOR="SeaGreen"]'Private Sub Save_Record_Click()
'   On Error GoTo Err_Save_Record_Click
'
'
'   DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
'
'Exit_Save_Record_Click:
'   Exit Sub
'
'Err_Save_Record_Click:
'   MsgBox Err.Description
'   Resume Exit_Save_Record_Click
'
'End Sub

'Private Sub Add_Record_Click()
'   On Error GoTo Err_Add_Record_Click
'
'
'   DoCmd.GoToRecord , , acNewRec
'
'Exit_Add_Record_Click:
'   Exit Sub
'
'Err_Add_Record_Click:
'   MsgBox Err.Description
'   Resume Exit_Add_Record_Click
'
'End Sub[/COLOR]
 
navigation buttons?

in a forms property sheet - just set navigation buttons to no

you might want to set record selectors to no also, if you want to remove more confusing stuff
 
Cool, I was able to get those navigational buttons off. Thx! I really haven't done anything w/ the button coding, because I'm scared I'm going to screw it up and not be able to put it back to what it was and make everything worse.
 
First things first
Is your form a data entry form or will the users also need to view the data entered.

Also something else
A command button wizard creates one command action at a time but that does not limit you from using one command button to achieve several things.
For example if you want to add a new record and save the current record at the same time you can use

Code:
Private Sub Add_Record_Click()
On Error GoTo Err_Add_Record_Click

[B]DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70[/B]
    DoCmd.GoToRecord , , acNewRec

Exit_Add_Record_Click:
    Exit Sub

Err_Add_Record_Click:
    MsgBox Err.Description
    Resume Exit_Add_Record_Click
    
End Sub

First save the record and then move next
NOTE: Ensure validation for fields with required Data
 
The users will not need to access the data for any reason. I actually successfully have my "Save Record" & "Close" command buttons working by using simple made macros. Unfortunately when creating a macro you don't the option for "Add Record". I would prefer to stay away from all this coding if possible, because none of it makes sense to me. I have 2 out of 3 buttons working now. I'm hoping for an answer that I can actually do for "Add Record". The coding stuff is above my head. Sorry, I probably turn your stomach by saying it, but that's definitely not my area of expertise. Is there a reason the options for creating macros included "Save" & "Close" and many others, but excluded one for "Add Record"?
 
Got it! I google and someone answered this question in this forum and got an easy answer. He said he creates a macro that saves, closes, & reopens the form. I was able to do that..........viola!!!!
 
There you go. Such a simple solution and we overlooked it.
 

Users who are viewing this thread

Back
Top Bottom