PC Review


Reply
Thread Tools Rate Thread

how do I change a field's value on a selected record using VBA?

 
 
=?Utf-8?B?TGVvQg==?=
Guest
Posts: n/a
 
      24th Mar 2006
I have a table that contains the names of other tables in my database and
when a new table is added, i'd like to add its name to the list as part of a
VBA module. Seems a pretty basic function, but I can't find any instruction
on how to do it in the Help pages.

I've tried things like:
Recordsets!SubRateCards.Fields("RateTable") = NewName

But this just gets me an error message saying that an object is required.
 
Reply With Quote
 
 
 
 
TC
Guest
Posts: n/a
 
      24th Mar 2006

(UNTESTED)

dim sql as string, v1 as string, v2 as integer
v1 = "abc" ' value for field F1.
v2 = 99 ' value for field F2.
sql = "INSERT INTO MyTable (F1, F2) VALUES (""" & v1 & """, " & v2 &
")"
msgbox sql
dbengine(0)(0).execute sql, dbfailonerror

- or -

with dbengine(0)(0).openrecordset("MyTable", dbopendynaset)
.addnew
![F1] = v1
![F2] = v2
.update
end with

HTH,
TC (MVP Access)
http://tc2.atspace.com

 
Reply With Quote
 
=?Utf-8?B?TUphdEFmbGFj?=
Guest
Posts: n/a
 
      24th Mar 2006
I would do something like this:

dim db as database
dim tdf as tabledef
dim rst as recordset
set db = currentdb()
set tdf = db.tabledefs("SubRateCard")
set rst = tdf.openrecordset(dbopendynaset)
with rst
..addnew
![RateTable] = NewName
..update
end with

Hope this helps
--
Michal Joyce
Project Management IS Analyst
Aflac - Project Management Office


"LeoB" wrote:

> I have a table that contains the names of other tables in my database and
> when a new table is added, i'd like to add its name to the list as part of a
> VBA module. Seems a pretty basic function, but I can't find any instruction
> on how to do it in the Help pages.
>
> I've tried things like:
> Recordsets!SubRateCards.Fields("RateTable") = NewName
>
> But this just gets me an error message saying that an object is required.

 
Reply With Quote
 
=?Utf-8?B?TGVvQg==?=
Guest
Posts: n/a
 
      24th Mar 2006
Hmm. I'm getting a Type Mismatch error when it tries to open the recordset
with that. I tried with db as the object instead of tdf and got the same.

"MJatAflac" wrote:

> I would do something like this:
>
> dim db as database
> dim tdf as tabledef
> dim rst as recordset
> set db = currentdb()
> set tdf = db.tabledefs("SubRateCard")
> set rst = tdf.openrecordset(dbopendynaset)
> with rst
> .addnew
> ![RateTable] = NewName
> .update
> end with
>
> Hope this helps
> --
> Michal Joyce
> Project Management IS Analyst
> Aflac - Project Management Office
>
>
> "LeoB" wrote:
>
> > I have a table that contains the names of other tables in my database and
> > when a new table is added, i'd like to add its name to the list as part of a
> > VBA module. Seems a pretty basic function, but I can't find any instruction
> > on how to do it in the Help pages.
> >
> > I've tried things like:
> > Recordsets!SubRateCards.Fields("RateTable") = NewName
> >
> > But this just gets me an error message saying that an object is required.

 
Reply With Quote
 
=?Utf-8?B?TUphdEFmbGFj?=
Guest
Posts: n/a
 
      24th Mar 2006
Don't know for sure but it sounds like the data you are trying to insert into
the table is not of the same datatype as the field you are trying to put it
into.

In my example the name of the table is SubRateCard and the name of the
column in the table is RateTable

regards,
--
Michal Joyce
Project Management IS Analyst
Aflac - Project Management Office


"LeoB" wrote:

> Hmm. I'm getting a Type Mismatch error when it tries to open the recordset
> with that. I tried with db as the object instead of tdf and got the same.
>
> "MJatAflac" wrote:
>
> > I would do something like this:
> >
> > dim db as database
> > dim tdf as tabledef
> > dim rst as recordset
> > set db = currentdb()
> > set tdf = db.tabledefs("SubRateCard")
> > set rst = tdf.openrecordset(dbopendynaset)
> > with rst
> > .addnew
> > ![RateTable] = NewName
> > .update
> > end with
> >
> > Hope this helps
> > --
> > Michal Joyce
> > Project Management IS Analyst
> > Aflac - Project Management Office
> >
> >
> > "LeoB" wrote:
> >
> > > I have a table that contains the names of other tables in my database and
> > > when a new table is added, i'd like to add its name to the list as part of a
> > > VBA module. Seems a pretty basic function, but I can't find any instruction
> > > on how to do it in the Help pages.
> > >
> > > I've tried things like:
> > > Recordsets!SubRateCards.Fields("RateTable") = NewName
> > >
> > > But this just gets me an error message saying that an object is required.

 
Reply With Quote
 
=?Utf-8?B?TGVvQg==?=
Guest
Posts: n/a
 
      24th Mar 2006
Thanks, Michal, but I don't think it can be that, as the thing grinds to a
halt before it gets to that point - stops at the OpenRecordset statement. I
can't see what the problem is from reading the help pages.

rgds,

Leo

"MJatAflac" wrote:

> Don't know for sure but it sounds like the data you are trying to insert into
> the table is not of the same datatype as the field you are trying to put it
> into.
>
> In my example the name of the table is SubRateCard and the name of the
> column in the table is RateTable
>
> regards,
> --
> Michal Joyce
> Project Management IS Analyst
> Aflac - Project Management Office
>
>
> "LeoB" wrote:
>
> > Hmm. I'm getting a Type Mismatch error when it tries to open the recordset
> > with that. I tried with db as the object instead of tdf and got the same.
> >
> > "MJatAflac" wrote:
> >
> > > I would do something like this:
> > >
> > > dim db as database
> > > dim tdf as tabledef
> > > dim rst as recordset
> > > set db = currentdb()
> > > set tdf = db.tabledefs("SubRateCard")
> > > set rst = tdf.openrecordset(dbopendynaset)
> > > with rst
> > > .addnew
> > > ![RateTable] = NewName
> > > .update
> > > end with
> > >
> > > Hope this helps
> > > --
> > > Michal Joyce
> > > Project Management IS Analyst
> > > Aflac - Project Management Office
> > >
> > >
> > > "LeoB" wrote:
> > >
> > > > I have a table that contains the names of other tables in my database and
> > > > when a new table is added, i'd like to add its name to the list as part of a
> > > > VBA module. Seems a pretty basic function, but I can't find any instruction
> > > > on how to do it in the Help pages.
> > > >
> > > > I've tried things like:
> > > > Recordsets!SubRateCards.Fields("RateTable") = NewName
> > > >
> > > > But this just gets me an error message saying that an object is required.

 
Reply With Quote
 
Douglas J. Steele
Guest
Posts: n/a
 
      24th Mar 2006
Try changing your declaration to

dim rst as DAO.recordset

I'm assuming that you've got references set to both the Microsoft DAO 3.6
Object Library and the Microsoft ActiveX Data Objects 2.1 Library, and the
ADO one is higher in the list (With any code module open, select Tools |
References from the menu bar to see what I'm talking about)

When you have both references, you'll find that you need to "disambiguate"
certain declarations, because objects with the same names exist in the 2
models. For example, to ensure that you get a DAO recordset, you'll need to
use Dim rsCurr as DAO.Recordset (to guarantee an ADO recordset, you'd use
Dim rsCurr As ADODB.Recordset)

The list of objects with the same names in the 2 models is Connection,
Error, Errors, Field, Fields, Parameter, Parameters, Property, Properties
and Recordset

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


"LeoB" <(E-Mail Removed)> wrote in message
news:6C1EA01D-D0AE-472B-8255-(E-Mail Removed)...
> Thanks, Michal, but I don't think it can be that, as the thing grinds to a
> halt before it gets to that point - stops at the OpenRecordset statement.
> I
> can't see what the problem is from reading the help pages.
>
> rgds,
>
> Leo
>
> "MJatAflac" wrote:
>
>> Don't know for sure but it sounds like the data you are trying to insert
>> into
>> the table is not of the same datatype as the field you are trying to put
>> it
>> into.
>>
>> In my example the name of the table is SubRateCard and the name of the
>> column in the table is RateTable
>>
>> regards,
>> --
>> Michal Joyce
>> Project Management IS Analyst
>> Aflac - Project Management Office
>>
>>
>> "LeoB" wrote:
>>
>> > Hmm. I'm getting a Type Mismatch error when it tries to open the
>> > recordset
>> > with that. I tried with db as the object instead of tdf and got the
>> > same.
>> >
>> > "MJatAflac" wrote:
>> >
>> > > I would do something like this:
>> > >
>> > > dim db as database
>> > > dim tdf as tabledef
>> > > dim rst as recordset
>> > > set db = currentdb()
>> > > set tdf = db.tabledefs("SubRateCard")
>> > > set rst = tdf.openrecordset(dbopendynaset)
>> > > with rst
>> > > .addnew
>> > > ![RateTable] = NewName
>> > > .update
>> > > end with
>> > >
>> > > Hope this helps
>> > > --
>> > > Michal Joyce
>> > > Project Management IS Analyst
>> > > Aflac - Project Management Office
>> > >
>> > >
>> > > "LeoB" wrote:
>> > >
>> > > > I have a table that contains the names of other tables in my
>> > > > database and
>> > > > when a new table is added, i'd like to add its name to the list as
>> > > > part of a
>> > > > VBA module. Seems a pretty basic function, but I can't find any
>> > > > instruction
>> > > > on how to do it in the Help pages.
>> > > >
>> > > > I've tried things like:
>> > > > Recordsets!SubRateCards.Fields("RateTable") = NewName
>> > > >
>> > > > But this just gets me an error message saying that an object is
>> > > > required.



 
Reply With Quote
 
=?Utf-8?B?TUphdEFmbGFj?=
Guest
Posts: n/a
 
      24th Mar 2006
Hmmm The code I sent you makes use of DAO as opposed to ADO that could be the
problem I suppose. This type of code works for me all the time doing exactly
what you are trying to do.

Check to see if you have a reference to Microsoft DAO 3.51 Object Library in
your database.

In case you don't know how to do this. Open any module and click on
Tools>References and then scroll through the list and then select the
reference you need to add.

This might solve the problem otherwise I think it will take one of the MVP's
to answer this.

My only other thought is where are you getting the new table name from?
Michal Joyce
Project Management IS Analyst
Aflac - Project Management Office


"LeoB" wrote:

> Thanks, Michal, but I don't think it can be that, as the thing grinds to a
> halt before it gets to that point - stops at the OpenRecordset statement. I
> can't see what the problem is from reading the help pages.
>
> rgds,
>
> Leo
>
> "MJatAflac" wrote:
>
> > Don't know for sure but it sounds like the data you are trying to insert into
> > the table is not of the same datatype as the field you are trying to put it
> > into.
> >
> > In my example the name of the table is SubRateCard and the name of the
> > column in the table is RateTable
> >
> > regards,
> > --
> > Michal Joyce
> > Project Management IS Analyst
> > Aflac - Project Management Office
> >
> >
> > "LeoB" wrote:
> >
> > > Hmm. I'm getting a Type Mismatch error when it tries to open the recordset
> > > with that. I tried with db as the object instead of tdf and got the same.
> > >
> > > "MJatAflac" wrote:
> > >
> > > > I would do something like this:
> > > >
> > > > dim db as database
> > > > dim tdf as tabledef
> > > > dim rst as recordset
> > > > set db = currentdb()
> > > > set tdf = db.tabledefs("SubRateCard")
> > > > set rst = tdf.openrecordset(dbopendynaset)
> > > > with rst
> > > > .addnew
> > > > ![RateTable] = NewName
> > > > .update
> > > > end with
> > > >
> > > > Hope this helps
> > > > --
> > > > Michal Joyce
> > > > Project Management IS Analyst
> > > > Aflac - Project Management Office
> > > >
> > > >
> > > > "LeoB" wrote:
> > > >
> > > > > I have a table that contains the names of other tables in my database and
> > > > > when a new table is added, i'd like to add its name to the list as part of a
> > > > > VBA module. Seems a pretty basic function, but I can't find any instruction
> > > > > on how to do it in the Help pages.
> > > > >
> > > > > I've tried things like:
> > > > > Recordsets!SubRateCards.Fields("RateTable") = NewName
> > > > >
> > > > > But this just gets me an error message saying that an object is required.

 
Reply With Quote
 
=?Utf-8?B?TUphdEFmbGFj?=
Guest
Posts: n/a
 
      24th Mar 2006
They should pay you guys Douglas!
--
Michal Joyce
Project Management IS Analyst
Aflac - Project Management Office


"Douglas J. Steele" wrote:

> Try changing your declaration to
>
> dim rst as DAO.recordset
>
> I'm assuming that you've got references set to both the Microsoft DAO 3.6
> Object Library and the Microsoft ActiveX Data Objects 2.1 Library, and the
> ADO one is higher in the list (With any code module open, select Tools |
> References from the menu bar to see what I'm talking about)
>
> When you have both references, you'll find that you need to "disambiguate"
> certain declarations, because objects with the same names exist in the 2
> models. For example, to ensure that you get a DAO recordset, you'll need to
> use Dim rsCurr as DAO.Recordset (to guarantee an ADO recordset, you'd use
> Dim rsCurr As ADODB.Recordset)
>
> The list of objects with the same names in the 2 models is Connection,
> Error, Errors, Field, Fields, Parameter, Parameters, Property, Properties
> and Recordset
>
> --
> Doug Steele, Microsoft Access MVP
> http://I.Am/DougSteele
> (no private e-mails, please)
>
>
> "LeoB" <(E-Mail Removed)> wrote in message
> news:6C1EA01D-D0AE-472B-8255-(E-Mail Removed)...
> > Thanks, Michal, but I don't think it can be that, as the thing grinds to a
> > halt before it gets to that point - stops at the OpenRecordset statement.
> > I
> > can't see what the problem is from reading the help pages.
> >
> > rgds,
> >
> > Leo
> >
> > "MJatAflac" wrote:
> >
> >> Don't know for sure but it sounds like the data you are trying to insert
> >> into
> >> the table is not of the same datatype as the field you are trying to put
> >> it
> >> into.
> >>
> >> In my example the name of the table is SubRateCard and the name of the
> >> column in the table is RateTable
> >>
> >> regards,
> >> --
> >> Michal Joyce
> >> Project Management IS Analyst
> >> Aflac - Project Management Office
> >>
> >>
> >> "LeoB" wrote:
> >>
> >> > Hmm. I'm getting a Type Mismatch error when it tries to open the
> >> > recordset
> >> > with that. I tried with db as the object instead of tdf and got the
> >> > same.
> >> >
> >> > "MJatAflac" wrote:
> >> >
> >> > > I would do something like this:
> >> > >
> >> > > dim db as database
> >> > > dim tdf as tabledef
> >> > > dim rst as recordset
> >> > > set db = currentdb()
> >> > > set tdf = db.tabledefs("SubRateCard")
> >> > > set rst = tdf.openrecordset(dbopendynaset)
> >> > > with rst
> >> > > .addnew
> >> > > ![RateTable] = NewName
> >> > > .update
> >> > > end with
> >> > >
> >> > > Hope this helps
> >> > > --
> >> > > Michal Joyce
> >> > > Project Management IS Analyst
> >> > > Aflac - Project Management Office
> >> > >
> >> > >
> >> > > "LeoB" wrote:
> >> > >
> >> > > > I have a table that contains the names of other tables in my
> >> > > > database and
> >> > > > when a new table is added, i'd like to add its name to the list as
> >> > > > part of a
> >> > > > VBA module. Seems a pretty basic function, but I can't find any
> >> > > > instruction
> >> > > > on how to do it in the Help pages.
> >> > > >
> >> > > > I've tried things like:
> >> > > > Recordsets!SubRateCards.Fields("RateTable") = NewName
> >> > > >
> >> > > > But this just gets me an error message saying that an object is
> >> > > > required.

>
>
>

 
Reply With Quote
 
=?Utf-8?B?TGVvQg==?=
Guest
Posts: n/a
 
      24th Mar 2006
Thanks, guys!

Leo

"MJatAflac" wrote:

> They should pay you guys Douglas!
> --
> Michal Joyce
> Project Management IS Analyst
> Aflac - Project Management Office
>
>
> "Douglas J. Steele" wrote:
>
> > Try changing your declaration to
> >
> > dim rst as DAO.recordset
> >
> > I'm assuming that you've got references set to both the Microsoft DAO 3.6
> > Object Library and the Microsoft ActiveX Data Objects 2.1 Library, and the
> > ADO one is higher in the list (With any code module open, select Tools |
> > References from the menu bar to see what I'm talking about)
> >
> > When you have both references, you'll find that you need to "disambiguate"
> > certain declarations, because objects with the same names exist in the 2
> > models. For example, to ensure that you get a DAO recordset, you'll need to
> > use Dim rsCurr as DAO.Recordset (to guarantee an ADO recordset, you'd use
> > Dim rsCurr As ADODB.Recordset)
> >
> > The list of objects with the same names in the 2 models is Connection,
> > Error, Errors, Field, Fields, Parameter, Parameters, Property, Properties
> > and Recordset
> >
> > --
> > Doug Steele, Microsoft Access MVP
> > http://I.Am/DougSteele
> > (no private e-mails, please)
> >
> >
> > "LeoB" <(E-Mail Removed)> wrote in message
> > news:6C1EA01D-D0AE-472B-8255-(E-Mail Removed)...
> > > Thanks, Michal, but I don't think it can be that, as the thing grinds to a
> > > halt before it gets to that point - stops at the OpenRecordset statement.
> > > I
> > > can't see what the problem is from reading the help pages.
> > >
> > > rgds,
> > >
> > > Leo
> > >
> > > "MJatAflac" wrote:
> > >
> > >> Don't know for sure but it sounds like the data you are trying to insert
> > >> into
> > >> the table is not of the same datatype as the field you are trying to put
> > >> it
> > >> into.
> > >>
> > >> In my example the name of the table is SubRateCard and the name of the
> > >> column in the table is RateTable
> > >>
> > >> regards,
> > >> --
> > >> Michal Joyce
> > >> Project Management IS Analyst
> > >> Aflac - Project Management Office
> > >>
> > >>
> > >> "LeoB" wrote:
> > >>
> > >> > Hmm. I'm getting a Type Mismatch error when it tries to open the
> > >> > recordset
> > >> > with that. I tried with db as the object instead of tdf and got the
> > >> > same.
> > >> >
> > >> > "MJatAflac" wrote:
> > >> >
> > >> > > I would do something like this:
> > >> > >
> > >> > > dim db as database
> > >> > > dim tdf as tabledef
> > >> > > dim rst as recordset
> > >> > > set db = currentdb()
> > >> > > set tdf = db.tabledefs("SubRateCard")
> > >> > > set rst = tdf.openrecordset(dbopendynaset)
> > >> > > with rst
> > >> > > .addnew
> > >> > > ![RateTable] = NewName
> > >> > > .update
> > >> > > end with
> > >> > >
> > >> > > Hope this helps
> > >> > > --
> > >> > > Michal Joyce
> > >> > > Project Management IS Analyst
> > >> > > Aflac - Project Management Office
> > >> > >
> > >> > >
> > >> > > "LeoB" wrote:
> > >> > >
> > >> > > > I have a table that contains the names of other tables in my
> > >> > > > database and
> > >> > > > when a new table is added, i'd like to add its name to the list as
> > >> > > > part of a
> > >> > > > VBA module. Seems a pretty basic function, but I can't find any
> > >> > > > instruction
> > >> > > > on how to do it in the Help pages.
> > >> > > >
> > >> > > > I've tried things like:
> > >> > > > Recordsets!SubRateCards.Fields("RateTable") = NewName
> > >> > > >
> > >> > > > But this just gets me an error message saying that an object is
> > >> > > > required.

> >
> >
> >

 
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
Re: Increment number in field when new record is selected Ken Snell [MVP] Microsoft Access Form Coding 0 25th May 2009 10:31 PM
Acquiring a field value from a currently selected record in a subf Dan_S Microsoft Access Forms 6 23rd Mar 2009 03:21 PM
change colors on selected record =?Utf-8?B?Umljb3ktQ2hpY2Fnbw==?= Microsoft Access Forms 1 5th Aug 2005 08:46 PM
Determining Field Value for Selected Record Kim P Microsoft Access Form Coding 1 13th Jan 2004 05:31 AM
Find field in selected record Ginger Microsoft Access Database Table Design 3 19th Aug 2003 03:57 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:32 AM.