473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can I get a value from a table field into a variable and viceversa using VBA code

I am studying a book in VBA language for beginners
and I can't see how I can get a value from a table field
and store it in a variable or get a value from a variable
and store it in a table field.

What code should I write?
And where should I put it?

I also would like to know clearer the following terms:

A procedure: Does this code always go into the events of a control
A module: (I'm not sure what it means)
An object: (I'm not sure what it means)

Thanks!!
Nov 27 '06 #1
13 31922
NeoPa
32,556 Expert Mod 16PB
I am studying a book in VBA language for beginners
and I can't see how I can get a value from a table field
and store it in a variable or get a value from a variable
and store it in a table field.

What code should I write?
And where should I put it?

I also would like to know clearer the following terms:

A procedure: Does this code always go into the events of a control
A module: (I'm not sure what it means)
An object: (I'm not sure what it means)

Thanks!!
Referring to a field in a table (or query for that matter) can only be done when you've openned the dataset (table; query) in code and have a current record.
At that point, your variable (rstThis or such like) that contains the RecordSet holds it (rstThis!FieldName or rstThis.Fields("FieldName")).
To assign the value to a variable (varThis) just say
varThis = rstThis!FieldName
A procedure is a self-contained set of code. It will generally be a method (event procedure), a function (which must return a value to the calling code or a subroutine which just runs some code.
A module is something in which you store code. Amongst other things it can contain procedures.
An object is a defined set of code and variables which, together, make up a consistent whole. Some examples of objects are Access Forms and Reports. Typically an object will have associated variables (properties) and procedures (methods).
Nov 27 '06 #2
I just understand it slightly!

Exactly, what steps should I take?
What must I do to open a dataset in code?
How can I set a record as the current record?

When you say 'your variable that contains the RecorSet holds it'
( what do you mean?)
How is this variable assigned?
What is exactly a RecordSet?

I am sorry I have so many questions.
Maybe what I want to do could be done in a different way, but
I had this idea and I am quite sure it would work if only I knew
how to do it.

I will give a brief explanation of the problem:
Let's say I have to tables:
Table1 and Table2
Table1 has a field called CurrentTax
Table2 has a field called Tax
By means of a form I set the Table2 fields.
I must get the CurrentTax value and put it in Tax
(I know that fields cannot be doubled)
But CurrentTax can change as time goes by
but the Tax field must remain with the same value

Is there another way of doing this?
I'm not sure wether this is clear enough

Thanks in advance!
Bye
Nov 27 '06 #3
PEB
1,418 Expert 1GB
Try this manner: using always VBA event procedure /CurrentTax GotFocus for exemple:
Paste this one there in conformity with your names that u use:

Me![Currenttax]=Dlookup("Tax","Table1","")

A bit more easy :)

:)
Nov 27 '06 #4
I am very new at this and it is difficult to see things clear!

I think that I need it the other way around.
Get the value in CurrentTax and put it in Tax.

Would it be similar?
Thanks!
Nov 27 '06 #5
PEB
1,418 Expert 1GB
In fact the function

Dlookup("Tax","Table1","") gets the value from table1 from column Tax

and gives it to the field Me![Currenttax] in your form.

If you want the inverse, only inverse the names...

So you have the method to catch a value from a table it's your turn!
:)
Nov 27 '06 #6
I think it is working!!
I have written this in the events of a control
I have used two fields in the same table to do some checking

Expand|Select|Wrap|Line Numbers
  1. Private Sub Inp_GotFocus()
  2. Me![Field1] = DLookup("Field2", "Table", "")
  3. End Sub
  4.  
What does Me! mean? Is it the current table or form?
If Field1 and Field 2 were in different tables,
how should I refer them?

I think that now I can work a little with this until
it is completely tuned.
Thanks very much!
Bye!
Nov 27 '06 #7
PEB
1,418 Expert 1GB
I think it is working!!
I have written this in the events of a control
I have used two fields in the same table to do some checking

Expand|Select|Wrap|Line Numbers
  1. Private Sub Inp_GotFocus()
  2. Me![Field1] = DLookup("Field2", "Table", "")
  3. End Sub
  4.  
What does Me! mean? Is it the current table or form?
If Field1 and Field 2 were in different tables,
how should I refer them?

I think that now I can work a little with this until
it is completely tuned.
Thanks very much!
Bye!
Me refers to the current active Form...

This never turns to a table!

Me replaces Forms![MyformName]
or Reports![MyreportName]
Instaed to write
Forms![Lovers on the beach]![FirstName]="Pete"
u write
Me![FirstName]="Pete"

U see the difference?
Nov 27 '06 #8
NeoPa
32,556 Expert Mod 16PB
Schultzy,

In answer to your earlier post (#3) I don't think I can provide an Access course via a web page.
I'm happy to answer questions but 'Please tell me everything I need to know about Access?' is a bit involved for here ;).
You also say :
I must get the CurrentTax value and put it in Tax
(I know that fields cannot be doubled)
But CurrentTax can change as time goes by
but the Tax field must remain with the same value
I don't understand how Tax CAN 'remain with the same value' after you've updated it with the CurrentTax.
You can't have one variable, the other static AND them both the same.

Normally, you would use something like an UPDATE query to update a single value in a recordset.
I'm not sure if this helps as I really don't follow where you're going, but if it does help then good.
Nov 27 '06 #9
Me refers to the current active Form...

This never turns to a table!

Me replaces Forms![MyformName]
or Reports![MyreportName]
Instaed to write
Forms![Lovers on the beach]![FirstName]="Pete"
u write
Me![FirstName]="Pete"

U see the difference?
I think it works perfectly right!
The DLookup function is what I needed
I am doing some checking and I have a question:

The function has three arguments
The field
The table
and the criteria
Can I use a variable in the criteria argument?
Something like this:
Me![field] = DLookup("field","table","field = variable")
Is this possible?
I'm trying and it gives me the error 2001
Thanks for everything!
Nov 28 '06 #10
NeoPa
32,556 Expert Mod 16PB
I think it works perfectly right!
The DLookup function is what I needed
I am doing some checking and I have a question:

The function has three arguments
The field
The table
and the criteria
Can I use a variable in the criteria argument?
Something like this:
Me![field] = DLookup("field","table","field = variable")
Is this possible?
I'm trying and it gives me the error 2001
Thanks for everything!
Firstly, DLookup() (and other Domain Aggregate functions) is fine for the basic logic, but can be a killer for performance with large datasets. If yours is small, don't worry about it, but if you want to support potentially large datasets then you need to be careful with its (their) use.
Secondly, you're nearly right with your DLookup() with a variable. It is possible, sort of.
You need to create the string parameter including the value from the variable. Use string manipulation (concatenation). Here's an example :
Expand|Select|Wrap|Line Numbers
  1. Me.[field] = DLookup("Field","Table","Field = " & Variable)
But bear in mind the rules of variable constants :
Numerics = as is
Strings = surrounded by (')
Dates = formatted as m/d/y and surrounded by (#).
Nov 28 '06 #11
It works fine!!!!
Everything is solved....for the moment!

But there's something I have to ask:
This is the code I finally have written and it does what I wanted

Expand|Select|Wrap|Line Numbers
  1. Me![InvoiceTax] = DLookup("ProductTax", "Products", "")
  2.  
Is it possible to write in the procedure:

Me![InvoiceTax] = [ProductTax]

The tables are related so:
Would this line get the ProductTax value and put it in InvoiceTax?
I've checked it and it seems to work but I need
to be sure there will be no drawback!

Thanks everybody for everything up to now!
Bye
Nov 29 '06 #12
NeoPa
32,556 Expert Mod 16PB
If your form contains a TextBox [ProductTax] or (I think) if the dataset that your form is bound to contains a field with that name, then you should be ok.
It would always reflect the current record of course, but that's probably what you want anyway.
Nov 30 '06 #13
I am obliged!
It is SOLVED!
THANKS EVERYBODY!
Dec 1 '06 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ralph Snart | last post by:
Is there a way to alternate table row colors without using the position() mod 2 trick? I'm in a series of nested xsl:for-each elements, about 3 deep, and I want to alternate the table row color...
2
by: MissiMaths | last post by:
I have already posted a similar question to this but the answers didn't work so I have decided to post again and hopefully I can be a little more specific about what I need. I am using access and...
4
by: Onion | last post by:
This has to be simple, but I'm forced to admit that I'm a novice who can't figure it out. I have a listbox in a form that allows multiple selections. That works fine. The problem: I can't...
2
by: Greg Strong | last post by:
Hello All, Is it possible to change table field lookup properties in code? I've been able to change other field properties in code, however so far no luck with field lookup properties. What...
3
by: zeno | last post by:
Hi All ! is it possible to use variable table and/or field names in stored procedures i.e. construct the name of a table/field based on an input parameter as in : create procedure toto(in...
2
by: mukesh | last post by:
Can we use expression in default value for a table field for example – IIf(Table-1.field-1=table-2 . field-1, table-1.field-2, 0) Interpretation – If field-1 of table -1 is like/equal to...
3
by: mckbill | last post by:
Is there a way I can direct the cursor to a specific field (variable) in a form by typing the field name while in form view? I have a form with many fields, and it would be nice if there were...
3
by: adiel_g | last post by:
Hello everyone, I am trying to move a field name to a variable in vb.net. For example, first I retrieve the record from the database and save its value: .... userGroup =...
3
by: JenniferM | last post by:
Annnnnd I'm back again, with a new problem, of course. I've got a table, TblKneeExamInfo, which corresponds to a form, FrmKneeExamInfo. In it, there is a section where I need to input patient...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...

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.