Use control name and control path in string (1 Viewer)

selvsagt

Registered User.
Local time
Today, 13:13
Joined
Jun 29, 2006
Messages
99
Hi.

I am filling out a PDF based on values in my form.
The form has 5 subforms, with a lot of controls on each.

The code works today, but it takes to much time to change.
The idea is that i reference each field in the form (each textbox control) by its name, and then i name the pdf field the same.

So the bottom half of the code below today lookes like this, and it fills out the pdf with customer information. Beneath I have one field per subform as an example. I would have maxed out the number of controls if i kept them in 1 form, therefore I have 5 subforms on my main form. The "button" is launched from the parent form.


First: this code works great today... but is difficult to maintain if the form changes, therefore i would like to improve my code.
Code:
Print #intFile, "<field name=""accountID"">"
Print #intFile, "<value>" & Me!account.Form!accountID & "</value>"
Print #intFile, "</field>"

Print #intFile, "<field name=""selskapstype"">"
Print #intFile, "<value>" & Me!HVV.Form!selskapstype & "</value>"
Print #intFile, "</field>"

Print #intFile, "<field name=""InnsattBel"">"
Print #intFile, "<value>" & Me!idok.Form!InnsattBel & "</value>"
Print #intFile, "</field>"

Print #intFile, "<field name=""etTekst"">"
Print #intFile, "<value>" & Me!et.Form!etTekst & "</value>"
Print #intFile, "</field>"

Print #intFile, "<field name=""FO_tekst"">"
Print #intFile, "<value>" & Me!FOtekst.Form!FO_tekst & "</value>"
Print #intFile, "</field>"

'to close up it adds this at the bottom to open the pdf
Print #intFile, "</fields>"
Print #intFile, "</xfdf>"
Close #intFile
ShellEx strOutput
Exit Sub

Instead of naming each and every control to the same name in the pdf, I am trying to make the program understand that i would like it to do the same for EVERY control the same way, without me typing in everything.

Code:
Dim ctl_input As Access.Control
Dim ctl_ref As Access.Reference

'starts the loop
For Each ctl_input In Me.Controls
            Print #intFile, "<field name=" & ctl_input.Name & ">"
            Print #intFile, "<value>" & ctl_ref & "</value>"
            Print #intFile, "</field>"
Next ctl_input
'continues until there is no more controls then adds the finishing lines

Print #intFile, "</fields>"
Print #intFile, "</xfdf>"
Close #intFile
ShellEx strOutput
Exit Sub

It fails here:
Code:
Print #intFile, "<value>" & ctl_ref & "</value>"
with the error message: Object variable or with block variable not set, run-time error 91.
I guess I am not getting the "path" to the control correctly. the path/ control address should probably be converted to a string... anyone knows how to do this?

Anyone?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 05:13
Joined
Aug 30, 2003
Messages
36,118
I suspect you want

ctl_input.Value

Which should return the value held in the control.
 

selvsagt

Registered User.
Local time
Today, 13:13
Joined
Jun 29, 2006
Messages
99
I suspect you want

ctl_input.Value

Which should return the value held in the control.

Thank you very much, you are absolutely right!

It gave me a new error in the adobe file itself (xml parsing error: not well-formed (invalid token) (error code 4), line 5 column 13 of file).

The reason for this "token error" was how I used quotes. So when I changed it;
Code:
Print #intFile, "<field name=" & "'" & ctl_input.Name & "'" & ">"

It now works! Thank you!
 
Last edited:

pbaldy

Wino Moderator
Staff member
Local time
Today, 05:13
Joined
Aug 30, 2003
Messages
36,118
Happy to help!
 

selvsagt

Registered User.
Local time
Today, 13:13
Joined
Jun 29, 2006
Messages
99
And a new problem occurred..which probably is an easy one.
The button that calls this is on the main form.
The actual data is in 5 different subforms.
The code worked when i called it from the form that actually contains the control, but I dont figure out how to deal with the controls on the subform.

I think I am looking for something like;
if control is subform then subformname.ctl_input.name
and subformname.ctl_input.value

But this doesn't solve the naming of the actual control nor the value in that control...

Is it possible to add a "for each" within the other to handle these differently and how would i reference to the subformname.controlname.value?


Here's what I got, ??? is probably where my issues start:
Code:
Dim ctl_input As Control

For Each ctl_input In Me.Controls
    If TypeOf ctl_input Is TextBox Then
        
        If IsNull(ctl_input) = False Then   'remove all IsNull's
                Print #intFile, "<field name=" & "'" & ctl_input.Name & "'" & ">"
                Print #intFile, "<value>" & ctl_input.Value & "</value>"
                Print #intFile, "</field>"
                    
                
        End If
                
                  
                    If ctl_input.ControlType = acSubform Then
                            Print #intFile, "<field name=" & "'" & ???.ctl_input.Name & "'" & ">"
                            Print #intFile, "<value>" & ???.ctl_input.Value & "</value>"
                            Print #intFile, "</field>"
                               
                    End If
    End If
    Next ctl_input
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 05:13
Joined
Aug 30, 2003
Messages
36,118
Untried, but I think you'd have to have another loop within that section, to loop the controls within the subform. You'd need a separate variable for that loop I suspect. If you have trouble, posting a sample db we can play with would make it easier to help. Off the top of my head, something like:

For Each ctl_input2 In Me(ctl_input.Name).Controls

but again, untested.
 

selvsagt

Registered User.
Local time
Today, 13:13
Joined
Jun 29, 2006
Messages
99
Untried, but I think you'd have to have another loop within that section, to loop the controls within the subform. You'd need a separate variable for that loop I suspect. If you have trouble, posting a sample db we can play with would make it easier to help. Off the top of my head, something like:

For Each ctl_input2 In Me(ctl_input.Name).Controls

but again, untested.

I tried this approach without any luck. This is absolutely doable, but i end up knocking med head against the wall in the naming of the secondary ctl (the one's one the subs).

So, for a quick and dirty fix I ended up doing the same loop 5 times for alle the different subs.



Code:
For Each ctl_input In Me.[COLOR="magenta"]account[/COLOR].Controls
    If TypeOf ctl_input Is TextBox Then
        If IsNull(ctl_input) = False Then   'remove all IsNull's
                Print #intFile, "<field name=" & "'" & ctl_input.Name & "'" & ">"
                Print #intFile, "<value>" & ctl_input.Value & "</value>"
                Print #intFile, "</field>"
                Debug.Print ctl_input.Name & ": " & ctl_input.Value
        End If
    End If
Next ctl_input
   
For Each ctl_input In Me.[COLOR="Magenta"]HVV[/COLOR].Controls
    If TypeOf ctl_input Is TextBox Then
        If IsNull(ctl_input) = False Then   'remove all IsNull's
                Print #intFile, "<field name=" & "'" & ctl_input.Name & "'" & ">"
                Print #intFile, "<value>" & ctl_input.Value & "</value>"
                Print #intFile, "</field>"
                Debug.Print ctl_input.Name & ": " & ctl_input.Value
        End If
    End If
Next ctl_input

and so on....
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 05:13
Joined
Aug 30, 2003
Messages
36,118
Glad you got it working.
 

Users who are viewing this thread

Top Bottom