473,847 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use VBA to insert a new record in a subform?

How do I use VBA to insert a *new* record into a subform?

I have a master form with client information, and a sub form with billing
information. I want to click on a button ("Add Data") and have a record
inserted into the subform's table, with date created and some information
from the main table inserted into the proper fields in the subform.

After the record is inserted, the data entry operator enters a Date Of
Service and a billing code.

I've created code to copy the the information I need from the main table and
place it in the proper fields, but it always goes to the first record in the
subform and overwrites the existing data. I have to manually enter the
subform and move to a new record, then click on the "Add Data" button, it
then inserts the data on the new record line.

Thanks,

Scott
Nov 12 '05 #1
4 65655
You could:
- set focus to the subform control;
- set focus to a control within the subform;
- move to a new record;
- write the values.
With Me.[NameOfYourSubfo rmControlHere]
.Form.SetFocus
.Form![SomeControl].SetFocus
RunCommand acCmdRecordsGot oNew
.Form!SomeContr ol = SomeValue
.Form!AnotherCo ntrol = AnotherValue
'etc.
End With

Alternatively, you could add the record to the RecordsetClone of the
subform:
With Me.[NameOfYourSubfo rmControlHere].Form.Recordset Clone
.AddNew
!SomeControl = SomeValue
!AnotherContorl = AnotherValue
'etc.
.Update
End With

Use the first approach if you want the user to be able to complete or back
out of saving the record, and the 2nd approach if you want to record saved
regardless of what the user does.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"YFS DBA" <Ny**********@p rexar.com> wrote in message
news:vp******** ****@corp.super news.com...
How do I use VBA to insert a *new* record into a subform?

I have a master form with client information, and a sub form with billing
information. I want to click on a button ("Add Data") and have a record
inserted into the subform's table, with date created and some information
from the main table inserted into the proper fields in the subform.

After the record is inserted, the data entry operator enters a Date Of
Service and a billing code.

I've created code to copy the the information I need from the main table and place it in the proper fields, but it always goes to the first record in the subform and overwrites the existing data. I have to manually enter the
subform and move to a new record, then click on the "Add Data" button, it
then inserts the data on the new record line.

Thanks,

Scott

Nov 12 '05 #2
Hi Allen, thanks for the help!

It works, except when I don't have any existing records, then the
"RunCommand acCmdRecordsGoT oNew" command is unavailable.

I think I should be able to use a DCount function to see if any records
exist in the subform, if not, then the sub wouldn't use the RunCommand
acCmdRecordsGoT oNew command.

Any ideas on how to count the records in the subform? Or is there an easier
(or smarter!) way?

Thanks again,

Scott

"Allen Browne" <al*********@Se eSig.invalid> wrote in message
news:3f******** *************** @freenews.iinet .net.au...
You could:
- set focus to the subform control;
- set focus to a control within the subform;
- move to a new record;
- write the values.
With Me.[NameOfYourSubfo rmControlHere]
.Form.SetFocus
.Form![SomeControl].SetFocus
RunCommand acCmdRecordsGot oNew
.Form!SomeContr ol = SomeValue
.Form!AnotherCo ntrol = AnotherValue
'etc.
End With

Alternatively, you could add the record to the RecordsetClone of the
subform:
With Me.[NameOfYourSubfo rmControlHere].Form.Recordset Clone
.AddNew
!SomeControl = SomeValue
!AnotherContorl = AnotherValue
'etc.
.Update
End With

Use the first approach if you want the user to be able to complete or back
out of saving the record, and the 2nd approach if you want to record saved
regardless of what the user does.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

Nov 12 '05 #3
Disregard message about counting records and disabling Add New Record, I
figured out how to use the Count function.! Life is good!

: )

Scott
"Allen Browne" <al*********@Se eSig.invalid> wrote in message
news:3f******** *************** @freenews.iinet .net.au...
You could:
- set focus to the subform control;
- set focus to a control within the subform;
- move to a new record;
- write the values.
With Me.[NameOfYourSubfo rmControlHere]
.Form.SetFocus
.Form![SomeControl].SetFocus
RunCommand acCmdRecordsGot oNew
.Form!SomeContr ol = SomeValue
.Form!AnotherCo ntrol = AnotherValue
'etc.
End With

Alternatively, you could add the record to the RecordsetClone of the
subform:
With Me.[NameOfYourSubfo rmControlHere].Form.Recordset Clone
.AddNew
!SomeControl = SomeValue
!AnotherContorl = AnotherValue
'etc.
.Update
End With

Use the first approach if you want the user to be able to complete or back
out of saving the record, and the 2nd approach if you want to record saved
regardless of what the user does.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"YFS DBA" <Ny**********@p rexar.com> wrote in message
news:vp******** ****@corp.super news.com...
How do I use VBA to insert a *new* record into a subform?

I have a master form with client information, and a sub form with billing information. I want to click on a button ("Add Data") and have a record
inserted into the subform's table, with date created and some information from the main table inserted into the proper fields in the subform.

After the record is inserted, the data entry operator enters a Date Of
Service and a billing code.

I've created code to copy the the information I need from the main table

and
place it in the proper fields, but it always goes to the first record in

the
subform and overwrites the existing data. I have to manually enter the
subform and move to a new record, then click on the "Add Data" button, it then inserts the data on the new record line.

Thanks,

Scott


Nov 12 '05 #4
Vozone
1 New Member
Hi. I was reading this thread, hoping to be able to replicate what you explain to a database of my own.
I became rather confused by what you mean by a subform control and a control within a subform...

My Form is named Movimentacao, my subform HistoriaSubform , I have a control button, 7 fields in my subform, and 6 text boxes from which I want to update my data.
Which is which...

Thabnk You
Jun 21 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2173
by: Paul | last post by:
Hi all Just a quick question more about the flow of an asp page. IF I have code as below: - MyDBConnection.Execute "Insert Record Into A Table" MyRecordset.Open "Retreive the same record from the table" Is there any chance that the connection process (Inserting the record) will not have completed by the time I try to retrieve the record. I.E. will the
1
1729
by: jaYPee | last post by:
i'm wondering how can i insert record into my sql server 2000. i have already created a connection to my sql server 2000 using the wizard. i added 4 sqldataadapter and 1 dataset. because i am an expert in ms access i created this first in access and i have done this in a couple of minutes. i have not already added the RegularLoad table and RegularLoad Details table cause i'm wondering how to do it? in my ms access program i have this...
10
2648
by: jaYPee | last post by:
as of now i am using a stored procedure from my sql server 2000 to insert record from another table. what i need now is on how can i insert record by not using the stored procedure and insert it using dataset. here is my code in stored procedure.. CREATE PROCEDURE AddRegularLoad @SchYrSemID as int, @ProgramID as int, @Sem as varchar(50), @Year as
1
2041
by: swatijogdand | last post by:
Hi all, I want to insert a record at a time from front end i.e. vba in a table located on sql server. But, one field on that table has an 'identity' constraint which is not enabling me to insert record . Please help me. Regards, Swati.
0
2163
ak1dnar
by: ak1dnar | last post by:
There is a Error getting while i am entering records using this jsp file. <%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %> <%@ include file="../Connections/conn.jsp" %> <% // *** Edit Operations: declare variables // set the form action variable String MM_editAction = request.getRequestURI(); if (request.getQueryString() != null && request.getQueryString().length() > 0) {
2
2528
by: guessvic | last post by:
Hello everyone, Does anyone know why after using the insert record function that Dreamweaver provides for ASP JavaScript page, then you CANNOT pass the value in the text field inside the form to another page?? Please help. Vic.
1
3034
by: sonydev | last post by:
i want know how can insert record into access database by inserting it to datagridview, plz reply me with simle code. thank you
10
3750
by: oracle newbie | last post by:
How to create a pl sql program which enable to insert record into database? Can anyone can give details or steps in creating a pl sql program which enable to insert record into database using the Oracle SQL Developer program? Please help me.... Thank you!!
3
2596
by: Rohullah | last post by:
I want to insert record into two table at a time. For example i have two table student1 and student2 with the same structure now i want to insert record into student1 the record of student1 should automiticlly insert into studendt2 table.
0
1616
by: Sanjeewa Dissa | last post by:
I used following code to insert record to foxpro dbf. But it throws an exception "Command contains unrecognized phrase/keyword." How can I fixed this. string strLogConnectionString = @"Provider=vfpoledb;Data Source=E:\DBF\fLog.dbf;Collating Sequence=machine;Mode=ReadWrite;"; OleDbConnection strConLog = new OleDbConnection(strLogConnectionString); strConLog.Open(); OleDbCommand oComm = new OleDbCommand("Insert into...
0
10647
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10706
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10338
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9481
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7882
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7056
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5719
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5910
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4119
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.