Results 1 to 7 of 7
  1. #1
    natilton is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    7

    Array of variables in a survey database

    Hello,

    I'm *very* new to VBA, and I'm not sure of how to accomplish a task that I think would warrant the use of an array. This would be an array of survey questions, MUL1_05 through MUL1_33, all of which I've already defined in the table design.

    What I'd like to do is disable certain fields and set their values on the form (the MUL1 variables) based on the subject's age.

    So when an ID is entered, I have the program look up the child's age from the enrollment table. Then, if the child is between 5 and 8 months, they start with question MUL1_05, and nothing is disabled. If they are between 9 to 12 months, they start with MUL1_08, while 05, 06, and 07 are disabled, and their values are set to 1.

    I'd like to have an array so that I can have the program loop through the variables that need to be disabled, instead of having to specify each variable.

    I have an AfterUpdate procedure

    Private Sub CaseID_AfterUpdate
    AgeMon = DLookup (.....) / I know how to do this part

    If (AgeMon >= 9 and AgeMon <= 12) Then
    Me.MUL1_05.Enabled = False
    Me.MUL1_05.Value = 1

    and so on.


    Any help would be much appreciated

    Thanks,
    Nick

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Since the controls are similarly named you can use the Controls collection in a loop. Since you know how many controls have this name structure, try:

    For i = 1 to 33
    Me.Controls("Mul1_" & IIf(i<10, "0", "") & i).Enabled = False
    Next

    Expand on this to include other conditional code to consider the age criteria.

    http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
    Last edited by June7; 10-20-2011 at 12:56 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    natilton is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    7
    Thanks! That seems much easier to implement than what I was envisioning.

  4. #4
    natilton is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    7
    Quote Originally Posted by June7 View Post
    Since the controls are similarly named you can use the

    For i = 1 to 33
    Me.Controls("Mul1_" & IIf(i<10, & "0" & i)).Enabled = False
    Next
    This worked like a charm. Thanks again!

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Ooops! Slight error in my suggestion and I edited post.
    Use:
    Me.Controls("Mul1_" & IIf(i<10, "0", "") & i).Enabled = False
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    natilton is offline Novice
    Windows XP Access 2007
    Join Date
    Oct 2011
    Posts
    7
    I just adjusted it slightly:

    Me.Controls(IFF(n < 10, "MUL1_0", "MUL1_") & n).Enabled = False

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    The function is IIf not IFF.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Report from survey responses
    By Saramj in forum Reports
    Replies: 0
    Last Post: 02-20-2011, 04:59 PM
  2. Newbie needs help with survey design
    By Buakaw in forum Database Design
    Replies: 5
    Last Post: 01-20-2011, 10:20 PM
  3. Replies: 1
    Last Post: 10-29-2010, 02:53 PM
  4. Complex Survey: Table Structure Design and Normalization
    By kevin007 in forum Database Design
    Replies: 2
    Last Post: 07-06-2010, 09:21 AM
  5. Replies: 23
    Last Post: 06-30-2010, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums