I have a form with some fields on it. Form was created from the table in question.
I've tried to use the button wizard to create a save button, I've tried to use the following:
Private Sub saveRecord_Click()
DoCmd.RunCommand acCmdSaveRecord
End Sub
Nothing works, any method I try to save the info doesn't work. I've made sure the form is bound, as far as I can tell it is, it was created off that table.
I have this line when the form loads to make sure the form is on a new record:
Me.Recordset.AddNew
From my limited understanding of the language, setting the form to a new record and then doing a save command should work?
RunCommand acCmdSaveRecord will only do a save if there is something to save. Simply adding a record to the form's recordset doesn't give you anything to save.
Make this change to saveRecord_Click() to confirm whether that is the explanation:
Private Sub saveRecord_Click()
If Me.Dirty = True Then
DoCmd.RunCommand acCmdSaveRecord
Else
MsgBox "nothing to save"
End If
End Sub
Some other suggestions ...
Since you want to go to the new record when the form loads, use GoToRecord instead of Me.Recordset.AddNew:
Private Sub Form_Load()
DoCmd.GoToRecord Record:=acNewRec
End Sub
The version of saveRecord_Click() I suggested earlier was only to explore the problem. But for routine use, it doesn't make sense to allow the user to click a saveRecord button and then tell them there is nothing to save. It is better to only make the button clickable when there is something to save.
So you can disable the command button in the form's On Current event and enable it in the On Dirty event (which means there is something to save).
Private Sub Form_Current()
Me.saveRecord.Enabled = False
End Sub
Private Sub Form_Dirty(Cancel As Integer)
Me.saveRecord.Enabled = True
End Sub
Also, as the last step of saveRecord_Click(), you may wish to SetFocus on another control and disable the command button.
Related
I am using an ActiveX Image Combo Control in my VBA Access application and I have been having a lot of issues with it. There is little to no documentation on it. I am unable to catch any events from the control. I tried using the "on change, on get focus, and on exit events" and none of them work. The events fire when the form is initialized but that is it. Does anyone know what is up with this control? I am debating as to whether I should cut my losses and use a normal combo box without any images.
The full name of the ActiveX Control is "Microsoft ImageComboBox Control, version 6.0"
Here is my debugging code
Private Sub ImageCombo8_Change()
Debug.Print "change"
End Sub
Private Sub ImageCombo8_Enter()
Debug.Print "entered"
End Sub
Private Sub ImageCombo8_Exit(Cancel As Integer)
Debug.Print "exit"
End Sub
Private Sub ImageCombo8_GotFocus()
Debug.Print "focused"
End Sub
On initialization, I get these ouputs
entered
focused
exit
change
change
And nothing happens when I actually do anything to the control.
Setting up the control with items:
Private Sub Form_Load()
Dim objNewItem As ComboItem
Set objNewItem = ImageCombo8.ComboItems.Add(1, , "Option 1", "pic1key")
objNewItem.Indentation = 1
Set objNewItem = ImageCombo8.ComboItems.Add(2, , "Option 2", "pic2key")
objNewItem.Indentation = 1
End Sub
I found the GotFocus event only triggered when tabbing into the control, not mouse click. Only the Change event triggers during form load, it does not trigger when item selected, nor does Updated. Enter, Exit, GotFocus, LostFocus all trigger appropriately. Also, the Click event does trigger when an item in the list is clicked.
Here is my working code:
Private Sub Form_Load()
Dim objNewItem As ComboItem, x As Integer
For x = 1 to 2
Set objNewItem = ImageCombo8.ComboItems.Add(x, , "Option " & x, "pic" & x & "key")
objNewItem.Indentation = 1
Next
End Sub
Private Sub ImageCombo8_Click()
MsgBox Me.ImageCombo8.SelectedItem.Text
End Sub
Even though the ControlSource property shows available on property sheet, it errors if bound to a field. The Value property also fails in code. Use Text property to capture info from the selected item and code to save data to field.
Took forever to figure out how to set up the ImageList and ImageCombo controls. Not a lot of info out there on these critters and most of it involves non-Access programs. Finally discovered that a double click on control in design view opened a dialog box and then found clicking the ellipsis on Custom property does the same.
I have a bound form in MS Access and I use a submit button to insert the data from form to table. I have around 10 fields in form and even if I fill only 1 field and press the F5 button, it saves the data with just one field. How to stop F5 key from doing this.
Edit - also when I close the form with partially filled data or if it gets closed accidently or when if i open design mode from there, it collects that partially filled data and then creates an entry, how to stop userform making entries via other means and make it only create record on button click.
In my opinion, you should not stop the F5 button from doing this, you should stop anything else than your submit button from saving data.
This can be achieved with some VBA code:
The save button is named cmdSave in this example
Private saveButtonPressed As Boolean
Private Sub cmdSave_Click()
saveButtonPressed = True
DoCmd.RunCommand acCmdSaveRecord
End Sub
Private Sub Form_BeforeInsert(Cancel As Integer)
If Not saveButtonPressed Then
'Update through other means
Cancel = True
End If
saveButtonPressed = False
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not saveButtonPressed Then
'Update through other means
Cancel = True
End If
saveButtonPressed = False
End Sub
I have some code in Before_Update event of Subform. Can I Cancel this code when I click on another control in Main form, like a Cmdbutton ? I tried with this, but It's not working:
Public CnclEvnt As Boolean
Private Sub Form_BeforeUpdate(Cancel As Integer)
If CnclEvnt=True Then
Cancel=True
End if
End sub
Private Sub cmdButton1_Click()
CnclEvnt=True
End Sub
No. The update happens before the click event or any other event like the OnExit of the subform control.
The only method I can see is to always cancel the update and then have a button to actively save the record. However, if you have several records in the subform, this would be very cumbersome.
Does anyone have an idea what could prevent either of these commands from actually saving a form? Is there a setting I'm missing that could make the form "read-only"? I'm toggling the control.enabled property on the form on load, but I can't seen to make it stick once I've closed the form.
DoCmd.Close acForm, Me.Name, acSaveYes
DoCmd.Save acForm, Me.Name
Edit:
Some psuedo code to clarify.
This function runs on form load to enable/disable certain controls and works 100% as expected.
Private Sub Form_Load()
If IsNull(TempVars.Item(Me.Name)) Then
TempVars.Add Me.Name,1
If somecondition = true then
Me.Controls.Item("somecontrol").enabled = True
Else
Me.Controls.Item("somecontrol").enabled = False
End If
End If
End Sub
This click event closes the form and opens another form. The problem is, the control.enabled settings I set on form load do not save.
Private Sub button_OnClick()
DoCmd.OpenForm "someotherform", acnormal
DoCmd.Close acForm, Me.Name, acSaveYes
End Sub
The next time I open the first form, the control properties will not be set, and I need to set the conrol.enabled property again.
I've not been able to find any documentation, but it appears that you can not save design changes that are made in Normal mode. I ended up declaring a global collection type variable. I stored concatenated strings in this collection variable of the convention "Form.Name & Control.Name". If the form had never been opened, I ran the long check on which controls should be enabled, and stored them in this collection global. If the form had been opened at least once, I looped through the controls on the form checking to see if they were in my global collection of "authorized" controls. It feels really hackish, but it works pretty well.
The code you have (Close, then Save) fails on my testing because the Me.Name is empty after executing the Close. Although I don't know what other code you may have, I was able to get the Form to close once I switched the order.
I suggest you place the code in the 'Open' event as follows:
Private Sub Form_Open(Cancel As Integer)
DoCmd.Save acForm, Me.Name
DoCmd.Close acForm, Me.Name, acSaveYes
End Sub
Do you want to change the enabled property on a condition? If you just need that control disabled IF the record ... then you'll want to move the enabling the form's Current event, which fires at every record navigation. Load and Open are too early and don't repeat at navigation.
Private Sub Form_Current()
If Me!X = True Then
Me.Controls("somecontrol").Enabled = False
Else
Me.Controls("somecontrol").Enabled = True
End If
End Sub
I have a bound form in microsoft access that allows a user to add data to a table. I want to place a "cancel" button on the form, that if clicked, will stop the record commitment.
What is the command I need to cancel the record commitment?
It's been a few years since I've had to work with Access, but you should be able to undo changes to the current record using the command:
DoCmd.RunCommand acCmdUndo
or
Me.Undo
?
I have a similar situation where I want the user to confirm whether to save changes, and have used the form's BeforeUpdate event to ask the user if they want to save changes. It seems to work pretty well, at least in my case.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox("Do you want to save changes?", vbYesNo, "Confirm change") = vbNo Then
Cancel = True
Else
'Do Nothing
End If
End Sub