"Add Record" gives "Can't go to specified record" after first use Access - ms-access

I have a command button in a form that allows users to add a new record. The command only works the first time you click it after the form is opened. I can navigate through the records without any errors and the first time you add a record after the form is opened will not give errors. Once you try to add a second record, the "Can't go to the specified record" error message is displayed.
I can make edits in the query without a problem and I can edit things without a problem. If I close out of the form and reopen it I can add a new record without issues. Anyone have ideas?
Using VBA, it's the standard add-new code, nothing else. Posted all the code for this form below. Some of the command buttons listed are in macros, don't know if that makes a difference.
Private Sub add_Click()
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub edit_info_Click()
Date_of_Echo.Locked = False
ID.Locked = False
AoV.Locked = False
AI.Locked = False
MR.Locked = False
TR.Locked = False
TR_velocity.Locked = False
PA_pressures.Locked = False
LVeDD.Locked = False
LVeSD.Locked = False
RV_function.Locked = False
comments.Locked = False
Me.next.Visible = False
Me.previous.Visible = False
Me.first.Visible = False
Me.last.Visible = False
Me.add.Visible = False
Me.save.Visible = True
Me.save.SetFocus
Me.edit_info.Visible = False
End Sub
Private Sub Form_Current()
If Me.NewRecord Then
Me.recordcounter.Caption = "New Record"
Me.next.Visible = False
Me.previous.Visible = False
Me.first.Visible = False
Me.last.Visible = False
Me.add.Visible = False
Me.edit_info.Visible = False
Me.save.Visible = True
Date_of_Echo.Locked = False
ID.Locked = False
AoV.Locked = False
AI.Locked = False
MR.Locked = False
TR.Locked = False
TR_velocity.Locked = False
PA_pressures.Locked = False
LVeDD.Locked = False
LVeSD.Locked = False
RV_function.Locked = False
comments.Locked = False
Else
Me.recordcounter.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.RecordCount
Me.next.Visible = True
Me.previous.Visible = True
Me.first.Visible = True
Me.last.Visible = True
Me.add.Visible = True
Me.edit_info.Visible = True
Me.save.Visible = False
End If
End Sub
Private Sub save_Click()
DoCmd.save
Me.previous.Visible = True
Me.first.Visible = True
Me.last.Visible = True
Me.next.Visible = True
Me.add.Visible = True
Me.edit_info.Visible = True
Me.recordcounter.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.RecordCount
Me.add.SetFocus
Me.save.Visible = False
Date_of_Echo.Locked = True
ID.Locked = True
AoV.Locked = True
AI.Locked = True
MR.Locked = True
TR.Locked = True
TR_velocity.Locked = True
PA_pressures.Locked = True
LVeDD.Locked = True
LVeSD.Locked = True
RV_function.Locked = True
comments.Locked = True
End Sub
Private Sub next_Click()
On Error GoTo Err_cmdLastRecord_Click
Me.AllowAdditions = False
DoCmd.GoToRecord , , acNext
Exit_cmdLastRecord_Click:
Exit Sub
Err_cmdLastRecord_Click:
MsgBox " There are no more records ", vbExclamation, ""
Resume Exit_cmdLastRecord_Click
End Sub

My first thought is that your record isn't saving before you try to go to a new record so try this and see if it fixes the issue.
Private Sub add_Click()
If Me.Dirty Then
Me.Dirty = False
End If
DoCmd.GoToRecord , , acNewRec
End Sub

If anyone is still having this problem, I can't explain why it's happening, but here's a work around:
Reset the AllowAdditionsProperty on the form:
Me.[form_name].Form.AllowAdditions = False
Me.[form_name].Form.AllowAdditions = True
This was a very strange issue because my code had been working fine for weeks. I was using something like this:
Me.frm.SetFocus
DoCmd.GoToRecord , , acNewRec
!Field1 = value1
!Field2 = value2
It was working great, and then out of the blue I started getting the "Can't go to Specified Record" error, even though I had made no changes to the code. The form settings were fine and I could add records all day long in the underlying table. I even checked the form's AllowAdditions property in the Immediate window, and that showed it was set to True. But I tried this work around, and it worked.
Now I add those two lines after every instance of Docmd.GoToNewRoecord, acNewRecord and it's working.

Another thing I have mentioned with the same problem is that you should check the Form data source query. Are all the FIELDS available, are the JOINS correct?
In my case I changed indexed joins from text fields to numeric and it needed update also in Form Data source query.

Related

MS Access Form locked for edit - Added unlock button but can't add New Record

I've got a MS Access form with a sub-form which I want to be read-only for existing records to prevent accidental changes. I've done this by putting simple code on both forms:
Private Sub Form_Current()
Me.AllowEdits = False
Me.AllowDeletions = False
Me.AllowAdditions = False
End Sub
I also added a button that changes the form to allow edits:
Private Sub Command110_Click()
Me.AllowEdits = True
Me.AllowDeletions = True
Me.AllowAdditions = True
End Sub
So I can then edit the form. However I have a button on the parent form to create a new record. I used the standard button function builder "Record Operations -> Add New Record" to create this button. But when I try to use it the error says: Error - You Can't Go To The Specified Record.
So I created a new button with this code:
Private Sub NewClientButton_Click()
Me.AllowEdits = True
Me.AllowDeletions = True
Me.AllowAdditions = True
DoCmd.GoToRecord , , acNewRec
End Sub
Thinking that would solve it but I still get an error: Runtime Error 2105 You can't Go To Specified Record.
The code on my form in totality looks like this:
Private Sub Command110_Click()
Me.AllowEdits = True
Me.AllowDeletions = True
Me.AllowAdditions = True
End Sub
Private Sub Form_Current()
Me.AllowEdits = False
Me.AllowDeletions = False
Me.AllowAdditions = False
End Sub
Private Sub NewClientButton_Click()
Me.AllowEdits = True
Me.AllowDeletions = True
Me.AllowAdditions = True
DoCmd.GoToRecord , , acNewRec
End Sub
Is it just the ordering of my code subs that is the problem or something else?
Thanks.

Runtime Error 438 Hiding Page

I receive a
run-time error 438
when changing visibility of a page in Access VBA. The code below is called "AfterUpdate" for a listbox. I believe my syntax is correct after much review. I've tested the conditional statements with MsgBox's successfully.
Any ideas?
Public Sub RefreshControlTabs()
If [Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData]![Channel].Value = "Mail" Then
[Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData].[tabChannelControl].Pages(0).Visible = True
[Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData].[tabChannelControl].Pages(1).Visible = False
ElseIf [Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData]![Channel].Value.Value = "Email" Then
[Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData].[tabChannelControl].Pages(1).Visible = True
[Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData].[tabChannelControl].Pages(0).Visible = False
Else
'do nothing
MsgBox "Valid Email/Mail channel not found for this job."
[Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData].[tabChannelControl].Pages(0).Visible = True
[Forms]![frmPrintPostage_Email_DB]![subfrm_CampaignData].[tabChannelControl].Pages(1).Visible = False
End If
End Sub
Perhaps your line:
ElseIf [Forms]! ... [Channel].Value.Value = "Email" Then
Should be:
ElseIf [Forms]! ... [Channel].Value = "Email" Then

User Defined Shortcut Keys Fail to Fire Event (In Some Circumstances)

I'm working with MS Access 2007. I have an event mapped to a command button, which works as expected when it's clicked. I've also added an ampersand in the caption field for the button command, so users can use shortcut keys (in this case alt+s) to also fire the event.
Here is the command that's executed when the command button is clicked or, depending on which fields are in focus, alt+s is pressed:
Private Sub cmdSearch_Click()
If cmdSearch.Caption = "&Search Mode" Then
cmdDelete.Enabled = False
cmdGotoNew.Enabled = False
cmdMenu.Enabled = False
cmdLicHistory.Enabled = False
cmdComplaints.Enabled = False
cmdAddPrint.Enabled = False
cmdReceipts.Enabled = False
cmdLicensee.Enabled = False
cmdSearch.Caption = "Run &Search"
Me.Filter = "1 = 0"
Me.FilterOn = True
DoCmd.GoToRecord acDataForm, "frmLicense", acNewRec
Detail.BackColor = 12615808
Else
Dim cntrl As Control
Dim strFilter As String
For Each cntrl In Me.Controls
If TypeName(cntrl) = "Textbox" Or TypeName(cntrl) = "Combobox" Then
If Not Len(cntrl) = 0 Then
If Not Len(strFilter) = 0 Then strFilter = strFilter & "and "
strFilter = strFilter & cntrl.ControlSource & " like '*" & cntrl & "*' "
End If
End If
Next
If Not Len(strFilter) = 0 Then
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
End If
Me.Filter = strFilter
Me.FilterOn = True
DoCmd.GoToRecord acDataForm, "frmLicense", acFirst
cmdDelete.Enabled = True
cmdGotoNew.Enabled = True
cmdMenu.Enabled = True
cmdLicHistory.Enabled = True
cmdComplaints.Enabled = True
cmdAddPrint.Enabled = True
cmdReceipts.Enabled = True
cmdLicensee.Enabled = True
cmdSearch.Caption = "&Search Mode"
' Detail.BackColor = 12632256
Detail.BackColor = -2147483633 ' Grey
End If
End Sub
However, depending on which fields are in focus, the shortcut keys will fire or not fire. Fields that nested in option groups will fire; fields nested in a tab control will not fire.
Is this behavior expected? This is a form level event, so I'm inclined to say that it's not. Any advice on debugging this would be appreciated.

Microsoft Access: Saving combo box data from form to table

I have a table that stores data entered by a user in a form. All of the data entered in the form saves properly in the table except for those fields chosen from a combo box. I have checked to make sure that all of these combo box selections in the Form Design View are bound to the associated table fields in the underlying data.
Currently, I have the data type for those fields in the design view of the table set as "Short Text". I am wondering if the problem is that I need to set the data type to a different type, or if there is something else causing this problem.
Option Compare Database
Option Explicit
Dim stay As String
Function EnableInfo()
Me.ADescription.Locked = False
Me.ComboVendor.Locked = False
Me.ComboVendor.Locked = False
Me.onHand.Locked = False
Me.onOrder.Locked = False
Me.CostB.Locked = False
Me.ListPriceB.Locked = False
End Function
Function DisableInfo()
Me.ADescription.Locked = True
Me.ComboVendor.Locked = True
Me.onHand.Locked = True
Me.onOrder.Locked = True
Me.CostB.Locked = True
Me.ListPriceB.Locked = True
End Function
'------------------------------------------------------------
' Function that disables/enable when command buttons when ADD and Edit are clicked!
'------------------------------------------------------------
Function AddEdit()
Me.CmdAdd.Enabled = False
Me.CmdEdit.Enabled = False
Me.CmdExit.Enabled = False
Me.CmdSave.Enabled = True
Me.CmdCancel.Enabled = True
Me.AllowAdditions = True
Me.AllowDeletions = True
Me.AllowEdits = True
Call EnableInfo
End Function
'------------------------------------------------------------
' Function that disables/enable when command buttons when Save and Cancel are clicked!
'------------------------------------------------------------
Function SaveCancel()
Me.CmdAdd.Enabled = True
Me.CmdEdit.Enabled = True
Me.CmdExit.Enabled = True
Me.CmdSave.Enabled = False
Me.CmdCancel.Enabled = False
Call DisableInfo
End Function
'------------------------------------------------------------
' Function that enables navigation buttons
'------------------------------------------------------------
Function EnableNavigation()
Me.cmdFirst.Enabled = True
Me.cmdNext.Enabled = True
Me.cmdPrevious.Enabled = True
Me.cmdlast.Enabled = True
End Function
'------------------------------------------------------------
' Function that disables navigation buttons
'------------------------------------------------------------
Function DisableNavigation()
Me.cmdFirst.Enabled = False
Me.cmdNext.Enabled = False
Me.cmdPrevious.Enabled = False
Me.cmdlast.Enabled = False
End Function
'------------------------------------------------------------
' Function when the ADD button is clicked
'------------------------------------------------------------
Private Sub CmdAdd_Click()
PartIDtext.SetFocus
stay = PartIDtext.Value
Me.DataEntry = True
Call EnableInfo
Call AddEdit
Call DisableNavigation
End Sub
'------------------------------------------------------------
' Function when the CANCEL button is clicked
'------------------------------------------------------------
Private Sub CmdCancel_Click()
Call SaveCancel
Call DisableInfo
Call EnableNavigation
Me.Undo
Me.DataEntry = False
Me.RecordsetClone.FindFirst "partID = " & stay
Me.Bookmark = Me.RecordsetClone.Bookmark
End Sub
'------------------------------------------------------------
' Function when the EDIT button is clicked
'------------------------------------------------------------
Private Sub CmdEdit_Click()
Call AddEdit
Call EnableInfo
Call DisableNavigation
PartIDtext.SetFocus
stay = PartIDtext.Value
End Sub
'------------------------------------------------------------
' Function when the EXIT button is clicked
'------------------------------------------------------------
Private Sub CmdExit_Click()
DoCmd.Close
End Sub
'------------------------------------------------------------
' Function when the SAVE button is clicked
'------------------------------------------------------------
Private Sub CmdSave_Click()
ADescription = Trim(ADescription.Value)
stay = Me.PartIDtext.Value
If IsNull(Me.ADescription) Or Len(Me.ADescription) < 5 Then
MsgBox "Please enter a description of at least 5 characters"
Me.ADescription.SetFocus
ElseIf IsNull(Me.onHand) Or (Me.onHand) < 0 Then
MsgBox "On hand must have a value greater than 0"
Me.onHand.SetFocus
ElseIf IsNull(Me.ComboVendor) Then
MsgBox "select one"
Me.onHand.SetFocus
ElseIf IsNull(Me.onOrder) Or (Me.onOrder) < 0 Then
MsgBox "On order must have a value greater than 0"
Me.onOrder.SetFocus
ElseIf IsNull(Me.CostB) Or (Me.CostB) < 0 Then
MsgBox "Cost must have a value greater than 0"
Me.CostB.SetFocus
ElseIf IsNull(Me.ListPriceB) Or (Me.ListPriceB) < (Me.CostB) Then
MsgBox "List price must be greater than cost!"
Me.ListPriceB.SetFocus
Else
Me.DataEntry = False
Me.RecordsetClone.FindFirst "partID = " & stay
Me.Bookmark = Me.RecordsetClone.Bookmark
Call SaveCancel
Call DisableInfo
Call EnableNavigation
End If
End Sub
'------------------------------------------------------------
' CmdNext
'------------------------------------------------------------
Private Sub CmdNext_Click()
On Error Resume Next
DoCmd.GoToRecord , "", acNext
End Sub
'------------------------------------------------------------
' CmdPrevious
'------------------------------------------------------------
Private Sub CmdPrevious_Click()
On Error Resume Next
DoCmd.GoToRecord , "", acPrevious
End Sub
'------------------------------------------------------------
' CmdFirst
'------------------------------------------------------------
Private Sub CmdFirst_Click()
DoCmd.GoToRecord , "", acFirst
End Sub
'------------------------------------------------------------
' CmdLast
'------------------------------------------------------------
Private Sub CmdLast_Click()
DoCmd.GoToRecord , "", acLast
End Sub

ACCESS : VBA Code Button Visibility not working

I have a problem in hiding my buttons and not working mostly is when the button itself was clicked, it supposed to be hidden.
I have a code below from one of my buttons but most of the button visibility codes are not working. I tried to add:
DoEvents
but still don't work. I don't know what the problem is. Some Enable properties were working but some are not.
Private Sub cmdSave_Click()
If MsgBox("Are you sure to save record?", vbYesNo, "Save Record") = vbYes Then
DoEvents
imgPic.Enabled = False
txtLRN.Enabled = False
txtLN.Enabled = False
txtFN.Enabled = False
txtMN.Enabled = False
txtS.Enabled = False
txtA.Enabled = False
txtBD.Enabled = False
txtBP.Enabled = False
txtMT.Enabled = False
txtIP.Enabled = False
txtRG.Enabled = False
txtAH.Enabled = False
txtAB.Enabled = False
txtAM.Enabled = False
txtAP.Enabled = False
txtF.Enabled = False
txtM.Enabled = False
txtGN.Enabled = False
txtGR.Enabled = False
txtCN.Enabled = False
txtR.Enabled = False
DoEvents
cmdSearch.Visible = True
cmdFirst.Visible = True
cmdPrev.Visible = True
cmdNext.Visible = True
cmdNext.Visible = True
cmdLast.Visible = True
cmdNew.Visible = True
cmdDelete.Visible = True
cmdEdit.Visible = True
DoEvents
cmdSave.Visible = False
cmdCancel.Visible = False
DoEvents
End if
End Sub
This simplified version of your procedure will trigger error #2165, "You can't hide a control that has the focus."
Private Sub cmdSave_Click()
Me.cmdSave.Visible = False
End Sub
During cmdSave_Click, cmdSave has focus, so you can't hide it without first moving focus to another unhidden control. This version will avoid that error:
Private Sub cmdSave_Click()
Me.cmdSearch.Visible = True
Me.cmdSearch.SetFocus
Me.cmdSave.Visible = False
End Sub
In similar cases, I've found that you need to specify the form reference ("Me.").
So try (for example)
Me.cmdCancel.Visible = True
In other words specify "Me." in front of the control name.