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] = Null
Forms![frmExampleSheet].[H2] = Null
Forms![frmExampleSheet].[H3] = Null
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, though I know this is not the correct syntax:
'So this loop would clear H1, H2, H3, ..., H20
For i = 1 To 20
Forms![frmExampleSheet].[H[i]] = Null
Next i
I have also attempted something like .[H & i]
and .["H" & i]
, etc. There's got to be a way to do it.
Edit #2 [ Related Inquiry ]:
Would it be possible to do something similar to the above code, but with a SQL injection? For instance, when updating a single record I use this code which works:
'Update Program Number
strSql = "UPDATE tblSheetLog " _
& "SET tblSheetLog.[Prog_No] = '" & Me.Prog_No & "' WHERE tblSheetLog.[Part_No] = '" & Me.Part_Number & "'"
CurrentDb.Execute strSql
But when I try to combine the two formats, say if I have a lot of similar fields (consider H1-H20 again), I do this code but it does not work:
For i = 1 To 20
strSql = "UPDATE tblSheetLog " _
& "SET tblSheetLog("H" & i) = '" & Me("H" & i) & "' WHERE tblSheetLog.[Part_No] = '" & Me.Part_Number & "'"
CurrentDb.Execute strSql
Next i
For reference, I have also tried:
"SET tblSheetLog.["H" & i] = '"
Is something like this possible or should I just do them one by one?