Graham, the new record is created on the subform.
:
Hi Silvio
I'm a little confused about what you are actually trying to do,
The code is in your subform, right?
Are you creating a new record in your main form or the subform?
If it's the subform, try this:
Me.Recordset.AddNew
If it's the main form:
Me.Parent.Recordset.AddNew
or
DoCmd.GoToRecord acForm, Me.Parent.Name, acNewRec
The reason Me.Name will not work in a subform is that subforms are not
seen
as open forms which are part of the Forms collection.
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
Graham I have tried to add: DoCmd.GoToRecord acForm, Me.Name,
acNewRec
however I am getting an error message "Error: 2489 the object
"frmProjAddCycle" isn't open". frmProjAddCycle is the name of my
subform
from
wich I am running the code. I am not sure what I am missing out
since
your
methong is new to me :-(
:
OK, so you do not have On Error Resume Next, but have you tried
explicitly
naming the form in GoToRecord as I suggested?
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
Graham, I am using Access 2003. The code runs from the subform.
Below
is
the
full code (A little lenghty)
Private Sub AddCyc_Click()
On Error GoTo HandleErr
Dim MyResponse As Integer
If IsNull(Me.TxAdmin) And _
IsNull(Me.TxAdminDate) And _
IsNull(Me.TxBuilding) And _
IsNull(Me.TxBuildingDate) And _
IsNull(Me.TxComplDate) And _
IsNull(Me.TxElectrical) And _
IsNull(Me.TxElectricalDate) And _
IsNull(Me.TxTradeCombo) And _
IsNull(Me.TxTradeComboDate) And _
IsNull(Me.TxTradeComboDate) Then
MsgBox "As minimum, you must enter at least one trade time
with
the
estimated completion date, and the cycle estimated completion
date.",
vbOKOnly + vbCritical, "Stop!"
Me.TxAdmin.SetFocus
Exit Sub
End If
If Not IsNull(Me.TxAdmin) And IsNull(Me.TxAdminDate) Or Not
IsNull(Me.TxAdminDate) And IsNull(Me.TxAdmin) Then
MsgBox "You must enter both, time and date.", vbOKOnly +
vbCritical,
"Stop!"
If IsNull(Me.TxAdmin) Then
Me.TxAdmin.SetFocus
Else
Me.TxAdminDate.SetFocus
End If
Exit Sub
End If
If Not IsNull(Me.TxBuilding) And IsNull(Me.TxBuildingDate) Or Not
IsNull(Me.TxBuildingDate) And IsNull(Me.TxBuilding) Then
MsgBox "You must enter both, time and date.", vbOKOnly +
vbCritical,
"Stop!"
If IsNull(Me.TxBuilding) Then
Me.TxBuilding.SetFocus
Else
Me.TxBuildingDate.SetFocus
End If
Exit Sub
End If
If Not IsNull(Me.TxElectrical) And IsNull(Me.TxElectricalDate) Or
Not
IsNull(Me.TxElectricalDate) And IsNull(Me.TxElectrical) Then
MsgBox "You must enter both, time and date.", vbOKOnly +
vbCritical,
"Stop!"
If IsNull(Me.TxElectrical) Then
Me.TxElectrical.SetFocus
Else
Me.TxElectricalDate.SetFocus
End If
Exit Sub
End If
If Not IsNull(Me.TxTradeCombo) And IsNull(Me.TxTradeComboDate) Or
Not
IsNull(Me.TxTradeComboDate) And IsNull(Me.TxTradeCombo) Then
MsgBox "You must enter both, time and date.", vbOKOnly +
vbCritical,
"Stop!"
If IsNull(Me.TxTradeCombo) Then
Me.TxTradeCombo.SetFocus
Else
Me.TxTradeComboDate.SetFocus
End If
Exit Sub
End If
If IsNull(Me.TxComplDate) Then
MsgBox "You must enter the estimated completion date for this
cycle.",
vbOKOnly + vbCritical, "Stop!"
Me.TxComplDate.SetFocus
Exit Sub
End If
If IsNull(Forms!frmProjEdit!frmProjCycle.Form!DateComplete) Then
MyResponse = MsgBox("You must close the currect cycle before
you
can
create a new one. Would you like to close it now?", vbYesNo +
vbDefaultButton2 + vbQuestion, "Attention")
If MyResponse = vbYes Then
Forms!frmProjEdit!frmProjCycle.Form!DateComplete =
Date
Forms!frmProjEdit!frmProjCycle.Form!UpdateBy =
DLookup("UserName", "qryCurrentUser")
Forms!frmProjEdit!frmProjCycle.Form!DateUpdate = Now()
Else
Exit Sub
End If
End If
DoCmd.GoToRecord , , acNewRec
EstAdmin = Me.TxAdmin
EstAdminDate = Me.TxAdminDate
EstBuilding = Me.TxBuilding
EstBuildingDate = Me.TxBuildingDate
EstElectrical = Me.TxElectrical
EstElectricalDate = Me.TxElectricalDate
EstTradeCombo = Me.TxTradeCombo
EstTradeComboDate = Me.TxTradeComboDate
EstComplDate = Me.TxComplDate
CreatedBy = DLookup("UserName", "qryCurrentUser")
Me.ProjCycle = NewCycle 'enter the new cycle number to the new
cycle
set
Forms!frmProjEdit!CurrentCycle = NewCycle 'update the cycle
number
in
the project table
Me.TxAdmin = Null
Me.TxAdminDate = Null
Me.TxBuilding = Null
Me.TxBuildingDate = Null
Me.TxElectrical = Null
Me.TxElectricalDate = Null
Me.TxTradeCombo = Null
Me.TxTradeComboDate = Null
Me.TxComplDate = Null
Forms!frmProjEdit.Refresh
MsgBox "New cycle saved successfully!", vbOKOnly, "Great!"
ExitHere:
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "Form_frmProjCycleAdd.AddCyc_Click"
End Select
End Sub
:
Hi Silvio
Do you have On Error Resume Next?
I suspect that GoToRecord is failing for some reason, so the
focus
remains
on the old record, but the error is being ignored.
The other possibility is that another form (perhaps your
subform)
somehow
has the focus when this code is executed, so the GoToRecord is
happening
in
the wrong form.
I suggest for GoToRecord (and DoCmd.Close and other methods that
work
by
default on the current form) you explicitly specify the optional
arguments
so there is no ambiguity:
DoCmd.GoToRecord acForm, Me.Name, acNewRec
Depending on the version of Access, this might also work:
Me.Recordset.AddNew
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
I am using the code below to create a new record (new cycle)
via
forms/subform, however, some time instead of creating a new
record
it
overwrite an existing record, what I am doing wrong here?
... (more code up here)...
DoCmd.GoToRecord , , acNewRec
EstAdmin = Me.TxAdmin
EstAdminDate = Me.TxAdminDate
EstBuilding = Me.TxBuilding
EstBuildingDate = Me.TxBuildingDate
EstElectrical = Me.TxElectrical
EstElectricalDate = Me.TxElectricalDate
EstTradeCombo = Me.TxTradeCombo
EstTradeComboDate = Me.TxTradeComboDate
EstComplDate = Me.TxComplDate
CreatedBy = DLookup("UserName", "qryCurrentUser")
Me.ProjCycle = NewCycle 'enter the new cycle number to the
new
cycle
set
Forms!frmProjEdit!CurrentCycle = NewCycle 'update the cycle
number
in
the project table
end of sub