473,386 Members | 1,721 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,386 software developers and data experts.

Syntax to reference variables in field names

Access 2007
I have a group of button called BUTT01, BUTT02, BUTT03... BUTT99. Instead of writing code over and over for each of their On_Click events I would like to use the variable in the field name. I imagine the code would be something like:

Expand|Select|Wrap|Line Numbers
  1. private sub Butt01_click
  2. aaa=1
  3. cshort(aaa)
  4. end sub
  5.  
  6. private sub Butt02_click
  7. aaa=2
  8. cshort(aaa)
  9. end sub
  10.  
  11. private sub short(aaa)
  12. Fld="fieldname" &aaa
  13. if forms![Main]![Fld]=true then xxxx else yyyy
  14. more code options referencing combinations of aaa in field names
  15. more code options referencing aaa
  16. aaa=aaa+1
  17. more code options referencing aaa
  18. .
  19. .
  20. end sub
I'm not really explaining this very well so fingers crossed. I have tried loads of combinations to crack this but fail everytime. please help if you can.
Sep 9 '12 #1
5 4650
zmbd
5,501 Expert Mod 4TB
no, not very clear.

You are stuck writing some code for every button's onclick event... at least to call a function or sub. Why you have so many buttons... wow... that may be another topic.

However, from the example code, I think you are on the right path. As I think that you've tried to show a function/sub that could parse a passed value and either thru a select case or a series of ifthens determine what actions to take. The Function/sub could be within the form or as a standalone; however, in each case be aware of the scope of the values/field/recordsets etc...

You should have no need to record the passed value anywhere within the form controls unless you are going to do something with a bound record or a calculation etc... it is not possible to determine that from what you have provided.

-z
Sep 9 '12 #2
So how if my field name is M01BUTT01 how do I reference it.
I'm trying to do something like:

Expand|Select|Wrap|Line Numbers
  1. ccc="M" &aaa &"BUTT" &bbb
  2. me.ccc.visible=true
  3.  
the ccc bit works if I want a string but when I try and use it to reference a field name it just doesnt happen for me...

I think what I am trying to do is Concantenate a number of variables to make up field names. I just need to know the syntax
Sep 9 '12 #3
zmbd
5,501 Expert Mod 4TB
You're not helping us help you.

Controls either are bound or unbound.
The name of the control is usually either the same as the bound field in the recordset or as assigned by the programmer.
I've never seen a command button bound to a record source.

Concantation for VBA is the same as for any BASIC language... use the plus or ampersign.

The "code" you've shown so far is not helpful... show us your actual code.

-z
Sep 9 '12 #4
Rabbit
12,516 Expert Mod 8TB
You can use the Me.Controls() collection to access a control by its name. Or you can just pass the control directly to the sub.
Sep 10 '12 #5
twinnyfo
3,653 Expert Mod 2GB
daovert99,

On the button's On Click event, you can also set it to (to use your example)

Expand|Select|Wrap|Line Numbers
  1. = cshort(aaa)
  2.  
and the function will execute based on the value you send to it. I have a form with 60 buttons on it, and this is how I save tons of code........
Sep 10 '12 #6

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

Similar topics

4
by: Michael Flanagan | last post by:
(Bottom line: I think what I'm looking for is an easy way of changing the case of key values in an array.) I've got code that I'm trying to make agnostic about the underlying database system I'm...
4
by: cppaddict | last post by:
I understand that there are a couple differences between reference variables and pointer: 1) reference variables *must* be initialized. 2) You cannot change what a reference variable refers...
3
by: Tom | last post by:
Data is collected over time in an Excel worksheet with 20 columns. The Excel worksheet starts out as a copy of a template as is filled in as data is collected. Eventually the worksheet file is...
2
by: Michael Bulatovich | last post by:
I have a simple db to keep track of work/time/projects etc. It has two fields (column) named "start time" and "end time" WITH THE SPACES. I'm trying to do some automation to a form associated with...
2
by: R Schlageter | last post by:
Several years ago I wrote a database which keeps track of our campus ministry participation ... I'm no expert but it keeps working well The database is divided front and back end. Now we need to...
3
by: Michael | last post by:
Hi everyone, I am trying to change the field names for a table that is being exported via Excel. Its a spreadsheet that our National Office sends us but even after promise after promise they...
5
by: Chaos | last post by:
Is It possible to have reference variables like in PHP ex. <?php $x = 1; $y =& $x; $y += 1;
3
by: Billy | last post by:
I do a SELECT * from table command in an ASP page to build a text file out on our server, but the export is not to allow a field name rows of records. The first thing I get is a row with all the...
1
by: Billy | last post by:
I do a SELECT * from table command in an ASP page to build a text file out on our server, but the export is not to allow a field name rows of records. The first thing I get is a row with all the...
1
by: Cainlynk | last post by:
I am trying to create a passthrough query in Access using VBA code. I have done this several times before successfully, but in this instance, I have run up against a wall due to a field name that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.