0

I'm trying to assign the name of a field in a loop to a specific variable in access vba. Then I need this variable to read a specific record from the database. Below you have the piece of code:

For n = 0 To numfields - 1
        a = "!" & rst.Fields(n).Name
        If IsNull(!Field1) = False Then
            If n = 1 Then strOutput = strOutput & a
            If n <> 1 And n <> 0 Then strOutput = strOutput & ";" & "!Field" & n
        End If
    Next n

The problem is the following: the stored value in the variable would be "!Field(n)" in text and will not get the value from the database in my conditional statement since it's not reading it. Is there any way to convert this text to and actual !Field(n) variable that can read from the database?

Thank you for your help.

5
  • Do you want a to store the name of Fields(n) or the value which is present in Fields(n)? Commented Jan 19, 2016 at 20:07
  • Let's say that on the database there is a Field1 and it has a value of "Hello". I want my Fields(n) to get that value of "Hello". So it would be the value which is present in Fields(n). Commented Jan 19, 2016 at 20:16
  • Then I think you want this: a = rst.Fields(n).Value Commented Jan 19, 2016 at 20:17
  • Awesome, it worked perfectly! Thank you so much, I appreciate it! Commented Jan 19, 2016 at 20:22
  • How do I put it in code in this reply box? I tried before and couldn't. Commented Jan 19, 2016 at 20:32

1 Answer 1

1

Here is the answer to my question. Thanks for your help.

For n = 0 To numfields - 1
        a = rst.Fields(n).Value
        If IsNull(!Field1) = False Then
            If n = 1 Then strOutput = strOutput & a
            If n <> 1 And n <> 0 Then strOutput = strOutput & ";" & a
        End If
Next n
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.