Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cycle through Access public variables with common names

Posted on 2014-01-29
4
Medium Priority
?
282 Views
Last Modified: 2014-01-30
I have a form with a series of common named controls that I want to update to the storerd values in similarly named public globals.

I want to set this up as a "for" loop  as I have a lot of field variables and have the syntax for the form fields as:
For i = 1 To 10
        Me("txtTextBox" & i).Value
    Next i

How do I structure global variable?
  The below is kind of what I want but obviously is incorrect syntax
Me("txtTextBox" & i).Value = pubText & i
0
Comment
Question by:ynot8669
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 750 total points
ID: 39820023
Declare your public variables as an array:

Public pubText(1 to 10) As String

Open in new window

This gives you 10 variables called pubText(1)...pubText(10)

Use them the following way:

Me("txtTextBox" & i).Value = pubText(i)

Open in new window

0
 

Author Closing Comment

by:ynot8669
ID: 39820059
Great, you have confirmed where my thinking was going.

I couldn't make any other syntax work ........and tried quite few.

Thank You :)
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 39820659
The other option, depending on the version of Access you are using, is to use TempVars.

TempVars is a collection of variables that you do not need to declare (dimension) and which can contain virtually anything.  To create a tempvar, simply use the syntax:

Tempvar.Add  "variableName", value

Note: I've also found that you can simply do:Tempvar!variableName = x

Then, you could use either of the following to refer to that variable.

TempVars!strAppName
TempVars.Item("strAppName")
TempVars.Item(0)

Advantages of tempvars are:
a.  the collection retains its values, even outside the scope of your code, even after encountering an unhandled error.
b.  You can actually refer to the variable by name
c.  you can use them in queries, which you cannot do directly with public variables.  I find this valuable in that I don't have to refer to form controls in my queries.  In the after update event of a control, say txtStartDate, I'll simply set the value of the tempvar:

Private sub txtStartDate_AfterUpdate

    if me.txtStartDate & "" = "" then
        'do nothing
   else
        Tempvars!StartDate = cdate(me.txtStartDate.Value)
   end if

End Sub

Then, in my queries, I can refer to tempvar!StartDate rather than referring to a form field.  This gives me the ability to set tempvars in the immediate window and test queries that would otherwise return nothing or use a popup for data entry.

Hope this helps.
0
 

Author Comment

by:ynot8669
ID: 39822769
Thanks Dale, I have used TempVars for most global variables but wasn't aware of the .item(x) access method.

I'm assuming you can vary (x) within a loop, if so it may well work out to be the way forward.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question