Solved

Cycle through Access public variables with common names

Posted on 2014-01-29
4
277 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 250 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 (Access MVP)
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

752 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