Solved

Cycle through Access public variables with common names

Posted on 2014-01-29
4
275 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 47

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Database 5 44
Slow running query - help 16 36
Copy all value in one form to another 4 39
Troubleshooting Save_Record_Click() VBA 6 18
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

763 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