Inserting return value from an array into a formfield

M

Martn Camern

I have an aray of keys for an INI file:
iniarray = Array("AgentsName", "AfterHours", "CellPhone", "Fax", "Email")

I iterate through the file as follows:
For x = 0 To 4
strvalue = System.PrivateProfileString(FileName, "MySectionName",
iniarray(x))
' Place strvalue in the form feild iniarray(x).Value
iniarray(x).Text = strvalue
next x

Simple!

However, it doesn't work. I have to do this:
For x = 0 To 4
strvalue = System.PrivateProfileString(FileName, "MySectionName",
iniarray(x))
If (iniarray(x) = "AgentsName") Then
fieldValue.Result = strvalue
'AgentsName.Value = strvalue
ElseIf (iniarray(x) = "AfterHours") Then
AfterHours.Value = strvalue
ElseIf (iniarray(x) = "CellPhone") Then
Cell.Value = strvalue
ElseIf (iniarray(x) = "Fax") Then
Fax.Value = strvalue
ElseIf (iniarray(x) = "Email") Then
Email.Value = strvalue
End If
next x


What is the solution.

If I do MsgBox(iniarray(x)) I get the string name of the textbox!
 
D

DA

I assume what you're trying to do is populate form fields with the values
you're reading out of the settings file

Your first approach is very close. You need to specify your form object
before addressing the form element and its text attribute. Depending on what
type of form you're using:
1) For a VBA Form:
YourFormName(Nameiniarray(x)).Text = strvalue

2)For a document form:
ActiveDocument.FormFields(Nameiniarray(x)).Range.Text = strvalue

Hope that helps.
Dennis
 
D

Doug Robbins - Word MVP

Try

ActiveDocument.FormFields(iniarray(x)).Result = strvalue

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
M

Martn Camern

I think I'm missing something in your reply - because of my lack of
knowledge! I tried the following:
ActiveDocument.FormFields(Nameiniarray(x)).Range.Text = strvalue

But the error that was returned was "The requested member of the collection
does not exist" (Some progress because that's a new error message.

I tink we're almost there but just a tweak needed.

BTW I am using the form for a fax sheet template in Word 2007.

Thanks in advance for your help.
 
M

Martn Camern

Hi Doug

When I try your suggestion, the following error is returned:
"The requested member of the collection does not exist"

I am using the form to fill a fax template.

Regards and thanks for your help

Martin
 
D

Doug Robbins - Word MVP

Are you sure that there are formfields with the the names returned by
iniarray(x)

As an alternative to using formfields, you could load document variables
with the values returned by

System.PrivateProfileString(FileName, "MySectionName", iniarray(x))

and display the information in the documnet by having DOCVARIABLE fields
located in the template.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
M

Martn Camern

Hi Doug

As you can see from the strings in the array, they are the same as the
textbox names. If I do:
MsgBox(iniarray(x) & ".Value")
I get exactly the same text as I have in the If ... Then ... Else routine.
I've even tried to Trim(iniarray(x)) in case there was some hidden character
there, but to no avail.

I also had a thought that it might be a Type problem. So I tried:

Dim fieldName as Formfield
For x = 0 to 4
strvalue = System.PrivateProfileString(FileName, "MySectionName",
iniarray(x))
fieldName=iniarray(x)
MsgBox (fieldName)
fieldName.Value = strvalue
next x

but got an error "Invalid Use of Property".

OK

So I set the type to String. And the MsgBox Returned the correct name of the
TextBox, but using that string as the name of the TextBox as in
fieldName.Value = strvalue
returned an error:
Invalid Qualifier
which suggests to me that the string that I am using a the textbox name is
not correctly interpreted by the compiler - hence my thoughts about typing.

While this is not earth shattering - I can eep the field names in the If ...
Then ... Else routine - I would like to understand WHY it's happening because
to my simpe mind, it's not making sense.

Regards
Martin
 
D

DA

Martin

As far as I can tell you are not using the same string names as the ones in
your If..Then routine.

Your array was (AgentsName, AfterHours, CellPhone, Fax, Email), yet your
conditional routine uses :
fieldValue (the AgentsName line looks to be commented out)
AfterHours (this seems to match)
Cell (instead of CellPhone)
Fax (this seems to match)
Email (this seems to match)

Check the properties of your form elements to :
a) make sure what they are called and match that in your array; and
b) ensure they can support the attribute you're trying to use (i.e. .Text,
Value, etc)

Dennis
 
M

Martn Camern

Here's how weird this is. The following code DOESN'T work:

<Code>

iniarray = Array("AgentsName", "AfterHours", "CellPhone", "Fax", "Email")
FileName = "c:\Documents and Settings\Martin\My
Documents\ini_files\FileName.ini"
For x = 0 To 4
StrValue = System.PrivateProfileString(FileName, "MySectionName",
iniarray(x))
fieldName = iniarray(x) & ".Value"
If (fieldName = "AgentsName.Value") Then
fieldName = StrValue
'AgentsName.Value = StrValue
ElseIf (iniarray(x) = "AfterHours") Then
AfterHours.Value = StrValue
ElseIf (iniarray(x) = "CellPhone") Then
CellPhone.Value = StrValue
ElseIf (iniarray(x) = "Fax") Then
Fax.Value = StrValue
ElseIf (iniarray(x) = "Email") Then
Email.Value = StrValue
End If
Next x

</code>

Yet this routine DOES work:

<code>

iniarray = Array("AgentsName", "AfterHours", "CellPhone", "Fax", "Email")
FileName = "c:\Documents and Settings\Martin\My
Documents\ini_files\FileName.ini"
For x = 0 To 4
StrValue = System.PrivateProfileString(FileName, "MySectionName",
iniarray(x))
fieldName = iniarray(x) & ".Value"
If (fieldName = "AgentsName.Value") Then
'fieldName = StrValue
AgentsName.Value = StrValue
ElseIf (iniarray(x) = "AfterHours") Then
AfterHours.Value = StrValue
ElseIf (iniarray(x) = "CellPhone") Then
CellPhone.Value = StrValue
ElseIf (iniarray(x) = "Fax") Then
Fax.Value = StrValue
ElseIf (iniarray(x) = "Email") Then
Email.Value = StrValue
End If
Next x

</code>

The only difference is that I have commented out the line:
fieldName = StrValue
for the AgentsName field

Regards

Martin Cameron
 
D

DA

What you're describing there is just a problem with the mismatch in data
type. How have you declared your fieldName and StrValue variables?
 
M

Martn Camern

DA said:
What you're describing there is just a problem with the mismatch in data
type. How have you declared your fieldName and StrValue variables?

Dim FileName As String
Dim x As Integer
Dim fieldName As String
Dim StrValue As String
 
M

Martn Camern

I thought it might be helpful if I gave all of the code that I am working
with, as well as the INI file.
So here is the INI file:

[MySectionName]
AgentsName=Martin Cameron
Address=10 Winslow Court
City=Hamilton
AfterHours=07 8553552
CellPhone=021930666
Fax=078397068
[email protected]


And here is the code that works, but is - IMHO - crap:

Private Sub UserForm_Initialize()
Dim FileName As String
Dim x As Integer
Dim fieldName As FormField
Dim StrValue As String
Dim fieldValue As String
iniarray = Array("AgentsName", "Address", "City", "AfterHours",
"CellPhone", "Fax", "Email")
FileName = "c:\Documents and Settings\Martin\My
Documents\ini_files\FileName.ini"
For x = 0 To 6
StrValue = System.PrivateProfileString(FileName, "MySectionName",
iniarray(x))
' Commented out this next line because it just doesn't work
'Set fieldName = iniarray(x)
If (iniarray(x) = "AgentsName") Then
'This should work, but it doesn't
' fieldName.Value = StrValue
AgentsName.Value = StrValue
ElseIf (iniarray(x) = "Address") Then
Address.Value = StrValue
ElseIf (iniarray(x) = "City") Then
City.Value = StrValue
ElseIf (iniarray(x) = "AfterHours") Then
AfterHours.Value = StrValue
ElseIf (iniarray(x) = "CellPhone") Then
CellPhone.Value = StrValue
ElseIf (iniarray(x) = "Fax") Then
Fax.Value = StrValue
ElseIf (iniarray(x) = "Email") Then
Email.Value = StrValue
End If
Next x
End Sub
 
D

DA

Sorry, I feel we're going in circles here. What error are you getting when
you're trying this line:?

fieldName = StrValue
 
J

Jean-Guy Marcil

Martn Camern was telling us:
Martn Camern nous racontait que :
I thought it might be helpful if I gave all of the code that I am
working with, as well as the INI file.
So here is the INI file:

[MySectionName]
AgentsName=Martin Cameron
Address=10 Winslow Court
City=Hamilton
AfterHours=07 8553552
CellPhone=021930666
Fax=078397068
[email protected]


And here is the code that works, but is - IMHO - crap:

Private Sub UserForm_Initialize()
Dim FileName As String
Dim x As Integer
Dim fieldName As FormField
Dim StrValue As String
Dim fieldValue As String
iniarray = Array("AgentsName", "Address", "City", "AfterHours",
"CellPhone", "Fax", "Email")
FileName = "c:\Documents and Settings\Martin\My
Documents\ini_files\FileName.ini"
For x = 0 To 6
StrValue = System.PrivateProfileString(FileName,
"MySectionName", iniarray(x))
' Commented out this next line because it just doesn't work
'Set fieldName = iniarray(x)
If (iniarray(x) = "AgentsName") Then
'This should work, but it doesn't
' fieldName.Value = StrValue
AgentsName.Value = StrValue
ElseIf (iniarray(x) = "Address") Then
Address.Value = StrValue
ElseIf (iniarray(x) = "City") Then
City.Value = StrValue
ElseIf (iniarray(x) = "AfterHours") Then
AfterHours.Value = StrValue
ElseIf (iniarray(x) = "CellPhone") Then
CellPhone.Value = StrValue
ElseIf (iniarray(x) = "Fax") Then
Fax.Value = StrValue
ElseIf (iniarray(x) = "Email") Then
Email.Value = StrValue
End If
Next x
End Sub

From what I can see:

Dim fieldName As FormField

iniarray = Array("AgentsName", "Address", "City", "AfterHours", "CellPhone",
"Fax", "Email")

' Commented out this next line because it just doesn't work
'Set fieldName = iniarray(x)

'This should work, but it doesn't
' fieldName.Value = StrValue

Now, "fieldName" is a FormField object and "iniarray" (Which is not
declared) by virtue of Array is an Array of variants, which are in fact
Strings.
Then, you tried
Set fieldName = iniarray(x)
and ran into problem. THis is becasue you can set a formfield object to a
Variant sub-typed as a String.

So, of course, by the time you get to:
fieldName.Value = StrValue
this does not work as well because fieldName has not been set.

I see that you Dim fieldName as a FormField but this code is part of a
UserForm. I do not see any code that has to do with formfields...

I am not sure I understand the overall goal here, although I can guess most
of it; I am still not sure because to me this seems to be a major
inconsistency.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Martn Camern

Bonjour Jean-Guy

Can you suggest a solution. It's drivng me nuts not being able to geet this,
even though it isn't necessary for the project.
 
J

Jean-Guy Marcil

Martn Camern was telling us:
Martn Camern nous racontait que :
Bonjour Jean-Guy

Can you suggest a solution. It's drivng me nuts not being able to
geet this, even though it isn't necessary for the project.

As I wrote before, I am not sure what FormField is...

Now, if you are dealing with formfields, the following will work:

'_______________________________________
Dim iniarray As Variant
Dim strvalue As String
Dim x As Long

Const fileName As String = "C:\SomeFolder\Test.ini"

iniarray = Array("AgentsName", "AfterHours", "CellPhone", "Fax", "Email")

For x = 0 To 4
strvalue = System.PrivateProfileString(fileName, "MySectionName",
iniarray(x))
'Place strvalue in the form feild iniarray(x).Value
ActiveDocument.Range.FormFields(iniarray(x)).Result = strvalue
Next x
'_______________________________________

However, if you are dealing with controls on a userform, use something like:

'_______________________________________
Dim iniarray As Variant
Dim strvalue As String
Dim x As Long

Const fileName As String = "C:\SomeFolder\Test.ini"

iniarray = Array("AgentsName", "AfterHours", "CellPhone", "Fax", "Email")

For x = 0 To 4
strvalue = System.PrivateProfileString(fileName, "MySectionName",
iniarray(x))
'Place strvalue in the form feild iniarray(x).Value
Me.Controls(iniarray(x)).Text = strvalue
Next x
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Martn Camern

Hi Dennis

With
Dim fieldName as String
fieldName = iniArray(x) & ".Value"
I don't get an error, BUT the value for the key is not returned - ie, the
field in the form corresponding to the key in the array is empty. (Am I
making sense???)

In short, if I use:
AgentsName.Value = StrValue
the field in the form is filled in with the data from the INI file. If
however, I use:
fieldName = StrValue
nothing is returned!

Oh, and by the way, if I do this:
MsgBox(fieldName)
I get the what I expected in the PopUp Box - eg: AgentsName.Value
CellPhone.Value .... etc
 
M

Martn Camern

Bonjour Jean-Guy

Merci!
Merci!
Merci!

This was the answer:

Me.Controls(iniarray(x)).Text = strvalue

I did not understand the fact that I was dealing with Controls on a userform
as distinct from formfields

One last thing - please explain why the fileName variable had to be a Consant?

Reards
Martin Cameron (from 100% Natural New Zealand)

Jean-Guy Marcil said:
Martn Camern was telling us:
Martn Camern nous racontait que :
I thought it might be helpful if I gave all of the code that I am
working with, as well as the INI file.
So here is the INI file:

[MySectionName]
AgentsName=Martin Cameron
Address=10 Winslow Court
City=Hamilton
AfterHours=07 8553552
CellPhone=021930666
Fax=078397068
[email protected]


And here is the code that works, but is - IMHO - crap:

Private Sub UserForm_Initialize()
Dim FileName As String
Dim x As Integer
Dim fieldName As FormField
Dim StrValue As String
Dim fieldValue As String
iniarray = Array("AgentsName", "Address", "City", "AfterHours",
"CellPhone", "Fax", "Email")
FileName = "c:\Documents and Settings\Martin\My
Documents\ini_files\FileName.ini"
For x = 0 To 6
StrValue = System.PrivateProfileString(FileName,
"MySectionName", iniarray(x))
' Commented out this next line because it just doesn't work
'Set fieldName = iniarray(x)
If (iniarray(x) = "AgentsName") Then
'This should work, but it doesn't
' fieldName.Value = StrValue
AgentsName.Value = StrValue
ElseIf (iniarray(x) = "Address") Then
Address.Value = StrValue
ElseIf (iniarray(x) = "City") Then
City.Value = StrValue
ElseIf (iniarray(x) = "AfterHours") Then
AfterHours.Value = StrValue
ElseIf (iniarray(x) = "CellPhone") Then
CellPhone.Value = StrValue
ElseIf (iniarray(x) = "Fax") Then
Fax.Value = StrValue
ElseIf (iniarray(x) = "Email") Then
Email.Value = StrValue
End If
Next x
End Sub

From what I can see:

Dim fieldName As FormField

iniarray = Array("AgentsName", "Address", "City", "AfterHours", "CellPhone",
"Fax", "Email")

' Commented out this next line because it just doesn't work
'Set fieldName = iniarray(x)

'This should work, but it doesn't
' fieldName.Value = StrValue

Now, "fieldName" is a FormField object and "iniarray" (Which is not
declared) by virtue of Array is an Array of variants, which are in fact
Strings.
Then, you tried
Set fieldName = iniarray(x)
and ran into problem. THis is becasue you can set a formfield object to a
Variant sub-typed as a String.

So, of course, by the time you get to:
fieldName.Value = StrValue
this does not work as well because fieldName has not been set.

I see that you Dim fieldName as a FormField but this code is part of a
UserForm. I do not see any code that has to do with formfields...

I am not sure I understand the overall goal here, although I can guess most
of it; I am still not sure because to me this seems to be a major
inconsistency.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

Martn Camern was telling us:
Martn Camern nous racontait que :
Bonjour Jean-Guy

Merci!
Merci!
Merci!

This was the answer:

Me.Controls(iniarray(x)).Text = strvalue

I did not understand the fact that I was dealing with Controls on a
userform as distinct from formfields

Formfields are the fields you insert directly on the page when you create
protected forms.
One last thing - please explain why the fileName variable had to be a
Consant?
You did not have to, a string variable would have worked. It is just that
when I write code, something that will not be changed by a procedure or the
user is a constant.
If the user needs to change this, then of course you would use a string
variable.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top