Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cycle through Access public variables with common names

Posted on 2014-01-29
4
Medium Priority
?
286 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
  • 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 49

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

926 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