Access VBA - Looping with counter in variable name

CosmicJTL

I'm relatively new to VBA but learning quickly. While building a form with many fields, I came across something that is not quite an issue but bugs me because I know there is some way for it to be far more efficient.

I will present a small example. Consider the form "frmExampleSheet" which holds 10 fields (holders: H1, H2, H3, and so on). Say those 10 fields currently have data in them, and I build a simple clear button that will wipe the data.

I would have something like this:

    Forms![frmExampleSheet].[H1] = ""
    Forms![frmExampleSheet].[H2] = ""
    Forms![frmExampleSheet].[H3] = ""

And so on...

The efficiency issue comes with this being 10 (or far more) lines long. I am looking for a way to somehow embed the counter into the number value of the field, so that I can just loop through it. Something like this:

    'So this loop would clear H1, H2, H3, ..., H20
    For i = 1 To 20
        Forms![frmExampleSheet].[H[i]] = ""
    Next i

I know this is not the correct syntax because I've tried it and it yelled at me. I have also attempted something like .[H & i] and .["H" & i], etc. There's got to be a way to do it I'm just not finding it and I'm hoping someone can give me a hand with the syntax.

Thanks for your time and consideration.

Edit [ Solved ]:

This syntax worked for me:

    For i = 1 To 20
            Forms!frmSetupSheet("H" & i) = NULL
    Next i
June7

This cannot be done with VBA variables. What you are really doing is dynamically building a reference to a field or control name.

I never allow empty string in fields, I prefer Null. If code is behind frmExampleSheet, can use Me alias. Use parens instead of brackets around field name construction.

Consider:

    'So this loop would clear H1, H2, H3, ..., H20
    For i = 1 To 20
        Me("H" & i) = Null
    Next i

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Timer looping in vba (Access)

From Dev

Underscore at the beginning of a variable name in ms access VBA?

From Dev

Concatenate counter to variable name in Python

From Dev

How to add counter variable to variable name?

From Dev

access vba array by looping through recordset

From Dev

How to select a sheet via a counter variable in vba?

From Dev

Jmeter -- how to combine variable name & counter()

From Dev

Jmeter -- how to combine variable name & counter()

From Dev

AutoHotKey: How to access array with counter variable

From Dev

Access local variable by name

From Dev

access element with variable name

From Dev

access element with variable name

From Dev

Looping and adding to a counter in R

From Dev

Looping counter - shortest method

From Dev

Looping batchfile with counter

From Dev

Looping array using a counter

From Dev

BASH - problems assigning variable using counter in variable name

From Dev

Build Table Name in VBA Access

From Dev

Can't use a variable as column name in VBA Excel query with Access db

From Dev

Function name defined by a variable in VBA

From Dev

VBA use string as variable name

From Dev

Excel VBA: Dynamic Variable Name

From Dev

Set an Array with a variable name VBA

From Dev

Passing a variable into SQL With VBA/Access

From Dev

Access VBA looping through collection, Making SQL statement for each item

From Dev

TensorFlow: Access Contents of a Variable By Name

From Dev

Access objects variable & method by name

From Dev

How to access variable by its name

From Dev

Access objects variable & method by name