Add a new record

S

stockton12

I have a command button on a form that will open another form. Attached is
the code:

Private Sub cmdLog_Click()
On Error GoTo Err_cmdLog_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmLog"

stLinkCriteria = "[Customer ID]=" & Me![txtCustomerID]

DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmdLog_Click:
Exit Sub

Err_cmdLog_Click:
MsgBox Err.Description
Resume Exit_cmdLog_Click

End Sub

Now, in the frmLog, If a record exist for that customer, I want a new record
to display with the customer number from the frmMain to display. If the
customer does not have a record in this table, simply display a new record
with the customer number from the frmMain.

On the frmLog, I have this code:



Private Sub Form_Load()


Me![txtCustomer] = Forms!frmmain!txtCustomerID

DoCmd.RunCommand acCmdRecordsGoToNew

Me![txtCustomer] = Forms!frmmain!txtCustomerID


End Sub


This works OK, if a record exists for the customer, however if the record
does not exist, I get an error stating that the NewRecordAdd cannot occur.

Any Ideas?
 
A

Alex Dybenko

Hi,
as I understand - the only what you need to do in Load event - is to set
default value for txtCustomer:

Me![txtCustomer].defaultvalue = Forms!frmmain!txtCustomerID

You don’t need to move form to new record, it will do it automatically

--
Best regards,
___________
Alex Dybenko (MVP)
http://accessblog.net
http://www.PointLtd.com
 
S

stockton12

Thanks Alex ~

This worked great for adding new records for customers that do not have a
record. Now is there a code if the customer already has a record, that it
will go a new record.

Currently, you need to use the navigation buttons to go to a new record. I
would like to have the system automatically go to a new record regardless if
the customer has a record.

Thanks!

Alex Dybenko said:
Hi,
as I understand - the only what you need to do in Load event - is to set
default value for txtCustomer:

Me![txtCustomer].defaultvalue = Forms!frmmain!txtCustomerID

You don’t need to move form to new record, it will do it automatically

--
Best regards,
___________
Alex Dybenko (MVP)
http://accessblog.net
http://www.PointLtd.com


stockton12 said:
I have a command button on a form that will open another form. Attached is
the code:

Private Sub cmdLog_Click()
On Error GoTo Err_cmdLog_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmLog"

stLinkCriteria = "[Customer ID]=" & Me![txtCustomerID]

DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmdLog_Click:
Exit Sub

Err_cmdLog_Click:
MsgBox Err.Description
Resume Exit_cmdLog_Click

End Sub

Now, in the frmLog, If a record exist for that customer, I want a new
record
to display with the customer number from the frmMain to display. If the
customer does not have a record in this table, simply display a new record
with the customer number from the frmMain.

On the frmLog, I have this code:



Private Sub Form_Load()


Me![txtCustomer] = Forms!frmmain!txtCustomerID

DoCmd.RunCommand acCmdRecordsGoToNew

Me![txtCustomer] = Forms!frmmain!txtCustomerID


End Sub


This works OK, if a record exists for the customer, however if the record
does not exist, I get an error stating that the NewRecordAdd cannot occur.

Any Ideas?
 
A

Alex Dybenko

hi,
then you can use either DoCmd.GoToRecord , , acNext or open form like this:

DoCmd.OpenForm stDocName, , , ,acFormAdd


--
Best regards,
___________
Alex Dybenko (MVP)
http://accessblog.net
http://www.PointLtd.com


stockton12 said:
Thanks Alex ~

This worked great for adding new records for customers that do not have a
record. Now is there a code if the customer already has a record, that it
will go a new record.

Currently, you need to use the navigation buttons to go to a new record.
I
would like to have the system automatically go to a new record regardless
if
the customer has a record.

Thanks!

Alex Dybenko said:
Hi,
as I understand - the only what you need to do in Load event - is to set
default value for txtCustomer:

Me![txtCustomer].defaultvalue = Forms!frmmain!txtCustomerID

You don’t need to move form to new record, it will do it automatically

--
Best regards,
___________
Alex Dybenko (MVP)
http://accessblog.net
http://www.PointLtd.com


stockton12 said:
I have a command button on a form that will open another form. Attached
is
the code:

Private Sub cmdLog_Click()
On Error GoTo Err_cmdLog_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmLog"

stLinkCriteria = "[Customer ID]=" & Me![txtCustomerID]

DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmdLog_Click:
Exit Sub

Err_cmdLog_Click:
MsgBox Err.Description
Resume Exit_cmdLog_Click

End Sub

Now, in the frmLog, If a record exist for that customer, I want a new
record
to display with the customer number from the frmMain to display. If
the
customer does not have a record in this table, simply display a new
record
with the customer number from the frmMain.

On the frmLog, I have this code:



Private Sub Form_Load()


Me![txtCustomer] = Forms!frmmain!txtCustomerID

DoCmd.RunCommand acCmdRecordsGoToNew

Me![txtCustomer] = Forms!frmmain!txtCustomerID


End Sub


This works OK, if a record exists for the customer, however if the
record
does not exist, I get an error stating that the NewRecordAdd cannot
occur.

Any Ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top