PC Review


Reply
Thread Tools Rate Thread

Add records with VBA

 
 
=?Utf-8?B?TmVpbA==?=
Guest
Posts: n/a
 
      27th Mar 2006
I need to add some reords to my DB application and I think I will need to di
using VBA but I'm unsure how.

I have a data table that wil record about six fields worth of information
along with a serial number. The users will have to make entries in the data
table to record a block of serial numbers against specific data that's held
in the form.

So, if there is a batch of ten componets, the user will enter all the
details in the form, (And all the details will be identical for each
component) and request that a block of ten serial numbers (The next ten in
the table) be allocated.

I have set up an unbound form that asks for the relevent data, and also has
a box for the number of items in the batch, but I'm struggling to work out
how to add entries to my data table using VBA.

I relaise that Ineed to do a For..Next loop to get the required number of
entries to the table etc and I know how to check what the last numbe ris,
it's all the table entry stuff I'm stuck on.

Are there any good and easy to follow web (or paper) based resources that
explain in easy terms how I can go about this.

Thanks in advance.

Neil
 
Reply With Quote
 
 
 
 
Douglas J. Steele
Guest
Posts: n/a
 
      27th Mar 2006
The two most common approaches are to open a recordset and use the AddNew
method for each new row, or to use an INSERT INTO SQL statement:

Dim rsCurr As DAO.Recordset

Set rsCurr = CurrentDb().OpenRecordset("SELECT * FROM MyTable")
With rsCurr
.AddNew
!Field1 = MyValue
!Field2 = MyOtherValue
.Update
End With

or

Dim strSQL As String

strSQL = "INSERT INTO MyTable(Field1, Field2) " & _
"VALUES(" & MyValue & ", " & Chr$(34) & MyOtherValue & _
Chr$(34) & ")"
CurrentDb.Execute strSQL, dbFailOnError

Note that I'm assuming that Field2 is a text field, hence the need for
quotes around the value (Chr$(34) is the equivalent of ")

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"Neil" <(E-Mail Removed)> wrote in message
newsBBBC9BC-C63D-464B-96B6-(E-Mail Removed)...
>I need to add some reords to my DB application and I think I will need to
>di
> using VBA but I'm unsure how.
>
> I have a data table that wil record about six fields worth of information
> along with a serial number. The users will have to make entries in the
> data
> table to record a block of serial numbers against specific data that's
> held
> in the form.
>
> So, if there is a batch of ten componets, the user will enter all the
> details in the form, (And all the details will be identical for each
> component) and request that a block of ten serial numbers (The next ten in
> the table) be allocated.
>
> I have set up an unbound form that asks for the relevent data, and also
> has
> a box for the number of items in the batch, but I'm struggling to work out
> how to add entries to my data table using VBA.
>
> I relaise that Ineed to do a For..Next loop to get the required number of
> entries to the table etc and I know how to check what the last numbe ris,
> it's all the table entry stuff I'm stuck on.
>
> Are there any good and easy to follow web (or paper) based resources that
> explain in easy terms how I can go about this.
>
> Thanks in advance.
>
> Neil



 
Reply With Quote
 
=?Utf-8?B?TmVpbA==?=
Guest
Posts: n/a
 
      27th Mar 2006
Thanks for your help Doug, the first method worked fine (Once I realised my
error message was being caused by a missing reference).

That's the easy bit done now, all I need to do is make it user proof!

Thanks again


Neil


"Douglas J. Steele" wrote:

> The two most common approaches are to open a recordset and use the AddNew
> method for each new row, or to use an INSERT INTO SQL statement:
>
> Dim rsCurr As DAO.Recordset
>
> Set rsCurr = CurrentDb().OpenRecordset("SELECT * FROM MyTable")
> With rsCurr
> .AddNew
> !Field1 = MyValue
> !Field2 = MyOtherValue
> .Update
> End With
>
> or
>
> Dim strSQL As String
>
> strSQL = "INSERT INTO MyTable(Field1, Field2) " & _
> "VALUES(" & MyValue & ", " & Chr$(34) & MyOtherValue & _
> Chr$(34) & ")"
> CurrentDb.Execute strSQL, dbFailOnError
>
> Note that I'm assuming that Field2 is a text field, hence the need for
> quotes around the value (Chr$(34) is the equivalent of ")
>
> --
> Doug Steele, Microsoft Access MVP
> http://I.Am/DougSteele
> (no private e-mails, please)
>
>
> "Neil" <(E-Mail Removed)> wrote in message
> newsBBBC9BC-C63D-464B-96B6-(E-Mail Removed)...
> >I need to add some reords to my DB application and I think I will need to
> >di
> > using VBA but I'm unsure how.
> >
> > I have a data table that wil record about six fields worth of information
> > along with a serial number. The users will have to make entries in the
> > data
> > table to record a block of serial numbers against specific data that's
> > held
> > in the form.
> >
> > So, if there is a batch of ten componets, the user will enter all the
> > details in the form, (And all the details will be identical for each
> > component) and request that a block of ten serial numbers (The next ten in
> > the table) be allocated.
> >
> > I have set up an unbound form that asks for the relevent data, and also
> > has
> > a box for the number of items in the batch, but I'm struggling to work out
> > how to add entries to my data table using VBA.
> >
> > I relaise that Ineed to do a For..Next loop to get the required number of
> > entries to the table etc and I know how to check what the last numbe ris,
> > it's all the table entry stuff I'm stuck on.
> >
> > Are there any good and easy to follow web (or paper) based resources that
> > explain in easy terms how I can go about this.
> >
> > Thanks in advance.
> >
> > Neil

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieve records, and all records joined to those records, etc., e Peter Stone Microsoft Access Queries 2 12th Jun 2009 05:41 AM
How to Form/Subform automatically duplicate records everytime I update records. ylho Microsoft Access Forms 0 6th Jun 2006 11:48 PM
to Duane Hookom - how to use Pivot Charts for individual records instead of summing all records Harold Good Microsoft Access Reports 1 21st Dec 2005 06:26 PM
Nslookup query for NS records returns all of the NS records, but not all of the Host records Bob Microsoft Windows 2000 Networking 1 8th Nov 2004 07:03 PM
Nslookup query for NS records returns all of the NS records, but not all of the Host records Bob Microsoft Windows 2000 DNS 2 7th Nov 2004 04:42 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:21 PM.